Skip to content

Commit

Permalink
embed/etcd.go: make v2 endpoint optional. fixes #7100
Browse files Browse the repository at this point in the history
  • Loading branch information
vimalk78 committed Jan 18, 2017
1 parent 5222322 commit 79c4918
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
2 changes: 2 additions & 0 deletions embed/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -175,6 +176,7 @@ func NewConfig() *Config {
InitialClusterToken: "etcd-cluster",
StrictReconfigCheck: true,
Metrics: "basic",
EnableV2: true,
}
cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
return cfg
Expand Down
19 changes: 13 additions & 6 deletions embed/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,23 @@ func (e *Etcd) serve() (err error) {
}(l)
}

// 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,
})
v2h := func() http.Handler {
if e.Config().EnableV2 {
// Start a client server goroutine for each listen address
return http.Handler(&cors.CORSHandler{
Handler: v2http.NewClientHandler(e.Server, e.Server.Cfg.ReqTimeout()),
Info: e.cfg.CorsInfo,
})
} else {
plog.Infof("etcd v2 endpoints not enabled.")
return nil
}
}()
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
Expand Down
22 changes: 15 additions & 7 deletions embed/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,19 @@ 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 {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
if otherHandler != nil {
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)
} else {
otherHandler.ServeHTTP(w, r)
}
})
} else {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
grpcServer.ServeHTTP(w, r)
} else {
otherHandler.ServeHTTP(w, r)
}
})
})
}
}

func servePeerHTTP(l net.Listener, handler http.Handler) error {
Expand Down Expand Up @@ -181,7 +187,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
}

Expand Down
3 changes: 3 additions & 0 deletions etcd.conf.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ initial-cluster-state: 'new'
# Reject reconfiguration requests that would cause quorum loss.
strict-reconfig-check: false

# etcd will start the v2 endpoints for the clients.
enable-v2-endpoints: true

# Valid values include 'on', 'readonly', 'off'
proxy: 'off'

Expand Down
1 change: 1 addition & 0 deletions etcdmain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ", ")))
Expand Down
2 changes: 2 additions & 0 deletions etcdmain/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 79c4918

Please sign in to comment.