From f80914fba21662cc376773ed1af434cd12702cb4 Mon Sep 17 00:00:00 2001 From: Vimal Kumar Date: Mon, 16 Jan 2017 22:25:26 +0530 Subject: [PATCH] embed/etcd.go: make v2 endpoint optional. fixes #7100 --- Documentation/op-guide/configuration.md | 6 ++++++ embed/config.go | 2 ++ embed/etcd.go | 13 ++++++++----- embed/serve.go | 9 ++++++++- etcd.conf.yml.sample | 3 +++ etcdmain/config.go | 1 + etcdmain/help.go | 2 ++ 7 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Documentation/op-guide/configuration.md b/Documentation/op-guide/configuration.md index 1ae45f51a..4c60ddd79 100644 --- a/Documentation/op-guide/configuration.md +++ b/Documentation/op-guide/configuration.md @@ -140,6 +140,12 @@ To start etcd automatically using custom settings at startup in Linux, using a [ + default: 0 + env variable: ETCD_AUTO_COMPACTION_RETENTION + +### --enable-v2 ++ Accept etcd V2 client requests ++ default: true ++ env variable: ETCD_ENABLE_V2 + ## Proxy flags `--proxy` prefix flags configures etcd to run in [proxy mode][proxy]. "proxy" supports v2 API only. diff --git a/embed/config.go b/embed/config.go index 05b425264..e1c6df2db 100644 --- a/embed/config.go +++ b/embed/config.go @@ -102,6 +102,7 @@ type Config struct { InitialCluster string `json:"initial-cluster"` InitialClusterToken string `json:"initial-cluster-token"` StrictReconfigCheck bool `json:"strict-reconfig-check"` + EnableV2 bool `json:"enable-v2"` // security @@ -175,6 +176,7 @@ func NewConfig() *Config { InitialClusterToken: "etcd-cluster", StrictReconfigCheck: true, Metrics: "basic", + EnableV2: true, } cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name) return cfg diff --git a/embed/etcd.go b/embed/etcd.go index b80179841..c33efa4f8 100644 --- a/embed/etcd.go +++ b/embed/etcd.go @@ -319,15 +319,18 @@ func (e *Etcd) serve() (err error) { } // Start a client server goroutine for each listen address - ch := http.Handler(&cors.CORSHandler{ - Handler: v2http.NewClientHandler(e.Server, e.Server.Cfg.ReqTimeout()), - Info: e.cfg.CorsInfo, - }) + var v2h http.Handler + if e.Config().EnableV2 { + v2h = http.Handler(&cors.CORSHandler{ + Handler: v2http.NewClientHandler(e.Server, e.Server.Cfg.ReqTimeout()), + Info: e.cfg.CorsInfo, + }) + } for _, sctx := range e.sctxs { // read timeout does not work with http close notify // TODO: https://github.com/golang/go/issues/9524 go func(s *serveCtx) { - e.errc <- s.serve(e.Server, ctlscfg, ch, e.errc) + e.errc <- s.serve(e.Server, ctlscfg, v2h, e.errc) }(sctx) } return nil diff --git a/embed/serve.go b/embed/serve.go index 84b97615c..d63638058 100644 --- a/embed/serve.go +++ b/embed/serve.go @@ -122,6 +122,11 @@ func (sctx *serveCtx) serve(s *etcdserver.EtcdServer, tlscfg *tls.Config, handle // grpcHandlerFunc returns an http.Handler that delegates to grpcServer on incoming gRPC // connections or otherHandler otherwise. Copied from cockroachdb. func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler { + if otherHandler == nil { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + grpcServer.ServeHTTP(w, r) + }) + } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") { grpcServer.ServeHTTP(w, r) @@ -181,7 +186,9 @@ func (sctx *serveCtx) createMux(gwmux *gw.ServeMux, handler http.Handler) *http. } httpmux.Handle("/v3alpha/", gwmux) - httpmux.Handle("/", handler) + if handler != nil { + httpmux.Handle("/", handler) + } return httpmux } diff --git a/etcd.conf.yml.sample b/etcd.conf.yml.sample index f945ce258..7a3e802eb 100644 --- a/etcd.conf.yml.sample +++ b/etcd.conf.yml.sample @@ -69,6 +69,9 @@ initial-cluster-state: 'new' # Reject reconfiguration requests that would cause quorum loss. strict-reconfig-check: false +# Accept etcd V2 client requests +enable-v2: true + # Valid values include 'on', 'readonly', 'off' proxy: 'off' diff --git a/etcdmain/config.go b/etcdmain/config.go index 4037ff768..27aaaa222 100644 --- a/etcdmain/config.go +++ b/etcdmain/config.go @@ -155,6 +155,7 @@ func newConfig() *config { plog.Panicf("unexpected error setting up clusterStateFlag: %v", err) } fs.BoolVar(&cfg.StrictReconfigCheck, "strict-reconfig-check", cfg.StrictReconfigCheck, "Reject reconfiguration requests that would cause quorum loss.") + fs.BoolVar(&cfg.EnableV2, "enable-v2", true, "Accept etcd V2 client requests.") // proxy fs.Var(cfg.proxy, "proxy", fmt.Sprintf("Valid values include %s", strings.Join(cfg.proxy.Values, ", "))) diff --git a/etcdmain/help.go b/etcdmain/help.go index 0ce4be636..11bddfc68 100644 --- a/etcdmain/help.go +++ b/etcdmain/help.go @@ -88,6 +88,8 @@ clustering flags: reject reconfiguration requests that would cause quorum loss. --auto-compaction-retention '0' auto compaction retention in hour. 0 means disable auto compaction. + --enable-v2 + Accept etcd V2 client requests. proxy flags: "proxy" supports v2 API only.