gateway.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2016 CoreOS, Inc.
  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/proxy/tcpproxy"
  21. "github.com/spf13/cobra"
  22. )
  23. var (
  24. gatewayListenAddr string
  25. gatewayEndpoints []string
  26. getewayRetryDelay time.Duration
  27. )
  28. var (
  29. rootCmd = &cobra.Command{
  30. Use: "etcd",
  31. Short: "etcd server",
  32. SuggestFor: []string{"etcd"},
  33. }
  34. )
  35. func init() {
  36. rootCmd.AddCommand(newGatewayCommand())
  37. }
  38. // newGatewayCommand returns the cobra command for "gateway".
  39. func newGatewayCommand() *cobra.Command {
  40. lpc := &cobra.Command{
  41. Use: "gateway <subcommand>",
  42. Short: "gateway related command",
  43. }
  44. lpc.AddCommand(newGatewayStartCommand())
  45. return lpc
  46. }
  47. func newGatewayStartCommand() *cobra.Command {
  48. cmd := cobra.Command{
  49. Use: "start",
  50. Short: "start the gateway",
  51. Run: startGateway,
  52. }
  53. cmd.Flags().StringVar(&gatewayListenAddr, "listen-addr", "127.0.0.1:23790", "listen address")
  54. cmd.Flags().StringSliceVar(&gatewayEndpoints, "endpoints", []string{"127.0.0.1:2379"}, "comma separated etcd cluster endpoints")
  55. cmd.Flags().DurationVar(&getewayRetryDelay, "retry-delay", time.Minute, "duration of delay before retrying failed endpoints")
  56. return &cmd
  57. }
  58. func startGateway(cmd *cobra.Command, args []string) {
  59. l, err := net.Listen("tcp", gatewayListenAddr)
  60. if err != nil {
  61. fmt.Fprintln(os.Stderr, err)
  62. os.Exit(1)
  63. }
  64. tp := tcpproxy.TCPProxy{
  65. Listener: l,
  66. Endpoints: gatewayEndpoints,
  67. MonitorInterval: getewayRetryDelay,
  68. }
  69. tp.Run()
  70. }