/
manager.go
79 lines (66 loc) · 2.33 KB
/
manager.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
70
71
72
73
74
75
76
77
78
79
package k8s
import (
"fmt"
"net/http"
"net/http/pprof"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/manager"
apiv1 "github.com/110y/bootes/internal/k8s/api/v1"
)
const (
healthzEndpoint = "/healthz"
readyzEndpoint = "/readyz"
healthzName = "healthz"
readyzName = "readyz"
pprofEndpointPrefix = "/debug/pprof"
pprofIndexEndpoint = pprofEndpointPrefix + "/"
pprofCmdlineEndpoint = pprofEndpointPrefix + "/cmdlilne"
pprofProfileEndpoint = pprofEndpointPrefix + "/profile"
pprofSymbolEndpoint = pprofEndpointPrefix + "/symbol"
pprofTraceEndpoint = pprofEndpointPrefix + "/trace"
)
var pprofHandlerMap = map[string]http.HandlerFunc{
pprofIndexEndpoint: pprof.Index,
pprofCmdlineEndpoint: pprof.Cmdline,
pprofProfileEndpoint: pprof.Profile,
pprofSymbolEndpoint: pprof.Symbol,
pprofTraceEndpoint: pprof.Trace,
}
func NewManager(c *ManagerConfig) (manager.Manager, error) {
s := runtime.NewScheme()
if err := scheme.AddToScheme(s); err != nil {
return nil, fmt.Errorf("failed to create new scheme: %w", err)
}
if err := apiv1.AddToScheme(s); err != nil {
return nil, fmt.Errorf("failed to add scheme to apiv1: %w", err)
}
cfg, err := ctrl.GetConfig()
if err != nil {
return nil, fmt.Errorf("failed to get kubernetes configuration: %w", err)
}
manager, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: s,
ReadinessEndpointName: readyzEndpoint,
LivenessEndpointName: healthzEndpoint,
HealthProbeBindAddress: fmt.Sprintf(":%d", c.HealthzServerPort),
MetricsBindAddress: fmt.Sprintf(":%d", c.MetricsServerPort),
})
if err != nil {
return nil, fmt.Errorf("failed to create manager: %w", err)
}
if err := manager.AddHealthzCheck(healthzName, healthz.Ping); err != nil {
return nil, fmt.Errorf("failed to register healthz checker: %w", err)
}
if err := manager.AddReadyzCheck(readyzName, healthz.Ping); err != nil {
return nil, fmt.Errorf("failed to register readyz checker: %w", err)
}
for endpoint, handler := range pprofHandlerMap {
if err := manager.AddMetricsExtraHandler(endpoint, http.HandlerFunc(handler)); err != nil {
return nil, fmt.Errorf("failed to register pprof handlers: %w", err)
}
}
return manager, nil
}