diff --git a/etcdserver/etcdhttp/client.go b/etcdserver/etcdhttp/client.go index f4a264e04..3a1fe87c9 100644 --- a/etcdserver/etcdhttp/client.go +++ b/etcdserver/etcdhttp/client.go @@ -358,7 +358,7 @@ func serveVersion(w http.ResponseWriter, r *http.Request) { if !allowMethod(w, r.Method, "GET") { return } - w.Write([]byte("etcd " + version.Version)) + w.Write(version.MarshalJSON()) } // parseKeyRequest converts a received http.Request on keysPrefix to diff --git a/etcdserver/etcdhttp/client_test.go b/etcdserver/etcdhttp/client_test.go index 8a1cf5599..16270a7ef 100644 --- a/etcdserver/etcdhttp/client_test.go +++ b/etcdserver/etcdhttp/client_test.go @@ -18,7 +18,6 @@ import ( "bytes" "encoding/json" "errors" - "fmt" "io/ioutil" "net/http" "net/http/httptest" @@ -1327,9 +1326,9 @@ func TestServeVersion(t *testing.T) { if rw.Code != http.StatusOK { t.Errorf("code=%d, want %d", rw.Code, http.StatusOK) } - w := fmt.Sprintf("etcd %s", version.Version) - if g := rw.Body.String(); g != w { - t.Fatalf("body = %q, want %q", g, w) + w := version.MarshalJSON() + if g := rw.Body.String(); g != string(w) { + t.Fatalf("body = %q, want %q", g, string(w)) } } diff --git a/etcdserver/etcdhttp/peer.go b/etcdserver/etcdhttp/peer.go index 0b2b47707..4df354472 100644 --- a/etcdserver/etcdhttp/peer.go +++ b/etcdserver/etcdhttp/peer.go @@ -40,6 +40,7 @@ func NewPeerHandler(clusterInfo etcdserver.ClusterInfo, timer etcdserver.RaftTim mux.Handle(rafthttp.RaftPrefix, raftHandler) mux.Handle(rafthttp.RaftPrefix+"/", raftHandler) mux.Handle(peerMembersPrefix, mh) + mux.HandleFunc(versionPath, serveVersion) return mux } diff --git a/version/version.go b/version/version.go index 19a790b7d..9b3423699 100644 --- a/version/version.go +++ b/version/version.go @@ -15,6 +15,8 @@ package version import ( + "encoding/json" + "log" "os" "path" @@ -37,6 +39,21 @@ const ( DataDir2_0_1 DataDirVersion = "2.0.1" ) +type Versions struct { + Server string `json:"etcdserver"` + // TODO: etcdcluster version + // TODO: raft state machine version +} + +// MarshalJSON returns the JSON encoding of Versions struct. +func MarshalJSON() []byte { + b, err := json.Marshal(Versions{Server: Version}) + if err != nil { + log.Panicf("version: cannot marshal versions to json (%v)", err) + } + return b +} + func DetectDataDir(dirpath string) (DataDirVersion, error) { names, err := fileutil.ReadDir(dirpath) if err != nil {