-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
111 lines (102 loc) · 2.82 KB
/
Copy pathmain.go
File metadata and controls
111 lines (102 loc) · 2.82 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"context"
"fmt"
"os"
"strings"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"
)
func init() {
log.SetLogger(zap.New())
}
func main() {
cfg := config.GetConfigOrDie()
fmt.Println("setting the manager")
mgr, err := manager.New(cfg, manager.Options{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("registering the controller")
c, err := controller.New("annotator-controller", mgr, controller.Options{
Reconciler: &reconcilePod{client: mgr.GetClient()},
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("watching pods")
err = c.Watch(source.Kind(mgr.GetCache(), &corev1.Pod{}, &handler.TypedEnqueueRequestForObject[*corev1.Pod]{}, typedPodPredicates()))
// err = c.Watch(source.Kind(mgr.GetCache(), &corev1.Pod{}, &handler.TypedEnqueueRequestForObject[*corev1.Pod]{}))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("starting the manager")
err = mgr.Start(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "problem running manager: %v\n", err)
os.Exit(1)
}
}
func GetConfigAnnotation() (string, error) {
cfg := config.GetConfigOrDie()
cl, err := client.New(cfg, client.Options{})
cm := &corev1.ConfigMap{}
err = cl.Get(context.Background(), client.ObjectKey{
Name: "annotation-config",
Namespace: "default",
}, cm)
if err != nil {
return "", err
}
annotationKey := cm.Data["annotation_key"]
if annotationKey == "" {
return "", nil
}
return annotationKey, nil
}
func typedPodPredicates() predicate.TypedFuncs[*corev1.Pod] {
return predicate.TypedFuncs[*corev1.Pod]{
CreateFunc: func(e event.TypedCreateEvent[*corev1.Pod]) bool {
return true
},
UpdateFunc: func(e event.TypedUpdateEvent[*corev1.Pod]) bool {
old := e.ObjectOld
new := e.ObjectNew
annotationKey, err := GetConfigAnnotation()
if err != nil {
return true
}
oldVal := ""
if old.Annotations != nil {
oldVal = old.Annotations[annotationKey]
}
newVal := ""
if new.Annotations != nil {
newVal = new.Annotations[annotationKey]
}
// Only reconcile if the value changed, or if it's still missing
if strings.EqualFold(oldVal, newVal) && strings.EqualFold(newVal, "true") {
return false
}
return true
},
DeleteFunc: func(e event.TypedDeleteEvent[*corev1.Pod]) bool {
return true
},
GenericFunc: func(e event.TypedGenericEvent[*corev1.Pod]) bool {
return false
},
}
}