11906: add /_health/ping to keep-web
authorradhika <radhika@curoverse.com>
Wed, 26 Jul 2017 16:26:56 +0000 (12:26 -0400)
committerradhika <radhika@curoverse.com>
Wed, 26 Jul 2017 16:26:56 +0000 (12:26 -0400)
Arvados-DCO-1.1-Signed-off-by: Radhika Chippada <radhika@curoverse.com>

services/keep-web/handler.go
services/keep-web/handler_test.go
services/keep-web/main.go

index 769c2f59ce9a79effcb2e094fd166978941bfa3a..56484490a462822ea7e6300864f383b711c395a9 100644 (file)
@@ -21,6 +21,7 @@ import (
        "git.curoverse.com/arvados.git/sdk/go/arvados"
        "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
        "git.curoverse.com/arvados.git/sdk/go/auth"
+       "git.curoverse.com/arvados.git/sdk/go/health"
        "git.curoverse.com/arvados.git/sdk/go/httpserver"
        "git.curoverse.com/arvados.git/sdk/go/keepclient"
 )
@@ -29,6 +30,7 @@ type handler struct {
        Config     *Config
        clientPool *arvadosclient.ClientPool
        setupOnce  sync.Once
+       hmux       *http.ServeMux
 }
 
 // parseCollectionIDFromDNSName returns a UUID or PDH if s begins with
@@ -70,7 +72,14 @@ func parseCollectionIDFromURL(s string) string {
 
 func (h *handler) setup() {
        h.clientPool = arvadosclient.MakeClientPool()
+
        keepclient.RefreshServiceDiscoveryOnSIGHUP()
+
+       h.hmux = http.NewServeMux()
+       h.hmux.Handle("/_health/", &health.Handler{
+               Token:  h.Config.ManagementToken,
+               Prefix: "/_health/",
+       })
 }
 
 func (h *handler) serveStatus(w http.ResponseWriter, r *http.Request) {
@@ -82,6 +91,10 @@ func (h *handler) serveStatus(w http.ResponseWriter, r *http.Request) {
        json.NewEncoder(w).Encode(status)
 }
 
+func (h *handler) healthCheck(w http.ResponseWriter, r *http.Request) {
+       h.hmux.ServeHTTP(w, r)
+}
+
 // ServeHTTP implements http.Handler.
 func (h *handler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
        h.setupOnce.Do(h.setup)
@@ -110,6 +123,11 @@ func (h *handler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
                httpserver.Log(remoteAddr, statusCode, statusText, w.WroteBodyBytes(), r.Method, r.Host, r.URL.Path, r.URL.RawQuery)
        }()
 
+       if strings.HasPrefix(r.URL.Path, "/_health/") && r.Method == "GET" {
+               h.healthCheck(w, r)
+               return
+       }
+
        if r.Method == "OPTIONS" {
                method := r.Header.Get("Access-Control-Request-Method")
                if method != "GET" && method != "POST" {
index 1d03b90a3a60e604ef2023a06dada39b000d6f97..04859595e6f337b7975f9305e05189c5aa6f3ce2 100644 (file)
@@ -589,3 +589,24 @@ func (s *IntegrationSuite) TestDirectoryListing(c *check.C) {
                }
        }
 }
+
+func (s *IntegrationSuite) TestHealthCheckPing(c *check.C) {
+       s.testServer.Config.ManagementToken = arvadostest.ManagementToken
+       authHeader := http.Header{
+               "Authorization": {"Bearer " + arvadostest.ManagementToken},
+       }
+
+       resp := httptest.NewRecorder()
+       u := mustParseURL("http://download.example.com/_health/ping")
+       req := &http.Request{
+               Method:     "GET",
+               Host:       u.Host,
+               URL:        u,
+               RequestURI: u.RequestURI(),
+               Header:     authHeader,
+       }
+       s.testServer.Handler.ServeHTTP(resp, req)
+
+       c.Check(resp.Code, check.Equals, http.StatusOK)
+       c.Check(resp.Body.String(), check.Matches, `{"health":"OK"}\n`)
+}
index c4103f409723fbc3c7bbbc3bbd8edcbf7184be42..585dab13e3cbf0ce68ef620c6dad31e4a17c974b 100644 (file)
@@ -34,6 +34,9 @@ type Config struct {
        // Hack to support old command line flag, which is a bool
        // meaning "get actual token from environment".
        deprecatedAllowAnonymous bool
+
+       //Authorization token to be included in all health check requests.
+       ManagementToken string
 }
 
 // DefaultConfig returns the default configuration.
@@ -76,6 +79,9 @@ func main() {
                "Only serve attachments at the given `host:port`"+deprecated)
        flag.BoolVar(&cfg.TrustAllContent, "trust-all-content", false,
                "Serve non-public content from a single origin. Dangerous: read docs before using!"+deprecated)
+       flag.StringVar(&cfg.ManagementToken, "management-token", "",
+               "Authorization token to be included in all health check requests.")
+
        dumpConfig := flag.Bool("dump-config", false,
                "write current configuration to stdout and exit")
        flag.Usage = usage