13 "git.curoverse.com/arvados.git/sdk/go/arvados"
14 "git.curoverse.com/arvados.git/sdk/go/auth"
17 const defaultTimeout = arvados.Duration(2 * time.Second)
19 // Aggregator implements http.Handler. It handles "GET /_health/all"
20 // by checking the health of all configured services on the cluster
21 // and responding 200 if everything is healthy.
22 type Aggregator struct {
24 httpClient *http.Client
25 timeout arvados.Duration
27 Config *arvados.Config
29 // If non-nil, Log is called after handling each request.
30 Log func(*http.Request, error)
33 func (agg *Aggregator) setup() {
34 agg.httpClient = http.DefaultClient
36 // this is always the case, except in the test suite
37 agg.timeout = defaultTimeout
41 func (agg *Aggregator) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
42 agg.setupOnce.Do(agg.setup)
43 sendErr := func(statusCode int, err error) {
44 resp.WriteHeader(statusCode)
45 json.NewEncoder(resp).Encode(map[string]string{"error": err.Error()})
51 resp.Header().Set("Content-Type", "application/json")
53 cluster, err := agg.Config.GetCluster("")
55 err = fmt.Errorf("arvados.GetCluster(): %s", err)
56 sendErr(http.StatusInternalServerError, err)
59 if !agg.checkAuth(req, cluster) {
60 sendErr(http.StatusUnauthorized, errUnauthorized)
63 if req.URL.Path != "/_health/all" {
64 sendErr(http.StatusNotFound, errNotFound)
67 json.NewEncoder(resp).Encode(agg.ClusterHealth(cluster))
73 type ClusterHealthResponse struct {
74 // "OK" if all needed services are OK, otherwise "ERROR".
75 Health string `json:"health"`
77 // An entry for each known health check of each known instance
78 // of each needed component: "instance of service S on node N
79 // reports health-check C is OK."
80 Checks map[string]CheckResult `json:"checks"`
82 // An entry for each service type: "service S is OK." This
83 // exposes problems that can't be expressed in Checks, like
84 // "service S is needed, but isn't configured to run
86 Services map[string]ServiceHealth `json:"services"`
89 type CheckResult struct {
90 Health string `json:"health"`
91 Error string `json:"error,omitempty"`
92 HTTPStatusCode int `json:",omitempty"`
93 HTTPStatusText string `json:",omitempty"`
94 Response map[string]interface{} `json:"response"`
95 ResponseTime json.Number `json:"responseTime"`
98 type ServiceHealth struct {
99 Health string `json:"health"`
103 func (agg *Aggregator) ClusterHealth(cluster *arvados.Cluster) ClusterHealthResponse {
104 resp := ClusterHealthResponse{
106 Checks: make(map[string]CheckResult),
107 Services: make(map[string]ServiceHealth),
111 wg := sync.WaitGroup{}
112 for node, nodeConfig := range cluster.SystemNodes {
113 for svc, addr := range nodeConfig.ServicePorts() {
114 // Ensure svc is listed in resp.Services.
116 if _, ok := resp.Services[svc]; !ok {
117 resp.Services[svc] = ServiceHealth{Health: "ERROR"}
122 // svc is not expected on this node.
127 go func(node, svc, addr string) {
129 var result CheckResult
130 url, err := agg.pingURL(node, addr)
132 result = CheckResult{
137 result = agg.ping(url, cluster)
142 resp.Checks[svc+"+"+url] = result
143 if result.Health == "OK" {
144 h := resp.Services[svc]
147 resp.Services[svc] = h
149 resp.Health = "ERROR"
156 // Report ERROR if a needed service didn't fail any checks
157 // merely because it isn't configured to run anywhere.
158 for _, sh := range resp.Services {
159 if sh.Health != "OK" {
160 resp.Health = "ERROR"
167 func (agg *Aggregator) pingURL(node, addr string) (string, error) {
168 _, port, err := net.SplitHostPort(addr)
169 return "http://" + node + ":" + port + "/_health/ping", err
172 func (agg *Aggregator) ping(url string, cluster *arvados.Cluster) (result CheckResult) {
177 result.ResponseTime = json.Number(fmt.Sprintf("%.6f", time.Since(t0).Seconds()))
179 result.Health, result.Error = "ERROR", err.Error()
185 req, err := http.NewRequest("GET", url, nil)
189 req.Header.Set("Authorization", "Bearer "+cluster.ManagementToken)
191 ctx, cancel := context.WithTimeout(req.Context(), time.Duration(agg.timeout))
193 req = req.WithContext(ctx)
194 resp, err := agg.httpClient.Do(req)
198 result.HTTPStatusCode = resp.StatusCode
199 result.HTTPStatusText = resp.Status
200 err = json.NewDecoder(resp.Body).Decode(&result.Response)
202 err = fmt.Errorf("cannot decode response: %s", err)
203 } else if resp.StatusCode != http.StatusOK {
204 err = fmt.Errorf("HTTP %d %s", resp.StatusCode, resp.Status)
205 } else if h, _ := result.Response["health"].(string); h != "OK" {
206 if e, ok := result.Response["error"].(string); ok && e != "" {
209 err = fmt.Errorf("health=%q in ping response", h)
215 func (agg *Aggregator) checkAuth(req *http.Request, cluster *arvados.Cluster) bool {
216 creds := auth.NewCredentialsFromHTTPRequest(req)
217 for _, token := range creds.Tokens {
218 if token != "" && token == cluster.ManagementToken {