grpc_proxy.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. "crypto/tls"
  17. "fmt"
  18. "net"
  19. "net/http"
  20. "os"
  21. "time"
  22. "github.com/coreos/etcd/clientv3"
  23. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  24. "github.com/coreos/etcd/pkg/transport"
  25. "github.com/coreos/etcd/proxy/grpcproxy"
  26. "github.com/spf13/cobra"
  27. "google.golang.org/grpc"
  28. "github.com/cockroachdb/cmux"
  29. grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
  30. "github.com/prometheus/client_golang/prometheus"
  31. )
  32. var (
  33. grpcProxyListenAddr string
  34. grpcProxyEndpoints []string
  35. grpcProxyCert string
  36. grpcProxyKey string
  37. grpcProxyCA string
  38. )
  39. func init() {
  40. rootCmd.AddCommand(newGRPCProxyCommand())
  41. }
  42. // newGRPCProxyCommand returns the cobra command for "grpc-proxy".
  43. func newGRPCProxyCommand() *cobra.Command {
  44. lpc := &cobra.Command{
  45. Use: "grpc-proxy <subcommand>",
  46. Short: "grpc-proxy related command",
  47. }
  48. lpc.AddCommand(newGRPCProxyStartCommand())
  49. return lpc
  50. }
  51. func newGRPCProxyStartCommand() *cobra.Command {
  52. cmd := cobra.Command{
  53. Use: "start",
  54. Short: "start the grpc proxy",
  55. Run: startGRPCProxy,
  56. }
  57. cmd.Flags().StringVar(&grpcProxyListenAddr, "listen-addr", "127.0.0.1:23790", "listen address")
  58. cmd.Flags().StringSliceVar(&grpcProxyEndpoints, "endpoints", []string{"127.0.0.1:2379"}, "comma separated etcd cluster endpoints")
  59. cmd.Flags().StringVar(&grpcProxyCert, "cert", "", "identify secure connections with etcd servers using this TLS certificate file")
  60. cmd.Flags().StringVar(&grpcProxyKey, "key", "", "identify secure connections with etcd servers using this TLS key file")
  61. cmd.Flags().StringVar(&grpcProxyCA, "cacert", "", "verify certificates of TLS-enabled secure etcd servers using this CA bundle")
  62. return &cmd
  63. }
  64. func startGRPCProxy(cmd *cobra.Command, args []string) {
  65. l, err := net.Listen("tcp", grpcProxyListenAddr)
  66. if err != nil {
  67. fmt.Fprintln(os.Stderr, err)
  68. os.Exit(1)
  69. }
  70. if l, err = transport.NewKeepAliveListener(l, "tcp", nil); err != nil {
  71. fmt.Fprintln(os.Stderr, err)
  72. os.Exit(1)
  73. }
  74. plog.Infof("listening for grpc-proxy client requests on %s", grpcProxyListenAddr)
  75. defer func() {
  76. l.Close()
  77. plog.Infof("stopping listening for grpc-proxy client requests on %s", grpcProxyListenAddr)
  78. }()
  79. m := cmux.New(l)
  80. cfg, err := newClientCfg()
  81. if err != nil {
  82. fmt.Fprintln(os.Stderr, err)
  83. os.Exit(1)
  84. }
  85. client, err := clientv3.New(*cfg)
  86. if err != nil {
  87. fmt.Fprintln(os.Stderr, err)
  88. os.Exit(1)
  89. }
  90. kvp, _ := grpcproxy.NewKvProxy(client)
  91. watchp, _ := grpcproxy.NewWatchProxy(client)
  92. clusterp := grpcproxy.NewClusterProxy(client)
  93. leasep := grpcproxy.NewLeaseProxy(client)
  94. mainp := grpcproxy.NewMaintenanceProxy(client)
  95. authp := grpcproxy.NewAuthProxy(client)
  96. server := grpc.NewServer(
  97. grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
  98. grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor),
  99. )
  100. pb.RegisterKVServer(server, kvp)
  101. pb.RegisterWatchServer(server, watchp)
  102. pb.RegisterClusterServer(server, clusterp)
  103. pb.RegisterLeaseServer(server, leasep)
  104. pb.RegisterMaintenanceServer(server, mainp)
  105. pb.RegisterAuthServer(server, authp)
  106. errc := make(chan error)
  107. grpcl := m.Match(cmux.HTTP2())
  108. go func() { errc <- server.Serve(grpcl) }()
  109. httpmux := http.NewServeMux()
  110. httpmux.HandleFunc("/", http.NotFound)
  111. httpmux.Handle("/metrics", prometheus.Handler())
  112. srvhttp := &http.Server{
  113. Handler: httpmux,
  114. }
  115. var httpl net.Listener
  116. if cfg.TLS != nil {
  117. srvhttp.TLSConfig = cfg.TLS
  118. httpl = tls.NewListener(m.Match(cmux.Any()), cfg.TLS)
  119. } else {
  120. httpl = m.Match(cmux.HTTP1())
  121. }
  122. go func() { errc <- srvhttp.Serve(httpl) }()
  123. go func() { errc <- m.Serve() }()
  124. // grpc-proxy is initialized, ready to serve
  125. notifySystemd()
  126. fmt.Fprintln(os.Stderr, <-errc)
  127. os.Exit(1)
  128. }
  129. func newClientCfg() (*clientv3.Config, error) {
  130. // set tls if any one tls option set
  131. var cfgtls *transport.TLSInfo
  132. tlsinfo := transport.TLSInfo{}
  133. if grpcProxyCert != "" {
  134. tlsinfo.CertFile = grpcProxyCert
  135. cfgtls = &tlsinfo
  136. }
  137. if grpcProxyKey != "" {
  138. tlsinfo.KeyFile = grpcProxyKey
  139. cfgtls = &tlsinfo
  140. }
  141. if grpcProxyCA != "" {
  142. tlsinfo.CAFile = grpcProxyCA
  143. cfgtls = &tlsinfo
  144. }
  145. cfg := clientv3.Config{
  146. Endpoints: grpcProxyEndpoints,
  147. DialTimeout: 5 * time.Second,
  148. }
  149. if cfgtls != nil {
  150. clientTLS, err := cfgtls.ClientConfig()
  151. if err != nil {
  152. return nil, err
  153. }
  154. cfg.TLS = clientTLS
  155. }
  156. // TODO: support insecure tls
  157. return &cfg, nil
  158. }