Merge branch 'master' into 13822-nm-delayed-daemon
[arvados.git] / sdk / go / health / aggregator.go
index 334584b6225f98c59bbaa8912550c9566739b109..a6cb8798aa328a468c1db98c3c3e5bf38773f15c 100644 (file)
@@ -1,8 +1,13 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: Apache-2.0
+
 package health
 
 import (
        "context"
        "encoding/json"
+       "errors"
        "fmt"
        "net"
        "net/http"
@@ -82,7 +87,7 @@ type ClusterHealthResponse struct {
        // exposes problems that can't be expressed in Checks, like
        // "service S is needed, but isn't configured to run
        // anywhere."
-       Services map[string]ServiceHealth `json:"services"`
+       Services map[arvados.ServiceName]ServiceHealth `json:"services"`
 }
 
 type CheckResult struct {
@@ -103,13 +108,13 @@ func (agg *Aggregator) ClusterHealth(cluster *arvados.Cluster) ClusterHealthResp
        resp := ClusterHealthResponse{
                Health:   "OK",
                Checks:   make(map[string]CheckResult),
-               Services: make(map[string]ServiceHealth),
+               Services: make(map[arvados.ServiceName]ServiceHealth),
        }
 
        mtx := sync.Mutex{}
        wg := sync.WaitGroup{}
-       for node, nodeConfig := range cluster.SystemNodes {
-               for svc, addr := range nodeConfig.ServicePorts() {
+       for profileName, profile := range cluster.NodeProfiles {
+               for svc, addr := range profile.ServicePorts() {
                        // Ensure svc is listed in resp.Services.
                        mtx.Lock()
                        if _, ok := resp.Services[svc]; !ok {
@@ -123,10 +128,10 @@ func (agg *Aggregator) ClusterHealth(cluster *arvados.Cluster) ClusterHealthResp
                        }
 
                        wg.Add(1)
-                       go func(node, svc, addr string) {
+                       go func(profileName string, svc arvados.ServiceName, addr string) {
                                defer wg.Done()
                                var result CheckResult
-                               url, err := agg.pingURL(node, addr)
+                               url, err := agg.pingURL(profileName, addr)
                                if err != nil {
                                        result = CheckResult{
                                                Health: "ERROR",
@@ -138,7 +143,7 @@ func (agg *Aggregator) ClusterHealth(cluster *arvados.Cluster) ClusterHealthResp
 
                                mtx.Lock()
                                defer mtx.Unlock()
-                               resp.Checks[svc+"+"+url] = result
+                               resp.Checks[fmt.Sprintf("%s+%s", svc, url)] = result
                                if result.Health == "OK" {
                                        h := resp.Services[svc]
                                        h.N++
@@ -147,7 +152,7 @@ func (agg *Aggregator) ClusterHealth(cluster *arvados.Cluster) ClusterHealthResp
                                } else {
                                        resp.Health = "ERROR"
                                }
-                       }(node, svc, addr)
+                       }(profileName, svc, addr)
                }
        }
        wg.Wait()
@@ -199,10 +204,14 @@ func (agg *Aggregator) ping(url string, cluster *arvados.Cluster) (result CheckR
        err = json.NewDecoder(resp.Body).Decode(&result.Response)
        if err != nil {
                err = fmt.Errorf("cannot decode response: %s", err)
-               return
        } else if resp.StatusCode != http.StatusOK {
                err = fmt.Errorf("HTTP %d %s", resp.StatusCode, resp.Status)
-               return
+       } else if h, _ := result.Response["health"].(string); h != "OK" {
+               if e, ok := result.Response["error"].(string); ok && e != "" {
+                       err = errors.New(e)
+               } else {
+                       err = fmt.Errorf("health=%q in ping response", h)
+               }
        }
        return
 }