Skip to content

Commit 6942978

Browse files
committed
Allow multiple notifiers to be registered
1 parent c6eeae8 commit 6942978

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

main.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"flag"
55
"fmt"
66
"github.com/adaptant-labs/k8s-node-label-monitor/notifiers"
7+
"github.com/go-logr/logr"
78
v1 "k8s.io/api/core/v1"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
910
"k8s.io/apimachinery/pkg/fields"
@@ -21,16 +22,16 @@ import (
2122
var (
2223
monitorName = "k8s-node-label-monitor"
2324
nodeLocal = false
24-
cronjob = ""
25+
cronjob = ""
2526
log = logf.Log.WithName(monitorName)
2627
nodeLabels = map[string]map[string]string{}
2728
)
2829

2930
type Controller struct {
30-
indexer cache.Indexer
31-
queue workqueue.RateLimitingInterface
32-
informer cache.Controller
33-
notifier notifiers.LabelNotifier
31+
indexer cache.Indexer
32+
queue workqueue.RateLimitingInterface
33+
informer cache.Controller
34+
notifiers []notifiers.LabelNotifier
3435
}
3536

3637
// Compare two label maps and determine which key/value pairs have been added, deleted, or updated.
@@ -65,12 +66,24 @@ func compareLabelMaps(oldMap map[string]string, newMap map[string]string) (added
6566

6667
func NewController(queue workqueue.RateLimitingInterface, indexer cache.Indexer, informer cache.Controller) *Controller {
6768
return &Controller{
68-
indexer: indexer,
69-
informer: informer,
70-
queue: queue,
69+
indexer: indexer,
70+
informer: informer,
71+
queue: queue,
72+
notifiers: make([]notifiers.LabelNotifier, 0),
7173
}
7274
}
7375

76+
func (c Controller) notify(log logr.Logger, notification notifiers.LabelUpdateNotification) error {
77+
for _, notifier := range c.notifiers {
78+
err := notifier.Notify(log, notification)
79+
if err != nil {
80+
return err
81+
}
82+
}
83+
84+
return nil
85+
}
86+
7487
// Calculate label changes across each node update
7588
func (c *Controller) labelUpdateHandler(key string) error {
7689
obj, exists, err := c.indexer.GetByKey(key)
@@ -95,7 +108,7 @@ func (c *Controller) labelUpdateHandler(key string) error {
95108
Deleted: deleted,
96109
}
97110

98-
err := c.notifier.Notify(log, notification)
111+
err := c.notify(log, notification)
99112
if err != nil {
100113
log.Error(err, "Failed to dispatch notification")
101114
return err
@@ -260,23 +273,26 @@ func main() {
260273

261274
controller := NewController(queue, indexer, informer)
262275

263-
// Set up the notifier for this controller
276+
// Set up the notifiers for this controller
277+
log.Info("Enabling Logging notifier")
278+
controller.notifiers = append(controller.notifiers, notifiers.LogNotifier{})
279+
264280
if len(endpoint) > 0 {
265-
var err error
266-
controller.notifier, err = notifiers.NewEndpointNotifier(log, endpoint)
281+
notifier, err := notifiers.NewEndpointNotifier(log, endpoint)
267282
if err != nil {
268283
log.Error(err, "failed to instantiate endpoint notifier")
269284
return
270285
}
271-
} else if len(cronjob) > 0 {
272-
var err error
273-
controller.notifier, err = notifiers.NewCronJobNotifier(clientset, cronjob)
286+
controller.notifiers = append(controller.notifiers, notifier)
287+
}
288+
289+
if len(cronjob) > 0 {
290+
notifier, err := notifiers.NewCronJobNotifier(log, clientset, cronjob)
274291
if err != nil {
275292
log.Error(err, "failed to instantiate cronjob notifier")
276293
return
277294
}
278-
} else {
279-
controller.notifier = notifiers.LogNotifier{}
295+
controller.notifiers = append(controller.notifiers, notifier)
280296
}
281297

282298
// Start the controller

0 commit comments

Comments
 (0)