-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
health.go
59 lines (50 loc) · 1.54 KB
/
health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package server
import (
"fmt"
"os"
"github.com/go-openapi/runtime/middleware"
healthModels "github.com/cilium/cilium/api/v1/health/models"
. "github.com/cilium/cilium/api/v1/health/server/restapi"
ciliumModels "github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/api"
"github.com/cilium/cilium/pkg/client"
"github.com/cilium/cilium/pkg/option"
)
type getHealthz struct {
*Server
}
// NewGetHealthzHandler handles health requests.
func NewGetHealthzHandler(s *Server) GetHealthzHandler {
return &getHealthz{Server: s}
}
func (h *getHealthz) getCiliumStatus() (*ciliumModels.StatusResponse, error) {
resp, err := h.Daemon.GetHealthz(nil)
if err != nil {
return nil, client.Hint(err)
}
return resp.Payload, nil
}
// Handle handles GET requests for /healthz .
func (h *getHealthz) Handle(params GetHealthzParams) middleware.Responder {
log.Debug("Handling request for /healthz")
if _, err := os.Stat(option.Config.SocketPath); os.IsNotExist(err) {
return api.Error(GetHealthzFailedCode, fmt.Errorf("missing socket: %s does not exist", option.Config.SocketPath))
}
ciliumStatus, err := h.getCiliumStatus()
if err != nil {
return api.Error(GetHealthzFailedCode, err)
}
load, err := dumpLoad()
if err != nil {
return api.Error(GetHealthzFailedCode, err)
}
sr := healthModels.HealthResponse{}
if ciliumStatus != nil {
sr.Cilium = *ciliumStatus
}
sr.Uptime = h.Server.DumpUptime()
sr.SystemLoad = load
return NewGetHealthzOK().WithPayload(&sr)
}