From 2cf41cea78b7c5bfd1cb3da77b4260b8a7270e56 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Fri, 12 Jan 2018 02:52:29 +0200 Subject: [PATCH] added version endpoint --- pkg/server/handlers.go | 25 +++++++++++++++++++++++++ pkg/server/server.go | 1 + 2 files changed, 26 insertions(+) diff --git a/pkg/server/handlers.go b/pkg/server/handlers.go index 9159aa2..2e2ade2 100644 --- a/pkg/server/handlers.go +++ b/pkg/server/handlers.go @@ -6,6 +6,7 @@ import ( "sync/atomic" "github.com/golang/glog" + "github.com/stefanprodan/k8s-podinfo/pkg/version" "gopkg.in/yaml.v2" ) @@ -52,6 +53,30 @@ func (s *Server) echo(w http.ResponseWriter, r *http.Request) { } } +func (s *Server) version(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/version" { + w.WriteHeader(http.StatusNotFound) + return + } + + resp := map[string]string{ + "version": version.VERSION, + "commit": version.GITCOMMIT, + } + + d, err := yaml.Marshal(resp) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + w.Header().Set("Content-Type", "text/x-yaml; charset=utf-8") + w.Header().Set("X-Content-Type-Options", "nosniff") + w.WriteHeader(http.StatusOK) + w.Write(d) +} + func (s *Server) healthz(w http.ResponseWriter, r *http.Request) { if atomic.LoadInt32(&healthy) == 1 { w.WriteHeader(http.StatusOK) diff --git a/pkg/server/server.go b/pkg/server/server.go index e5407ed..22779be 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -34,6 +34,7 @@ func NewServer(options ...func(*Server)) *Server { s.mux.HandleFunc("/readyz/disable", s.disable) s.mux.HandleFunc("/echo", s.echo) s.mux.HandleFunc("/panic", s.panic) + s.mux.HandleFunc("/version", s.version) s.mux.Handle("/metrics", promhttp.Handler()) return s