forked from uswitch/heimdall
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.go
More file actions
372 lines (302 loc) · 12 KB
/
Copy pathcontroller.go
File metadata and controls
372 lines (302 loc) · 12 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"fmt"
"time"
log "github.com/Sirupsen/logrus"
apps "k8s.io/api/apps/v1"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
lister "k8s.io/client-go/listers/apps/v1"
extlisters "k8s.io/client-go/listers/extensions/v1beta1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
prominformers "github.com/coreos/prometheus-operator/pkg/client/informers/externalversions"
promlisters "github.com/coreos/prometheus-operator/pkg/client/listers/monitoring/v1"
promclientset "github.com/coreos/prometheus-operator/pkg/client/versioned"
"github.com/uswitch/heimdall/pkg/templates"
)
type Controller struct {
kubeclientset kubernetes.Interface
promclientset promclientset.Interface
templateManager *templates.PrometheusRuleTemplateManager
ingressLister extlisters.IngressLister
ingressSynced cache.InformerSynced
ingressWorkqueue workqueue.RateLimitingInterface
deploymentLister lister.DeploymentLister
deploymentSynced cache.InformerSynced
deploymentWorkqueue workqueue.RateLimitingInterface
promruleLister promlisters.PrometheusRuleLister
promruleSynced cache.InformerSynced
promruleWorkqueue workqueue.RateLimitingInterface
}
func enqueueTo(queue workqueue.RateLimitingInterface) func(interface{}) {
return func(obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
runtime.HandleError(err)
return
}
queue.AddRateLimited(key)
}
}
func NewController(
kubeclientset kubernetes.Interface,
promclientset promclientset.Interface,
kubeInformerFactory kubeinformers.SharedInformerFactory,
promInformerFactory prominformers.SharedInformerFactory,
templateManager *templates.PrometheusRuleTemplateManager) *Controller {
ingressInformer := kubeInformerFactory.Extensions().V1beta1().Ingresses()
deploymentInformer := kubeInformerFactory.Apps().V1().Deployments()
promruleInformer := promInformerFactory.Monitoring().V1().PrometheusRules()
controller := &Controller{
kubeclientset: kubeclientset,
promclientset: promclientset,
templateManager: templateManager,
ingressLister: ingressInformer.Lister(),
ingressSynced: ingressInformer.Informer().HasSynced,
ingressWorkqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "Ingresses"),
deploymentLister: deploymentInformer.Lister(),
deploymentSynced: deploymentInformer.Informer().HasSynced,
deploymentWorkqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "Deployments"),
promruleLister: promruleInformer.Lister(),
promruleSynced: promruleInformer.Informer().HasSynced,
promruleWorkqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "PrometheusRules"),
}
// Setup Ingress Informer
enqueueIngress := enqueueTo(controller.ingressWorkqueue)
ingressInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: enqueueIngress,
UpdateFunc: func(old, new interface{}) {
oldObj := old.(*extensionsv1beta1.Ingress)
newObj := new.(*extensionsv1beta1.Ingress)
if newObj.ResourceVersion != oldObj.ResourceVersion {
enqueueIngress(new)
}
},
DeleteFunc: enqueueIngress,
})
// Setup Deployment Informer
enqueueDeployment := enqueueTo(controller.deploymentWorkqueue)
deploymentInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: enqueueDeployment,
UpdateFunc: func(old, new interface{}) {
oldObj := old.(*apps.Deployment)
newObj := new.(*apps.Deployment)
if newObj.ResourceVersion != oldObj.ResourceVersion {
enqueueDeployment(new)
}
},
DeleteFunc: enqueueDeployment,
})
// Setup PrometheusRule Informer
enqueuePrometheusRule := enqueueTo(controller.promruleWorkqueue)
promruleInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: enqueuePrometheusRule,
UpdateFunc: func(old, new interface{}) {
oldObj := old.(*monitoringv1.PrometheusRule)
newObj := new.(*monitoringv1.PrometheusRule)
if newObj.ResourceVersion != oldObj.ResourceVersion {
enqueuePrometheusRule(new)
}
},
DeleteFunc: enqueuePrometheusRule,
})
return controller
}
// prometheusRulesByIngress
// - Accepts an prometheusRulesByIngress and returns all it's PrometheusRules
func (c *Controller) prometheusRulesByIngress(ingress *extensionsv1beta1.Ingress) ([]*monitoringv1.PrometheusRule, error) {
filteredPrometheusRules := []*monitoringv1.PrometheusRule{}
prometheusrules, err := c.promruleLister.List(labels.Everything())
for _, promrule := range prometheusrules {
ownerRefs := promrule.GetOwnerReferences()
for _, ownerRef := range ownerRefs {
if ownerRef.UID == ingress.GetUID() {
filteredPrometheusRules = append(filteredPrometheusRules, promrule)
break
}
}
}
return filteredPrometheusRules, err
}
// prometheusRulesByDeployment
// - Accepts an prometheusRulesByDeployment and returns all it's PrometheusRules
func (c *Controller) prometheusRulesByDeployment(deployment *apps.Deployment) ([]*monitoringv1.PrometheusRule, error) {
filteredPrometheusRules := []*monitoringv1.PrometheusRule{}
prometheusrules, err := c.promruleLister.List(labels.Everything())
for _, promrule := range prometheusrules {
ownerRefs := promrule.GetOwnerReferences()
for _, ownerRef := range ownerRefs {
if ownerRef.UID == deployment.GetUID() {
filteredPrometheusRules = append(filteredPrometheusRules, promrule)
break
}
}
}
return filteredPrometheusRules, err
}
func GetObjectMetaKey(meta metav1.Object) string {
return meta.GetNamespace() + meta.GetName()
}
func PrometheusRulesByKey(prometheusrules []*monitoringv1.PrometheusRule) map[string]*monitoringv1.PrometheusRule {
out := map[string]*monitoringv1.PrometheusRule{}
for _, promrule := range prometheusrules {
out[GetObjectMetaKey(promrule)] = promrule
}
return out
}
func (c *Controller) processIngress(namespace, name string) error {
ingress, err := c.ingressLister.Ingresses(namespace).Get(name)
if err != nil {
if errors.IsNotFound(err) {
runtime.HandleError(fmt.Errorf("Ingress '%s.%s' in work queue no longer exists", namespace, name))
return nil
}
return err
}
oldPrometheusRules, err := c.prometheusRulesByIngress(ingress)
if err != nil {
return err
}
newPrometheusRules, err := c.templateManager.CreateFromIngress(ingress)
if err != nil {
return err
}
return c.syncPrometheusRules(oldPrometheusRules, newPrometheusRules)
}
func (c *Controller) processDeployment(namespace, name string) error {
deployment, err := c.deploymentLister.Deployments(namespace).Get(name)
if err != nil {
if errors.IsNotFound(err) {
runtime.HandleError(fmt.Errorf("Deployment '%s.%s' in work queue no longer exists", namespace, name))
return nil
}
return err
}
oldPrometheusRules, err := c.prometheusRulesByDeployment(deployment)
if err != nil {
return err
}
// We have to look up the namespace to decide which Prometheus instance the Deployment should report to
deploymentNamespace, err := c.kubeclientset.CoreV1().Namespaces().Get(deployment.GetNamespace(), metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
runtime.HandleError(fmt.Errorf("We were unable to set the alert as the namespace '%s' for deployment '%s' doesn't have the prometheus label", namespace, deployment))
return nil
}
return err
}
deploymentNamespacePrometheus := deploymentNamespace.GetAnnotations()["prometheus"]
log.Printf("********* Chosen prometheus is going to be: %s", deploymentNamespacePrometheus)
newPrometheusRules, err := c.templateManager.CreateFromDeployment(deployment, deploymentNamespacePrometheus)
if err != nil {
return err
}
return c.syncPrometheusRules(oldPrometheusRules, newPrometheusRules)
}
func (c *Controller) syncPrometheusRules(oldPrometheusRules, newPrometheusRules []*monitoringv1.PrometheusRule) error {
oldPrometheusRulesByKey := PrometheusRulesByKey(oldPrometheusRules)
for _, newPrometheusRule := range newPrometheusRules {
if oldPrometheusRule, ok := oldPrometheusRulesByKey[GetObjectMetaKey(newPrometheusRule)]; ok {
newPrometheusRule.SetResourceVersion(oldPrometheusRule.GetResourceVersion())
if _, err := c.promclientset.MonitoringV1().PrometheusRules(newPrometheusRule.GetNamespace()).Update(newPrometheusRule); err != nil {
return err
}
} else {
if _, err := c.promclientset.MonitoringV1().PrometheusRules(newPrometheusRule.GetNamespace()).Create(newPrometheusRule); err != nil {
return err
}
}
}
newPrometheusRulesByKey := PrometheusRulesByKey(newPrometheusRules)
for _, oldPrometheusRule := range oldPrometheusRules {
if _, ok := newPrometheusRulesByKey[GetObjectMetaKey(oldPrometheusRule)]; !ok {
if err := c.promclientset.MonitoringV1().PrometheusRules(oldPrometheusRule.GetNamespace()).Delete(oldPrometheusRule.GetName(), nil); err != nil {
return err
}
}
}
return nil
}
func runner(workqueue workqueue.RateLimitingInterface, processFn func(string, string) error) func() {
return func() {
for {
obj, shutdown := workqueue.Get()
if shutdown {
return
}
// We wrap this block in a func so we can defer c.workqueue.Done.
err := func(obj interface{}) error {
// We call Done here so the workqueue knows we have finished
// processing this item. We also must remember to call Forget if we
// do not want this work item being re-queued. For example, we do
// not call Forget if a transient error occurs, instead the item is
// put back on the workqueue and attempted again after a back-off
// period.
defer workqueue.Done(obj)
var key string
var ok bool
// We expect strings to come off the workqueue. These are of the
// form namespace/name. We do this as the delayed nature of the
// workqueue means the items in the informer cache may actually be
// more up to date that when the item was initially put onto the
// workqueue.
if key, ok = obj.(string); !ok {
// As the item in the workqueue is actually invalid, we call
// Forget here else we'd go into a loop of attempting to
// process a work item that is invalid.
workqueue.Forget(obj)
runtime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj))
return nil
}
// Convert the namespace/name string into a distinct namespace and name
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
// Run the processFn, passing it the namespace/name string of the Foo resource to be synced.
if err := processFn(namespace, name); err != nil {
return fmt.Errorf("error syncing '%s': %s", key, err.Error())
}
// Finally, no error has occurred; we Forget this item so it does not
// get queued again until another change happens.
workqueue.Forget(obj)
log.Infof("Successfully synced '%s'", key)
return nil
}(obj)
if err != nil {
runtime.HandleError(err)
}
}
}
}
func (c *Controller) Run(stopCh <-chan struct{}) error {
defer runtime.HandleCrash()
defer c.ingressWorkqueue.ShutDown()
defer c.deploymentWorkqueue.ShutDown()
defer c.promruleWorkqueue.ShutDown()
// Start the informer factories to begin populating the informer caches
log.Info("Starting Heimdall")
// Wait for the caches to be synced before starting workers
log.Info("Waiting for informer caches to sync")
if ok := cache.WaitForCacheSync(stopCh, c.ingressSynced, c.promruleSynced); !ok {
return fmt.Errorf("failed to wait for caches to sync")
}
ingressRunner := runner(c.ingressWorkqueue, c.processIngress)
deploymentRunner := runner(c.deploymentWorkqueue, c.processDeployment)
log.Info("Starting workers")
go wait.Until(ingressRunner, time.Second, stopCh)
go wait.Until(deploymentRunner, time.Second, stopCh)
log.Info("Started workers")
<-stopCh
log.Info("Shutting down workers")
return nil
}