forked from getsentry/sentry-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
informer_jobs.go
61 lines (48 loc) · 1.57 KB
/
informer_jobs.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
package main
import (
"context"
"os"
"github.com/rs/zerolog"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"
)
func createJobInformer(ctx context.Context, factory informers.SharedInformerFactory) (cache.SharedIndexInformer, error) {
logger := zerolog.Ctx(ctx)
logger.Debug().Msgf("starting job informer\n")
jobInformer := factory.Batch().V1().Jobs().Informer()
var handler cache.ResourceEventHandlerFuncs
handler.AddFunc = func(obj interface{}) {
job := obj.(*batchv1.Job)
logger.Debug().Msgf("ADD: Job Added to Store: %s\n", job.GetName())
err := runSentryCronsCheckin(ctx, job, EventHandlerAdd)
if err != nil {
return
}
}
handler.UpdateFunc = func(oldObj, newObj interface{}) {
oldJob := oldObj.(*batchv1.Job)
newJob := newObj.(*batchv1.Job)
if oldJob.ResourceVersion == newJob.ResourceVersion {
logger.Debug().Msgf("UPDATE: Event sync %s/%s\n", oldJob.GetNamespace(), oldJob.GetName())
} else {
runSentryCronsCheckin(ctx, newJob, EventHandlerUpdate)
}
}
handler.DeleteFunc = func(obj interface{}) {
job := obj.(*batchv1.Job)
logger.Debug().Msgf("DELETE: Job deleted from Store: %s\n", job.GetName())
err := runSentryCronsCheckin(ctx, job, EventHandlerDelete)
if err != nil {
return
}
}
// Check if cronjob monitoring is enabled
if isTruthy(os.Getenv("SENTRY_K8S_MONITOR_CRONJOBS")) {
logger.Info().Msgf("Add job informer handlers for cronjob monitoring")
jobInformer.AddEventHandler(handler)
} else {
logger.Info().Msgf("Cronjob monitoring is disabled")
}
return jobInformer, nil
}