-
Notifications
You must be signed in to change notification settings - Fork 64
/
informer_cronjobs.go
55 lines (44 loc) · 1.76 KB
/
informer_cronjobs.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
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 createCronjobInformer(ctx context.Context, factory informers.SharedInformerFactory) (cache.SharedIndexInformer, error) {
logger := zerolog.Ctx(ctx)
logger.Debug().Msgf("Starting cronjob informer\n")
cronjobInformer := factory.Batch().V1().CronJobs().Informer()
var handler cache.ResourceEventHandlerFuncs
handler.AddFunc = func(obj interface{}) {
cronjob := obj.(*batchv1.CronJob)
logger.Debug().Msgf("ADD: CronJob Added to Store: %s\n", cronjob.GetName())
_, ok := cronsMetaData.getCronsMonitorData(cronjob.Name)
if ok {
logger.Debug().Msgf("cronJob %s already exists in the crons informer data struct...\n", cronjob.Name)
} else {
cronsMetaData.addCronsMonitorData(cronjob.Name, NewCronsMonitorData(cronjob.Name, cronjob.Spec.Schedule, cronjob.Spec.JobTemplate.Spec.Completions))
}
}
handler.DeleteFunc = func(obj interface{}) {
cronjob := obj.(*batchv1.CronJob)
logger.Debug().Msgf("DELETE: CronJob deleted from Store: %s\n", cronjob.GetName())
_, ok := cronsMetaData.getCronsMonitorData(cronjob.Name)
if ok {
cronsMetaData.deleteCronsMonitorData(cronjob.Name)
logger.Debug().Msgf("cronJob %s deleted from the crons informer data struct...\n", cronjob.Name)
} else {
logger.Debug().Msgf("cronJob %s not in the crons informer data struct...\n", cronjob.Name)
}
}
// Check if cronjob monitoring is enabled
if isTruthy(os.Getenv("SENTRY_K8S_MONITOR_CRONJOBS")) {
logger.Info().Msgf("Add cronjob informer handlers for cronjob monitoring")
cronjobInformer.AddEventHandler(handler)
} else {
logger.Info().Msgf("Cronjob monitoring is disabled")
}
return cronjobInformer, nil
}