-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.go
84 lines (69 loc) · 1.86 KB
/
main.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
80
81
82
83
84
// Package main holds the entrypoint for kubenurse
package main
import (
"context"
"errors"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/postfinance/kubenurse/internal/kubenurse"
corev1 "k8s.io/api/core/v1"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
restConf, err := controllerruntime.GetConfig()
if err != nil {
slog.Error("error during controllerruntime.GetConfig()", "err", err)
return
}
kubenurseNs := os.Getenv("KUBENURSE_NAMESPACE")
ca, err := cache.New(restConf, cache.Options{
ByObject: map[client.Object]cache.ByObject{
&corev1.Pod{}: {Namespaces: map[string]cache.Config{
kubenurseNs: {},
}},
&corev1.Node{}: {},
},
})
if err != nil {
slog.Error("error during cache creation", "err", err)
return
}
go func() {
if err = ca.Start(ctx); err != nil && !errors.Is(err, context.Canceled) {
slog.Error("controller-runtime client cache error", "err", err)
cancel()
}
}()
opts := client.Options{
Cache: &client.CacheOptions{
Reader: ca,
},
}
c, err := client.New(restConf, opts)
if err != nil {
slog.Error("error while starting controller-runtime client", "err", err)
return
}
server, err := kubenurse.New(c)
if err != nil {
slog.Error("error in kubenurse.New call", "err", err)
return
}
go func() {
<-ctx.Done() // blocks until ctx is canceled
slog.Info("shutting down, received signal to stop")
if err := server.Shutdown(); err != nil {
slog.Error("error during graceful shutdown", "err", err)
}
}()
// blocks, until the server is stopped by calling Shutdown()
if err := server.Run(ctx); err != nil {
slog.Error("error while running kubenurse", "err", err)
}
}