From 72dd4a18c534453a97ae52b7611d19b8b1f1b363 Mon Sep 17 00:00:00 2001 From: Hitoshi Mitake Date: Mon, 31 Dec 2018 19:33:37 +0900 Subject: [PATCH] *: add a new option --enable-grpc-gateway for enabling/disabling grpc gateway --- embed/config.go | 3 +++ embed/etcd.go | 1 + embed/serve.go | 52 ++++++++++++++++++++++++-------------------- etcdmain/config.go | 3 +++ etcdserver/config.go | 2 ++ 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/embed/config.go b/embed/config.go index d9317b956..01a61f3c9 100644 --- a/embed/config.go +++ b/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. diff --git a/embed/etcd.go b/embed/etcd.go index d61692a3a..7cfce805b 100644 --- a/embed/etcd.go +++ b/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 { diff --git a/embed/serve.go b/embed/serve.go index af82dbe50..7cbcc9e57 100644 --- a/embed/serve.go +++ b/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) } diff --git a/etcdmain/config.go b/etcdmain/config.go index 78700e74d..db5ece034 100644 --- a/etcdmain/config.go +++ b/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.") diff --git a/etcdserver/config.go b/etcdserver/config.go index 5bf10ea4d..d037b83b9 100644 --- a/etcdserver/config.go +++ b/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