|
|
@@ -17,7 +17,6 @@ package v2http
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
- "expvar"
|
|
|
"fmt"
|
|
|
"io/ioutil"
|
|
|
"net/http"
|
|
|
@@ -30,18 +29,15 @@ import (
|
|
|
etcdErr "github.com/coreos/etcd/error"
|
|
|
"github.com/coreos/etcd/etcdserver"
|
|
|
"github.com/coreos/etcd/etcdserver/api"
|
|
|
+ "github.com/coreos/etcd/etcdserver/api/etcdhttp"
|
|
|
"github.com/coreos/etcd/etcdserver/api/v2http/httptypes"
|
|
|
"github.com/coreos/etcd/etcdserver/auth"
|
|
|
"github.com/coreos/etcd/etcdserver/etcdserverpb"
|
|
|
"github.com/coreos/etcd/etcdserver/membership"
|
|
|
"github.com/coreos/etcd/etcdserver/stats"
|
|
|
"github.com/coreos/etcd/pkg/types"
|
|
|
- "github.com/coreos/etcd/raft"
|
|
|
"github.com/coreos/etcd/store"
|
|
|
- "github.com/coreos/etcd/version"
|
|
|
- "github.com/coreos/pkg/capnslog"
|
|
|
"github.com/jonboulle/clockwork"
|
|
|
- "github.com/prometheus/client_golang/prometheus"
|
|
|
"golang.org/x/net/context"
|
|
|
)
|
|
|
|
|
|
@@ -51,17 +47,18 @@ const (
|
|
|
machinesPrefix = "/v2/machines"
|
|
|
membersPrefix = "/v2/members"
|
|
|
statsPrefix = "/v2/stats"
|
|
|
- varsPath = "/debug/vars"
|
|
|
- metricsPath = "/metrics"
|
|
|
- healthPath = "/health"
|
|
|
- versionPath = "/version"
|
|
|
- configPath = "/config"
|
|
|
)
|
|
|
|
|
|
// NewClientHandler generates a muxed http.Handler with the given parameters to serve etcd client requests.
|
|
|
func NewClientHandler(server *etcdserver.EtcdServer, timeout time.Duration) http.Handler {
|
|
|
- sec := auth.NewStore(server, timeout)
|
|
|
+ mux := http.NewServeMux()
|
|
|
+ etcdhttp.HandleBasic(mux, server)
|
|
|
+ handleV2(mux, server, timeout)
|
|
|
+ return requestLogger(mux)
|
|
|
+}
|
|
|
|
|
|
+func handleV2(mux *http.ServeMux, server *etcdserver.EtcdServer, timeout time.Duration) {
|
|
|
+ sec := auth.NewStore(server, timeout)
|
|
|
kh := &keysHandler{
|
|
|
sec: sec,
|
|
|
server: server,
|
|
|
@@ -91,25 +88,16 @@ func NewClientHandler(server *etcdserver.EtcdServer, timeout time.Duration) http
|
|
|
cluster: server.Cluster(),
|
|
|
clientCertAuthEnabled: server.Cfg.ClientCertAuthEnabled,
|
|
|
}
|
|
|
-
|
|
|
- mux := http.NewServeMux()
|
|
|
mux.HandleFunc("/", http.NotFound)
|
|
|
- mux.Handle(healthPath, healthHandler(server))
|
|
|
- mux.HandleFunc(versionPath, versionHandler(server.Cluster(), serveVersion))
|
|
|
mux.Handle(keysPrefix, kh)
|
|
|
mux.Handle(keysPrefix+"/", kh)
|
|
|
mux.HandleFunc(statsPrefix+"/store", sh.serveStore)
|
|
|
mux.HandleFunc(statsPrefix+"/self", sh.serveSelf)
|
|
|
mux.HandleFunc(statsPrefix+"/leader", sh.serveLeader)
|
|
|
- mux.HandleFunc(varsPath, serveVars)
|
|
|
- mux.HandleFunc(configPath+"/local/log", logHandleFunc)
|
|
|
- mux.Handle(metricsPath, prometheus.Handler())
|
|
|
mux.Handle(membersPrefix, mh)
|
|
|
mux.Handle(membersPrefix+"/", mh)
|
|
|
mux.Handle(machinesPrefix, mah)
|
|
|
handleAuth(mux, sech)
|
|
|
-
|
|
|
- return requestLogger(mux)
|
|
|
}
|
|
|
|
|
|
type keysHandler struct {
|
|
|
@@ -319,103 +307,13 @@ func (h *statsHandler) serveLeader(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
stats := h.stats.LeaderStats()
|
|
|
if stats == nil {
|
|
|
- writeError(w, r, httptypes.NewHTTPError(http.StatusForbidden, "not current leader"))
|
|
|
+ etcdhttp.WriteError(w, r, httptypes.NewHTTPError(http.StatusForbidden, "not current leader"))
|
|
|
return
|
|
|
}
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
w.Write(stats)
|
|
|
}
|
|
|
|
|
|
-func serveVars(w http.ResponseWriter, r *http.Request) {
|
|
|
- if !allowMethod(w, r.Method, "GET") {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
- fmt.Fprintf(w, "{\n")
|
|
|
- first := true
|
|
|
- expvar.Do(func(kv expvar.KeyValue) {
|
|
|
- if !first {
|
|
|
- fmt.Fprintf(w, ",\n")
|
|
|
- }
|
|
|
- first = false
|
|
|
- fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
|
|
|
- })
|
|
|
- fmt.Fprintf(w, "\n}\n")
|
|
|
-}
|
|
|
-
|
|
|
-func healthHandler(server *etcdserver.EtcdServer) http.HandlerFunc {
|
|
|
- return func(w http.ResponseWriter, r *http.Request) {
|
|
|
- if !allowMethod(w, r.Method, "GET") {
|
|
|
- return
|
|
|
- }
|
|
|
- if uint64(server.Leader()) == raft.None {
|
|
|
- http.Error(w, `{"health": "false"}`, http.StatusServiceUnavailable)
|
|
|
- return
|
|
|
- }
|
|
|
- ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
- defer cancel()
|
|
|
- if _, err := server.Do(ctx, etcdserverpb.Request{Method: "QGET"}); err != nil {
|
|
|
- http.Error(w, `{"health": "false"}`, http.StatusServiceUnavailable)
|
|
|
- return
|
|
|
- }
|
|
|
- w.WriteHeader(http.StatusOK)
|
|
|
- w.Write([]byte(`{"health": "true"}`))
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func versionHandler(c api.Cluster, fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
|
|
|
- return func(w http.ResponseWriter, r *http.Request) {
|
|
|
- v := c.Version()
|
|
|
- if v != nil {
|
|
|
- fn(w, r, v.String())
|
|
|
- } else {
|
|
|
- fn(w, r, "not_decided")
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func serveVersion(w http.ResponseWriter, r *http.Request, clusterV string) {
|
|
|
- if !allowMethod(w, r.Method, "GET") {
|
|
|
- return
|
|
|
- }
|
|
|
- vs := version.Versions{
|
|
|
- Server: version.Version,
|
|
|
- Cluster: clusterV,
|
|
|
- }
|
|
|
-
|
|
|
- w.Header().Set("Content-Type", "application/json")
|
|
|
- b, err := json.Marshal(&vs)
|
|
|
- if err != nil {
|
|
|
- plog.Panicf("cannot marshal versions to json (%v)", err)
|
|
|
- }
|
|
|
- w.Write(b)
|
|
|
-}
|
|
|
-
|
|
|
-func logHandleFunc(w http.ResponseWriter, r *http.Request) {
|
|
|
- if !allowMethod(w, r.Method, "PUT") {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- in := struct{ Level string }{}
|
|
|
-
|
|
|
- d := json.NewDecoder(r.Body)
|
|
|
- if err := d.Decode(&in); err != nil {
|
|
|
- writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid json body"))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- logl, err := capnslog.ParseLevel(strings.ToUpper(in.Level))
|
|
|
- if err != nil {
|
|
|
- writeError(w, r, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid log level "+in.Level))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- plog.Noticef("globalLogLevel set to %q", logl.String())
|
|
|
- capnslog.SetGlobalLogLevel(logl)
|
|
|
- w.WriteHeader(http.StatusNoContent)
|
|
|
-}
|
|
|
-
|
|
|
// parseKeyRequest converts a received http.Request on keysPrefix to
|
|
|
// a server Request, performing validation of supplied fields as appropriate.
|
|
|
// If any validation fails, an empty Request and non-nil error is returned.
|