gateway.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/proxy/tcpproxy"
  22. "github.com/spf13/cobra"
  23. )
  24. var (
  25. gatewayListenAddr string
  26. gatewayEndpoints []string
  27. gatewayDNSCluster string
  28. getewayRetryDelay time.Duration
  29. )
  30. var (
  31. rootCmd = &cobra.Command{
  32. Use: "etcd",
  33. Short: "etcd server",
  34. SuggestFor: []string{"etcd"},
  35. }
  36. )
  37. func init() {
  38. rootCmd.AddCommand(newGatewayCommand())
  39. }
  40. // newGatewayCommand returns the cobra command for "gateway".
  41. func newGatewayCommand() *cobra.Command {
  42. lpc := &cobra.Command{
  43. Use: "gateway <subcommand>",
  44. Short: "gateway related command",
  45. }
  46. lpc.AddCommand(newGatewayStartCommand())
  47. return lpc
  48. }
  49. func newGatewayStartCommand() *cobra.Command {
  50. cmd := cobra.Command{
  51. Use: "start",
  52. Short: "start the gateway",
  53. Run: startGateway,
  54. }
  55. cmd.Flags().StringVar(&gatewayListenAddr, "listen-addr", "127.0.0.1:23790", "listen address")
  56. cmd.Flags().StringVar(&gatewayDNSCluster, "discovery-srv", "", "DNS domain used to bootstrap initial cluster")
  57. cmd.Flags().StringSliceVar(&gatewayEndpoints, "endpoints", []string{"127.0.0.1:2379"}, "comma separated etcd cluster endpoints")
  58. cmd.Flags().DurationVar(&getewayRetryDelay, "retry-delay", time.Minute, "duration of delay before retrying failed endpoints")
  59. return &cmd
  60. }
  61. func startGateway(cmd *cobra.Command, args []string) {
  62. endpoints := gatewayEndpoints
  63. if gatewayDNSCluster != "" {
  64. eps, err := client.NewSRVDiscover().Discover(gatewayDNSCluster)
  65. if err != nil {
  66. fmt.Fprintln(os.Stderr, err)
  67. os.Exit(1)
  68. }
  69. plog.Infof("discovered the cluster %s from %s", eps, gatewayDNSCluster)
  70. }
  71. l, err := net.Listen("tcp", gatewayListenAddr)
  72. if err != nil {
  73. fmt.Fprintln(os.Stderr, err)
  74. os.Exit(1)
  75. }
  76. tp := tcpproxy.TCPProxy{
  77. Listener: l,
  78. Endpoints: endpoints,
  79. MonitorInterval: getewayRetryDelay,
  80. }
  81. tp.Run()
  82. }