/
controller.go
322 lines (284 loc) · 9.9 KB
/
controller.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package controller
import (
"context"
"fmt"
"strconv"
"time"
"github.com/lnsp/k8s-crash-informer/pkg/chat"
"github.com/lnsp/k8s-crash-informer/pkg/client"
"github.com/lnsp/k8s-crash-informer/pkg/utils"
"k8s.io/klog"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
type Controller struct {
queue workqueue.RateLimitingInterface
cacheIndexer cache.Indexer
cacheController cache.Controller
chat chat.Client
clientset kubernetes.Interface
timeouts map[string]time.Time
}
func (c *Controller) processNextItem() bool {
// Wait until there is a new item in the working queue
key, quit := c.queue.Get()
if quit {
return false
}
// Tell the queue that we are done with processing this key. This unblocks the key for other workers
// This allows safe parallel processing because two pods with the same key are never processed in
// parallel.
defer c.queue.Done(key)
// Invoke the method containing the business logic
err := c.syncToStdout(key.(string))
// Handle the error if something went wrong during the execution of the business logic
c.handleErr(err, key)
return true
}
const (
annotationEnableMattermost = "espe.tech/crash-informer"
annotationEnableMattermostInform = "true"
)
func (c *Controller) hasValidAnnotation(pod *v1.Pod) bool {
podAnnotations := pod.ObjectMeta.Annotations
if podAnnotations == nil {
podAnnotations = make(map[string]string)
}
if podAnnotations[annotationEnableMattermost] == annotationEnableMattermostInform {
return true
}
// Backtrack to deployment using ownerReferences
rs, err := c.findPodReplicaSet(pod)
if err != nil {
klog.Infof("Failed to get replicaset: %v", err)
return false
}
rsAnnotations := rs.ObjectMeta.Annotations
if rsAnnotations == nil {
rsAnnotations = make(map[string]string)
}
podAnnotations[annotationMattermostBackoff] = rsAnnotations[annotationMattermostBackoff]
if rsAnnotations[annotationEnableMattermost] == annotationEnableMattermostInform {
return true
}
dep, err := c.findPodDeployment(pod, rs)
if err != nil {
klog.Infof("Failed to get deployment: %v", err)
return false
}
depAnnotations := dep.ObjectMeta.Annotations
if depAnnotations == nil {
depAnnotations = make(map[string]string)
}
podAnnotations[annotationMattermostBackoff] = depAnnotations[annotationMattermostBackoff]
return depAnnotations[annotationEnableMattermost] == annotationEnableMattermostInform
}
func (c *Controller) findPodReplicaSet(pod *v1.Pod) (*appsv1.ReplicaSet, error) {
for _, ref := range pod.GetOwnerReferences() {
if ref.Kind == "ReplicaSet" {
rs, err := c.clientset.AppsV1().ReplicaSets(pod.Namespace).Get(context.Background(), ref.Name, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("failed to retrieve owner ref: %w", err)
}
return rs, nil
}
}
return nil, fmt.Errorf("replicaset not found")
}
func (c *Controller) findPodDeployment(pod *v1.Pod, rs *appsv1.ReplicaSet) (*appsv1.Deployment, error) {
for _, ref := range rs.GetOwnerReferences() {
if ref.Kind == "Deployment" {
dep, err := c.clientset.AppsV1().Deployments(pod.Namespace).Get(context.Background(), ref.Name, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("failed to retrieve owner ref: %w", err)
}
return dep, nil
}
}
return nil, fmt.Errorf("deployment not found")
}
const (
annotationMattermostBackoff = "espe.tech/informer-backoff"
annotationMattermostBackoffDefault = time.Minute * 10
)
func (c *Controller) refreshBackoff(pod *v1.Pod, container *v1.ContainerStatus) bool {
backoff := annotationMattermostBackoffDefault
if backoffVal := pod.GetObjectMeta().GetAnnotations()[annotationMattermostBackoff]; backoffVal != "" {
seconds, err := strconv.Atoi(backoffVal)
if err != nil {
klog.Warningf("Pod %s has invalid backoff interval: '%s'", pod.GetName(), backoffVal)
return false
}
backoff = time.Duration(seconds) * time.Second
}
if time.Since(c.timeouts[pod.GetName()]) < backoff {
return false
}
c.timeouts[pod.GetName()] = time.Now()
return true
}
func (c *Controller) clearTimeout(pod *v1.Pod) {
delete(c.timeouts, pod.GetName())
}
func (c *Controller) sendCrashNotification(pod *v1.Pod, container *v1.ContainerStatus) {
logs, _ := c.clientset.
CoreV1().Pods(pod.Namespace).
GetLogs(pod.Name, &v1.PodLogOptions{Container: container.Name}).Do(context.Background()).Raw()
message := fmt.Sprintf("Container %s of pod %s keeps crashing, maybe its time to intervene.", container.Name, pod.Name)
note := &chat.CrashNotification{
Title: "Crash loop detected!",
Message: message,
Logs: string(logs),
}
if container.LastTerminationState.Terminated != nil {
note.Reason = container.LastTerminationState.Terminated.Reason
}
c.chat.Send(note)
}
func (c *Controller) handlePodUpdate(pod *v1.Pod) {
for _, container := range pod.Status.ContainerStatuses {
if !container.Ready && container.State.Waiting != nil && c.hasValidAnnotation(pod) {
switch container.State.Waiting.Reason {
case "CrashLoopBackOff":
if !c.refreshBackoff(pod, &container) {
continue
}
c.sendCrashNotification(pod, &container)
}
}
}
}
// syncToStdout is the business logic of the controller. In this controller it simply prints
// information about the pod to stdout. In case an error happened, it has to simply return the error.
// The retry logic should not be part of the business logic.
func (c *Controller) syncToStdout(key string) error {
obj, exists, err := c.cacheIndexer.GetByKey(key)
if err != nil {
klog.Errorf("Fetching object with key %s from store failed with %v", key, err)
return err
}
if !exists {
// Below we will warm up our cache with a Pod, so that we will see a delete for one pod
klog.Infof("Pod %s does not exist anymore\n", key)
// Clean up intervals if possible
if obj != nil {
c.clearTimeout(obj.(*v1.Pod))
}
} else {
klog.Infof("Received create/update/delete for Pod %s\n", key)
// Note that you also have to check the uid if you have a local controlled resource, which
// is dependent on the actual instance, to detect that a Pod was recreated with the same name
c.handlePodUpdate(obj.(*v1.Pod))
}
return nil
}
// handleErr checks if an error happened and makes sure we will retry later.
func (c *Controller) handleErr(err error, key interface{}) {
if err == nil {
// Forget about the #AddRateLimited history of the key on every successful synchronization.
// This ensures that future processing of updates for this key is not delayed because of
// an outdated error history.
c.queue.Forget(key)
return
}
// This controller retries 5 times if something goes wrong. After that, it stops trying.
if c.queue.NumRequeues(key) < 5 {
klog.Infof("Error syncing pod %v: %v", key, err)
// Re-enqueue the key rate limited. Based on the rate limiter on the
// queue and the re-enqueue history, the key will be processed later again.
c.queue.AddRateLimited(key)
return
}
c.queue.Forget(key)
// Report to an external entity that, even after several retries, we could not successfully process this key
runtime.HandleError(err)
klog.Infof("Dropping pod %q out of the queue: %v", key, err)
}
func (c *Controller) Run(threadiness int, stopCh chan struct{}) {
defer runtime.HandleCrash()
// Let the workers stop when we are done
defer c.queue.ShutDown()
klog.Info("Starting Pod controller")
go c.cacheController.Run(stopCh)
// Wait for all involved caches to be synced, before processing items from the queue is started
if !cache.WaitForCacheSync(stopCh, c.cacheController.HasSynced) {
runtime.HandleError(fmt.Errorf("Timed out waiting for caches to sync"))
return
}
for i := 0; i < threadiness; i++ {
go wait.Until(c.runWorker, time.Second, stopCh)
}
<-stopCh
klog.Info("Stopping Pod controller")
}
func (c *Controller) runWorker() {
for c.processNextItem() {
}
}
func Run() {
klog.InitFlags(nil)
chat, err := chat.NewClientFromEnv()
if err != nil {
klog.Fatal(err)
}
klog.Info("Chat client configuration successful")
clientset, err := client.InCluster()
if err != nil {
klog.Fatal(err)
}
namespace, err := utils.Namespace()
if err != nil {
klog.Fatal(err)
}
klog.Infof("Watching namespace %s", namespace)
// create the pod watcher
podListWatcher := cache.NewListWatchFromClient(clientset.CoreV1().RESTClient(), "pods", namespace, fields.Everything())
// create the workqueue
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
// Bind the workqueue to a cache with the help of an informer. This way we make sure that
// whenever the cache is updated, the pod key is added to the workqueue.
// Note that when we finally process the item from the workqueue, we might see a newer version
// of the Pod than the version which was responsible for triggering the update.
indexer, informer := cache.NewIndexerInformer(podListWatcher, &v1.Pod{}, 0, cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err == nil {
queue.Add(key)
}
},
UpdateFunc: func(old interface{}, new interface{}) {
key, err := cache.MetaNamespaceKeyFunc(new)
if err == nil {
queue.Add(key)
}
},
DeleteFunc: func(obj interface{}) {
// IndexerInformer uses a delta queue, therefore for deletes we have to use this
// key function.
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err == nil {
queue.Add(key)
}
},
}, cache.Indexers{})
controller := &Controller{
clientset: clientset,
chat: chat,
cacheIndexer: indexer,
cacheController: informer,
queue: queue,
timeouts: make(map[string]time.Time),
}
stop := make(chan struct{})
defer close(stop)
go controller.Run(1, stop)
// Wait forever
select {}
}