gateway.go 3.4 KB

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