gateway.go 3.7 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. "github.com/coreos/etcd/client"
  22. "github.com/coreos/etcd/pkg/transport"
  23. "github.com/coreos/etcd/proxy/tcpproxy"
  24. "github.com/spf13/cobra"
  25. )
  26. var (
  27. gatewayListenAddr string
  28. gatewayEndpoints []string
  29. gatewayDNSCluster 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().BoolVar(&gatewayInsecureDiscovery, "insecure-discovery", false, "accept insecure SRV records")
  62. cmd.Flags().StringVar(&gatewayCA, "trusted-ca-file", "", "path to the client server TLS CA file.")
  63. cmd.Flags().StringSliceVar(&gatewayEndpoints, "endpoints", []string{"127.0.0.1:2379"}, "comma separated etcd cluster endpoints")
  64. cmd.Flags().DurationVar(&getewayRetryDelay, "retry-delay", time.Minute, "duration of delay before retrying failed endpoints")
  65. return &cmd
  66. }
  67. func stripSchema(eps []string) []string {
  68. var endpoints []string
  69. for _, ep := range eps {
  70. if u, err := url.Parse(ep); err == nil && u.Host != "" {
  71. ep = u.Host
  72. }
  73. endpoints = append(endpoints, ep)
  74. }
  75. return endpoints
  76. }
  77. func startGateway(cmd *cobra.Command, args []string) {
  78. endpoints := gatewayEndpoints
  79. if gatewayDNSCluster != "" {
  80. eps, err := client.NewSRVDiscover().Discover(gatewayDNSCluster)
  81. if err != nil {
  82. fmt.Fprintln(os.Stderr, err)
  83. os.Exit(1)
  84. }
  85. plog.Infof("discovered the cluster %s from %s", eps, gatewayDNSCluster)
  86. // confirm TLS connections are good
  87. if !gatewayInsecureDiscovery {
  88. tlsInfo := transport.TLSInfo{
  89. TrustedCAFile: gatewayCA,
  90. ServerName: gatewayDNSCluster,
  91. }
  92. plog.Infof("validating discovered endpoints %v", eps)
  93. endpoints, err = transport.ValidateSecureEndpoints(tlsInfo, eps)
  94. if err != nil {
  95. plog.Warningf("%v", err)
  96. }
  97. plog.Infof("using discovered endpoints %v", endpoints)
  98. }
  99. }
  100. // Strip the schema from the endpoints because we start just a TCP proxy
  101. endpoints = stripSchema(endpoints)
  102. if len(endpoints) == 0 {
  103. plog.Fatalf("no endpoints found")
  104. }
  105. l, err := net.Listen("tcp", gatewayListenAddr)
  106. if err != nil {
  107. fmt.Fprintln(os.Stderr, err)
  108. os.Exit(1)
  109. }
  110. tp := tcpproxy.TCPProxy{
  111. Listener: l,
  112. Endpoints: endpoints,
  113. MonitorInterval: getewayRetryDelay,
  114. }
  115. // At this point, etcd gateway listener is initialized
  116. notifySystemd()
  117. tp.Run()
  118. }