forked from getsentry/sentry-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
informers.go
79 lines (69 loc) · 2.21 KB
/
informers.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
package main
import (
"context"
"errors"
"time"
"k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"
)
var cronjobInformer cache.SharedIndexInformer
var jobInformer cache.SharedIndexInformer
var replicasetInformer cache.SharedIndexInformer
var deploymentInformer cache.SharedIndexInformer
// Starts all informers (jobs, cronjobs, replicasets, deployments)
// if we opt into cronjob, attach the job/cronjob event handlers
// and add to the crons monitor data struct for Sentry Crons
func startInformers(ctx context.Context, namespace string) error {
clientset, err := getClientsetFromContext(ctx)
if err != nil {
return errors.New("failed to get clientset")
}
// Create factory that will produce both the cronjob informer and job informer
factory := informers.NewSharedInformerFactoryWithOptions(
clientset,
5*time.Second,
informers.WithNamespace(namespace),
)
// Create the job informer
jobInformer, err = createJobInformer(ctx, factory)
if err != nil {
return err
}
// Create the cronjob informer
cronjobInformer, err = createCronjobInformer(ctx, factory)
if err != nil {
return err
}
// Create the replicaset informer
replicasetInformer, err = createReplicasetInformer(ctx, factory)
if err != nil {
return err
}
// Create the deployment informer
deploymentInformer, err = createDeploymentInformer(ctx, factory)
if err != nil {
return err
}
// Channel to tell the factory to stop the informers
doneChan := make(chan struct{})
factory.Start(doneChan)
// Sync the cronjob informer cache
if ok := cache.WaitForCacheSync(doneChan, cronjobInformer.HasSynced); !ok {
return errors.New("cronjob informer failed to sync")
}
// Sync the job informer cache
if ok := cache.WaitForCacheSync(doneChan, jobInformer.HasSynced); !ok {
return errors.New("job informer failed to sync")
}
// Sync the replicaset informer cache
if ok := cache.WaitForCacheSync(doneChan, replicasetInformer.HasSynced); !ok {
return errors.New("replicaset informer failed to sync")
}
// Sync the deployment informer cache
if ok := cache.WaitForCacheSync(doneChan, deploymentInformer.HasSynced); !ok {
return errors.New("deployment informer failed to sync")
}
// Wait for the channel to be closed
<-doneChan
return nil
}