gateway.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "os"
  19. "time"
  20. "github.com/coreos/etcd/client"
  21. "github.com/coreos/etcd/pkg/transport"
  22. "github.com/coreos/etcd/proxy/tcpproxy"
  23. "github.com/spf13/cobra"
  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 startGateway(cmd *cobra.Command, args []string) {
  67. endpoints := gatewayEndpoints
  68. if gatewayDNSCluster != "" {
  69. eps, err := client.NewSRVDiscover().Discover(gatewayDNSCluster)
  70. if err != nil {
  71. fmt.Fprintln(os.Stderr, err)
  72. os.Exit(1)
  73. }
  74. plog.Infof("discovered the cluster %s from %s", eps, gatewayDNSCluster)
  75. // confirm TLS connections are good
  76. if !gatewayInsecureDiscovery {
  77. tlsInfo := transport.TLSInfo{
  78. TrustedCAFile: gatewayCA,
  79. ServerName: gatewayDNSCluster,
  80. }
  81. plog.Infof("validating discovered endpoints %v", eps)
  82. endpoints, err = transport.ValidateSecureEndpoints(tlsInfo, eps)
  83. if err != nil {
  84. plog.Warningf("%v", err)
  85. }
  86. plog.Infof("using discovered endpoints %v", endpoints)
  87. }
  88. }
  89. if len(endpoints) == 0 {
  90. plog.Fatalf("no endpoints found")
  91. }
  92. l, err := net.Listen("tcp", gatewayListenAddr)
  93. if err != nil {
  94. fmt.Fprintln(os.Stderr, err)
  95. os.Exit(1)
  96. }
  97. tp := tcpproxy.TCPProxy{
  98. Listener: l,
  99. Endpoints: gatewayEndpoints,
  100. MonitorInterval: getewayRetryDelay,
  101. }
  102. tp.Run()
  103. }