gateway.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. gatewayInsecureDiscovery bool
  30. getewayRetryDelay time.Duration
  31. gatewayCA string
  32. )
  33. var (
  34. rootCmd = &cobra.Command{
  35. Use: "etcd",
  36. Short: "etcd server",
  37. SuggestFor: []string{"etcd"},
  38. }
  39. )
  40. func init() {
  41. rootCmd.AddCommand(newGatewayCommand())
  42. }
  43. // newGatewayCommand returns the cobra command for "gateway".
  44. func newGatewayCommand() *cobra.Command {
  45. lpc := &cobra.Command{
  46. Use: "gateway <subcommand>",
  47. Short: "gateway related command",
  48. }
  49. lpc.AddCommand(newGatewayStartCommand())
  50. return lpc
  51. }
  52. func newGatewayStartCommand() *cobra.Command {
  53. cmd := cobra.Command{
  54. Use: "start",
  55. Short: "start the gateway",
  56. Run: startGateway,
  57. }
  58. cmd.Flags().StringVar(&gatewayListenAddr, "listen-addr", "127.0.0.1:23790", "listen address")
  59. cmd.Flags().StringVar(&gatewayDNSCluster, "discovery-srv", "", "DNS domain used to bootstrap initial cluster")
  60. cmd.Flags().BoolVar(&gatewayInsecureDiscovery, "insecure-discovery", false, "accept insecure SRV records")
  61. cmd.Flags().StringVar(&gatewayCA, "trusted-ca-file", "", "path to the client server TLS CA file.")
  62. cmd.Flags().StringSliceVar(&gatewayEndpoints, "endpoints", []string{"127.0.0.1:2379"}, "comma separated etcd cluster endpoints")
  63. cmd.Flags().DurationVar(&getewayRetryDelay, "retry-delay", time.Minute, "duration of delay before retrying failed endpoints")
  64. return &cmd
  65. }
  66. func stripSchema(eps []string) []string {
  67. var endpoints []string
  68. for _, ep := range eps {
  69. if u, err := url.Parse(ep); err == nil && u.Host != "" {
  70. ep = u.Host
  71. }
  72. endpoints = append(endpoints, ep)
  73. }
  74. return endpoints
  75. }
  76. func startGateway(cmd *cobra.Command, args []string) {
  77. var lg *zap.Logger
  78. lg, err := zap.NewProduction()
  79. if err != nil {
  80. fmt.Fprintln(os.Stderr, err)
  81. os.Exit(1)
  82. }
  83. srvs := discoverEndpoints(lg, gatewayDNSCluster, gatewayCA, gatewayInsecureDiscovery)
  84. if len(srvs.Endpoints) == 0 {
  85. // no endpoints discovered, fall back to provided endpoints
  86. srvs.Endpoints = gatewayEndpoints
  87. }
  88. // Strip the schema from the endpoints because we start just a TCP proxy
  89. srvs.Endpoints = stripSchema(srvs.Endpoints)
  90. if len(srvs.SRVs) == 0 {
  91. for _, ep := range srvs.Endpoints {
  92. h, p, serr := net.SplitHostPort(ep)
  93. if serr != nil {
  94. fmt.Printf("error parsing endpoint %q", ep)
  95. os.Exit(1)
  96. }
  97. var port uint16
  98. fmt.Sscanf(p, "%d", &port)
  99. srvs.SRVs = append(srvs.SRVs, &net.SRV{Target: h, Port: port})
  100. }
  101. }
  102. if len(srvs.Endpoints) == 0 {
  103. fmt.Println("no endpoints found")
  104. os.Exit(1)
  105. }
  106. var l net.Listener
  107. l, err = net.Listen("tcp", gatewayListenAddr)
  108. if err != nil {
  109. fmt.Fprintln(os.Stderr, err)
  110. os.Exit(1)
  111. }
  112. tp := tcpproxy.TCPProxy{
  113. Logger: lg,
  114. Listener: l,
  115. Endpoints: srvs.SRVs,
  116. MonitorInterval: getewayRetryDelay,
  117. }
  118. // At this point, etcd gateway listener is initialized
  119. notifySystemd(lg)
  120. tp.Run()
  121. }