gateway.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etcdmain
  15. import (
  16. "fmt"
  17. "net"
  18. "net/url"
  19. "os"
  20. "time"
  21. "go.etcd.io/etcd/proxy/tcpproxy"
  22. "github.com/spf13/cobra"
  23. "go.uber.org/zap"
  24. )
  25. var (
  26. gatewayListenAddr string
  27. gatewayEndpoints []string
  28. gatewayDNSCluster string
  29. gatewayDNSClusterServiceName string
  30. gatewayInsecureDiscovery bool
  31. getewayRetryDelay time.Duration
  32. gatewayCA string
  33. )
  34. var (
  35. rootCmd = &cobra.Command{
  36. Use: "etcd",
  37. Short: "etcd server",
  38. SuggestFor: []string{"etcd"},
  39. }
  40. )
  41. func init() {
  42. rootCmd.AddCommand(newGatewayCommand())
  43. }
  44. // newGatewayCommand returns the cobra command for "gateway".
  45. func newGatewayCommand() *cobra.Command {
  46. lpc := &cobra.Command{
  47. Use: "gateway <subcommand>",
  48. Short: "gateway related command",
  49. }
  50. lpc.AddCommand(newGatewayStartCommand())
  51. return lpc
  52. }
  53. func newGatewayStartCommand() *cobra.Command {
  54. cmd := cobra.Command{
  55. Use: "start",
  56. Short: "start the gateway",
  57. Run: startGateway,
  58. }
  59. cmd.Flags().StringVar(&gatewayListenAddr, "listen-addr", "127.0.0.1:23790", "listen address")
  60. cmd.Flags().StringVar(&gatewayDNSCluster, "discovery-srv", "", "DNS domain used to bootstrap initial cluster")
  61. cmd.Flags().StringVar(&gatewayDNSClusterServiceName, "discovery-srv-name", "", "service name to query when using DNS discovery")
  62. cmd.Flags().BoolVar(&gatewayInsecureDiscovery, "insecure-discovery", false, "accept insecure SRV records")
  63. cmd.Flags().StringVar(&gatewayCA, "trusted-ca-file", "", "path to the client server TLS CA file.")
  64. cmd.Flags().StringSliceVar(&gatewayEndpoints, "endpoints", []string{"127.0.0.1:2379"}, "comma separated etcd cluster endpoints")
  65. cmd.Flags().DurationVar(&getewayRetryDelay, "retry-delay", time.Minute, "duration of delay before retrying failed endpoints")
  66. return &cmd
  67. }
  68. func stripSchema(eps []string) []string {
  69. var endpoints []string
  70. for _, ep := range eps {
  71. if u, err := url.Parse(ep); err == nil && u.Host != "" {
  72. ep = u.Host
  73. }
  74. endpoints = append(endpoints, ep)
  75. }
  76. return endpoints
  77. }
  78. func startGateway(cmd *cobra.Command, args []string) {
  79. var lg *zap.Logger
  80. lg, err := zap.NewProduction()
  81. if err != nil {
  82. fmt.Fprintln(os.Stderr, err)
  83. os.Exit(1)
  84. }
  85. srvs := discoverEndpoints(lg, gatewayDNSCluster, gatewayCA, gatewayInsecureDiscovery, gatewayDNSClusterServiceName)
  86. if len(srvs.Endpoints) == 0 {
  87. // no endpoints discovered, fall back to provided endpoints
  88. srvs.Endpoints = gatewayEndpoints
  89. }
  90. // Strip the schema from the endpoints because we start just a TCP proxy
  91. srvs.Endpoints = stripSchema(srvs.Endpoints)
  92. if len(srvs.SRVs) == 0 {
  93. for _, ep := range srvs.Endpoints {
  94. h, p, serr := net.SplitHostPort(ep)
  95. if serr != nil {
  96. fmt.Printf("error parsing endpoint %q", ep)
  97. os.Exit(1)
  98. }
  99. var port uint16
  100. fmt.Sscanf(p, "%d", &port)
  101. srvs.SRVs = append(srvs.SRVs, &net.SRV{Target: h, Port: port})
  102. }
  103. }
  104. if len(srvs.Endpoints) == 0 {
  105. fmt.Println("no endpoints found")
  106. os.Exit(1)
  107. }
  108. var l net.Listener
  109. l, err = net.Listen("tcp", gatewayListenAddr)
  110. if err != nil {
  111. fmt.Fprintln(os.Stderr, err)
  112. os.Exit(1)
  113. }
  114. tp := tcpproxy.TCPProxy{
  115. Logger: lg,
  116. Listener: l,
  117. Endpoints: srvs.SRVs,
  118. MonitorInterval: getewayRetryDelay,
  119. }
  120. // At this point, etcd gateway listener is initialized
  121. notifySystemd(lg)
  122. tp.Run()
  123. }