/
pod_alerts.go
129 lines (116 loc) · 3.14 KB
/
pod_alerts.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
package operator
import (
"reflect"
"strings"
"github.com/appscode/go/log"
api "github.com/appscode/searchlight/apis/monitoring/v1alpha1"
"github.com/golang/glog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/tools/cache"
"kmodules.xyz/client-go/tools/queue"
)
func (op *Operator) initPodAlertWatcher() {
op.paInformer = op.monInformerFactory.Monitoring().V1alpha1().PodAlerts().Informer()
op.paQueue = queue.New("PodAlert", op.MaxNumRequeues, op.NumThreads, op.reconcilePodAlert)
op.paInformer.AddEventHandler(&cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
alert := obj.(*api.PodAlert)
if op.isValid(alert) {
queue.Enqueue(op.paQueue.GetQueue(), obj)
}
},
UpdateFunc: func(oldObj interface{}, newObj interface{}) {
old := oldObj.(*api.PodAlert)
nu := newObj.(*api.PodAlert)
if reflect.DeepEqual(old.Spec, nu.Spec) {
return
}
if op.isValid(nu) {
queue.Enqueue(op.paQueue.GetQueue(), nu)
}
},
DeleteFunc: func(obj interface{}) {
queue.Enqueue(op.paQueue.GetQueue(), obj)
},
})
op.paLister = op.monInformerFactory.Monitoring().V1alpha1().PodAlerts().Lister()
}
func (op *Operator) reconcilePodAlert(key string) error {
obj, exists, err := op.paInformer.GetIndexer().GetByKey(key)
if err != nil {
glog.Errorf("Fetching object with key %s from store failed with %v", key, err)
return err
}
if !exists {
log.Warningf("PodAlert %s does not exist anymore\n", key)
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
return op.ensurePodAlertDeleted(namespace, name)
}
alert := obj.(*api.PodAlert).DeepCopy()
log.Infof("Sync/Add/Update for PodAlert %s\n", alert.GetName())
op.ensurePodAlert(alert)
op.ensurePodAlertDeleted(alert.Namespace, alert.Name)
return nil
}
func (op *Operator) ensurePodAlert(alert *api.PodAlert) error {
if alert.Spec.PodName != nil {
pod, err := op.podLister.Pods(alert.Namespace).Get(*alert.Spec.PodName)
if err != nil {
return err
}
key, err := cache.MetaNamespaceKeyFunc(pod)
if err == nil {
op.podQueue.GetQueue().Add(key)
}
return nil
}
sel, err := metav1.LabelSelectorAsSelector(alert.Spec.Selector)
if err != nil {
return err
}
pods, err := op.podLister.Pods(alert.Namespace).List(sel)
if err != nil {
return err
}
for i := range pods {
pod := pods[i]
key, err := cache.MetaNamespaceKeyFunc(pod)
if err == nil {
op.podQueue.GetQueue().Add(key)
}
}
return nil
}
func alertAppliedToPod(a map[string]string, key string) bool {
if a == nil {
return false
}
if val, ok := a[api.AnnotationKeyAlerts]; ok {
names := strings.Split(val, ",")
for _, name := range names {
if name == key {
return true
}
}
}
return false
}
func (op *Operator) ensurePodAlertDeleted(alertNamespace, alertName string) error {
pods, err := op.podLister.Pods(alertNamespace).List(labels.Everything())
if err != nil {
return err
}
for _, pod := range pods {
if alertAppliedToPod(pod.Annotations, alertName) {
key, err := cache.MetaNamespaceKeyFunc(pod)
if err == nil {
op.podQueue.GetQueue().Add(key)
}
}
}
return nil
}