forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth.go
69 lines (56 loc) · 1.73 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
60
61
62
63
64
65
66
67
68
69
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package main
import (
"fmt"
"net/http"
"github.com/spf13/pflag"
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/hive"
"github.com/cilium/cilium/pkg/hive/cell"
k8sClient "github.com/cilium/cilium/pkg/k8s/client"
"github.com/cilium/cilium/pkg/option"
)
type HealthAPIServerConfig struct {
ClusterMeshHealthPort int
}
func (HealthAPIServerConfig) Flags(flags *pflag.FlagSet) {
flags.Int(option.ClusterMeshHealthPort, defaults.ClusterMeshHealthPort, "TCP port for ClusterMesh apiserver health API")
}
var healthAPIServerCell = cell.Module(
"health-api-server",
"ClusterMesh Health API Server",
cell.Config(HealthAPIServerConfig{}),
cell.Invoke(registerHealthAPIServer),
)
func registerHealthAPIServer(lc hive.Lifecycle, clientset k8sClient.Clientset, cfg HealthAPIServerConfig) {
mux := http.NewServeMux()
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
statusCode := http.StatusOK
reply := "ok"
if _, err := clientset.Discovery().ServerVersion(); err != nil {
statusCode = http.StatusInternalServerError
reply = err.Error()
}
w.WriteHeader(statusCode)
if _, err := w.Write([]byte(reply)); err != nil {
log.WithError(err).Error("Failed to respond to /healthz request")
}
})
srv := &http.Server{
Handler: mux,
Addr: fmt.Sprintf(":%d", cfg.ClusterMeshHealthPort),
}
lc.Append(hive.Hook{
OnStart: func(hive.HookContext) error {
go func() {
log.Info("Started health API")
if err := srv.ListenAndServe(); err != nil {
log.WithError(err).Fatalf("Unable to start health API")
}
}()
return nil
},
OnStop: func(ctx hive.HookContext) error { return srv.Shutdown(ctx) },
})
}