-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandlers.go
More file actions
170 lines (150 loc) · 4.27 KB
/
handlers.go
File metadata and controls
170 lines (150 loc) · 4.27 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package controller
import (
"context"
"fmt"
"log/slog"
"time"
"github.com/github/deployment-tracker/pkg/dtmetrics"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
)
// registerEventHandlers adds pod event handlers to the informer. Events
// are filtered and enqueued to the controller's work queue for processing.
func (c *Controller) registerEventHandlers() error {
_, err := c.podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj any) {
pod, ok := obj.(*corev1.Pod)
if !ok {
slog.Error("Invalid object returned",
"object", obj,
)
return
}
// Only process pods that are running and belong
// to a deployment
if pod.Status.Phase == corev1.PodRunning && getDeploymentName(pod) != "" {
key, err := cache.MetaNamespaceKeyFunc(obj)
// For our purposes, there are in practice
// no error event we care about, so don't
// bother with handling it.
if err == nil {
c.workqueue.Add(PodEvent{
Key: key,
EventType: EventCreated,
})
}
}
},
UpdateFunc: func(oldObj, newObj any) {
oldPod, ok := oldObj.(*corev1.Pod)
if !ok {
slog.Error("Invalid old object returned",
"object", oldObj,
)
return
}
newPod, ok := newObj.(*corev1.Pod)
if !ok {
slog.Error("Invalid new object returned",
"object", newObj,
)
return
}
// Skip if pod is being deleted or doesn't belong
// to a deployment
if newPod.DeletionTimestamp != nil || getDeploymentName(newPod) == "" {
return
}
// Only process if pod just became running.
// We need to process this as often when a container
// is created, the spec does not contain the digest
// so we need to wait for the status field to be
// populated from where we can get the digest.
if oldPod.Status.Phase != corev1.PodRunning &&
newPod.Status.Phase == corev1.PodRunning {
key, err := cache.MetaNamespaceKeyFunc(newObj)
// For our purposes, there are in practice
// no error event we care about, so don't
// bother with handling it.
if err == nil {
c.workqueue.Add(PodEvent{
Key: key,
EventType: EventCreated,
})
}
}
},
DeleteFunc: func(obj any) {
pod, ok := obj.(*corev1.Pod)
if !ok {
// Handle deleted final state unknown
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
return
}
pod, ok = tombstone.Obj.(*corev1.Pod)
if !ok {
return
}
}
// Only process pods that belong to a deployment
if getDeploymentName(pod) == "" {
return
}
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
// For our purposes, there are in practice
// no error event we care about, so don't
// bother with handling it.
if err == nil {
c.workqueue.Add(PodEvent{
Key: key,
EventType: EventDeleted,
DeletedPod: pod,
})
}
},
})
if err != nil {
return fmt.Errorf("failed to add event handlers: %w", err)
}
return nil
}
// runWorker runs a worker to process items from the work queue.
func (c *Controller) runWorker(ctx context.Context) {
for c.processNextItem(ctx) {
}
}
// startWorkers launches the specified number of workers and blocks until
// the context is cancelled.
func (c *Controller) startWorkers(ctx context.Context, workers int) {
for i := 0; i < workers; i++ {
go wait.UntilWithContext(ctx, c.runWorker, time.Second)
}
}
// processNextItem processes the next item from the work queue.
func (c *Controller) processNextItem(ctx context.Context) bool {
event, shutdown := c.workqueue.Get()
if shutdown {
return false
}
defer c.workqueue.Done(event)
start := time.Now()
err := c.processEvent(ctx, event)
dur := time.Since(start)
if err == nil {
dtmetrics.EventsProcessedOk.WithLabelValues(event.EventType).Inc()
dtmetrics.EventsProcessedTimer.WithLabelValues("ok").Observe(dur.Seconds())
c.workqueue.Forget(event)
return true
}
dtmetrics.EventsProcessedTimer.WithLabelValues("failed").Observe(dur.Seconds())
dtmetrics.EventsProcessedFailed.WithLabelValues(event.EventType).Inc()
// Requeue on error with rate limiting
slog.Error("Failed to process event, requeuing",
"event_key", event.Key,
"error", err,
)
c.workqueue.AddRateLimited(event)
return true
}