Browse Source

*: add a new option --enable-grpc-gateway for enabling/disabling grpc gateway

Hitoshi Mitake 7 years ago
parent
commit
72dd4a18c5
5 changed files with 38 additions and 23 deletions
  1. 3 0
      embed/config.go
  2. 1 0
      embed/etcd.go
  3. 29 23
      embed/serve.go
  4. 3 0
      etcdmain/config.go
  5. 2 0
      etcdserver/config.go

+ 3 - 0
embed/config.go

@@ -318,6 +318,9 @@ type Config struct {
 	loggerCore        zapcore.Core
 	loggerWriteSyncer zapcore.WriteSyncer
 
+	// EnableGRPCGateway is false to disable grpc gateway.
+	EnableGRPCGateway bool `json:"enable-grpc-gateway"`
+
 	// TO BE DEPRECATED
 
 	// LogPkgLevels is being deprecated in v3.5.

+ 1 - 0
embed/etcd.go

@@ -200,6 +200,7 @@ func StartEtcd(inCfg *Config) (e *Etcd, err error) {
 		LoggerWriteSyncer:          cfg.loggerWriteSyncer,
 		Debug:                      cfg.Debug,
 		ForceNewCluster:            cfg.ForceNewCluster,
+		EnableGRPCGateway:          cfg.EnableGRPCGateway,
 	}
 	print(e.cfg.logger, *cfg, srvcfg, memberInitialized)
 	if e.Server, err = etcdserver.NewServer(srvcfg); err != nil {

+ 29 - 23
embed/serve.go

@@ -118,9 +118,11 @@ func (sctx *serveCtx) serve(
 		go func() { errHandler(gs.Serve(grpcl)) }()
 
 		var gwmux *gw.ServeMux
-		gwmux, err = sctx.registerGateway([]grpc.DialOption{grpc.WithInsecure()})
-		if err != nil {
-			return err
+		if s.Cfg.EnableGRPCGateway {
+			gwmux, err = sctx.registerGateway([]grpc.DialOption{grpc.WithInsecure()})
+			if err != nil {
+				return err
+			}
 		}
 
 		httpmux := sctx.createMux(gwmux, handler)
@@ -156,15 +158,17 @@ func (sctx *serveCtx) serve(
 		}
 		handler = grpcHandlerFunc(gs, handler)
 
-		dtls := tlscfg.Clone()
-		// trust local server
-		dtls.InsecureSkipVerify = true
-		creds := credentials.NewTLS(dtls)
-		opts := []grpc.DialOption{grpc.WithTransportCredentials(creds)}
 		var gwmux *gw.ServeMux
-		gwmux, err = sctx.registerGateway(opts)
-		if err != nil {
-			return err
+		if s.Cfg.EnableGRPCGateway {
+			dtls := tlscfg.Clone()
+			// trust local server
+			dtls.InsecureSkipVerify = true
+			creds := credentials.NewTLS(dtls)
+			opts := []grpc.DialOption{grpc.WithTransportCredentials(creds)}
+			gwmux, err = sctx.registerGateway(opts)
+			if err != nil {
+				return err
+			}
 		}
 
 		var tlsl net.Listener
@@ -270,19 +274,21 @@ func (sctx *serveCtx) createMux(gwmux *gw.ServeMux, handler http.Handler) *http.
 		httpmux.Handle(path, h)
 	}
 
-	httpmux.Handle(
-		"/v3/",
-		wsproxy.WebsocketProxy(
-			gwmux,
-			wsproxy.WithRequestMutator(
-				// Default to the POST method for streams
-				func(_ *http.Request, outgoing *http.Request) *http.Request {
-					outgoing.Method = "POST"
-					return outgoing
-				},
+	if gwmux != nil {
+		httpmux.Handle(
+			"/v3/",
+			wsproxy.WebsocketProxy(
+				gwmux,
+				wsproxy.WithRequestMutator(
+					// Default to the POST method for streams
+					func(_ *http.Request, outgoing *http.Request) *http.Request {
+						outgoing.Method = "POST"
+						return outgoing
+					},
+				),
 			),
-		),
-	)
+		)
+	}
 	if handler != nil {
 		httpmux.Handle("/", handler)
 	}

+ 3 - 0
etcdmain/config.go

@@ -241,6 +241,9 @@ func newConfig() *config {
 	fs.StringVar(&cfg.ec.AuthToken, "auth-token", cfg.ec.AuthToken, "Specify auth token specific options.")
 	fs.UintVar(&cfg.ec.BcryptCost, "bcrypt-cost", cfg.ec.BcryptCost, "Specify bcrypt algorithm cost factor for auth password hashing.")
 
+	// gateway
+	fs.BoolVar(&cfg.ec.EnableGRPCGateway, "enable-grpc-gateway", true, "Enable GRPC gateway.")
+
 	// experimental
 	fs.BoolVar(&cfg.ec.ExperimentalInitialCorruptCheck, "experimental-initial-corrupt-check", cfg.ec.ExperimentalInitialCorruptCheck, "Enable to check data corruption before serving any client/peer traffic.")
 	fs.DurationVar(&cfg.ec.ExperimentalCorruptCheckTime, "experimental-corrupt-check-time", cfg.ec.ExperimentalCorruptCheckTime, "Duration of time between cluster corruption check passes.")

+ 2 - 0
etcdserver/config.go

@@ -148,6 +148,8 @@ type ServerConfig struct {
 
 	// LeaseCheckpointInterval time.Duration is the wait duration between lease checkpoints.
 	LeaseCheckpointInterval time.Duration
+
+	EnableGRPCGateway bool
 }
 
 // VerifyBootstrap sanity-checks the initial config for bootstrap case