Skip to content

Commit 64177cc

Browse files
committed
Send notifications too when emitting events
This adds the external event recorder (a.k.a., notifications client) to the reconciler, and expands the definition of `<reconciler>.event(...)` so that it will send a notification whenever an event is emitted. This is the conventional way of handling events amongst the GitOps Toolkit controllers. Signed-off-by: Michael Bridgen <[email protected]>
1 parent 31f1e62 commit 64177cc

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

controllers/imageupdateautomation_controller.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ const imagePolicyKey = ".spec.update.imagePolicy"
7171
// ImageUpdateAutomationReconciler reconciles a ImageUpdateAutomation object
7272
type ImageUpdateAutomationReconciler struct {
7373
client.Client
74-
Log logr.Logger
75-
Scheme *runtime.Scheme
76-
EventRecorder kuberecorder.EventRecorder
77-
MetricsRecorder *metrics.Recorder
74+
Log logr.Logger
75+
Scheme *runtime.Scheme
76+
EventRecorder kuberecorder.EventRecorder
77+
ExternalEventRecorder *events.Recorder
78+
MetricsRecorder *metrics.Recorder
7879
}
7980

8081
// +kubebuilder:rbac:groups=image.toolkit.fluxcd.io,resources=imageupdateautomations,verbs=get;list;watch;create;update;patch;delete
@@ -382,6 +383,24 @@ func (r *ImageUpdateAutomationReconciler) event(auto imagev1.ImageUpdateAutomati
382383
if r.EventRecorder != nil {
383384
r.EventRecorder.Event(&auto, "Normal", severity, msg)
384385
}
386+
if r.ExternalEventRecorder != nil {
387+
objRef, err := reference.GetReference(r.Scheme, &auto)
388+
if err != nil {
389+
r.Log.WithValues(
390+
"request",
391+
fmt.Sprintf("%s/%s", auto.GetNamespace(), auto.GetName()),
392+
).Error(err, "unable to send event")
393+
return
394+
}
395+
396+
if err := r.ExternalEventRecorder.Eventf(*objRef, nil, severity, severity, msg); err != nil {
397+
r.Log.WithValues(
398+
"request",
399+
fmt.Sprintf("%s/%s", auto.GetNamespace(), auto.GetName()),
400+
).Error(err, "unable to send event")
401+
return
402+
}
403+
}
385404
}
386405

387406
func (r *ImageUpdateAutomationReconciler) recordReadinessMetric(auto *imagev1.ImageUpdateAutomation) {

main.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
2828

2929
imagev1alpha1_reflect "github.com/fluxcd/image-reflector-controller/api/v1alpha1"
30+
"github.com/fluxcd/pkg/runtime/events"
3031
"github.com/fluxcd/pkg/runtime/logger"
3132
"github.com/fluxcd/pkg/runtime/metrics"
3233
"github.com/fluxcd/pkg/runtime/probes"
@@ -37,6 +38,8 @@ import (
3738
// +kubebuilder:scaffold:imports
3839
)
3940

41+
const controllerName = "image-automation-controller"
42+
4043
var (
4144
scheme = runtime.NewScheme()
4245
setupLog = ctrl.Log.WithName("setup")
@@ -54,6 +57,7 @@ func init() {
5457
func main() {
5558
var (
5659
metricsAddr string
60+
eventsAddr string
5761
healthAddr string
5862
enableLeaderElection bool
5963
logLevel string
@@ -62,6 +66,7 @@ func main() {
6266
)
6367

6468
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
69+
flag.StringVar(&eventsAddr, "events-addr", "", "The address of the events receiver.")
6570
flag.StringVar(&healthAddr, "health-addr", ":9440", "The address the health endpoint binds to.")
6671
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
6772
"Enable leader election for controller manager. "+
@@ -74,6 +79,16 @@ func main() {
7479

7580
ctrl.SetLogger(logger.NewLogger(logLevel, logJSON))
7681

82+
var eventRecorder *events.Recorder
83+
if eventsAddr != "" {
84+
if er, err := events.NewRecorder(eventsAddr, controllerName); err != nil {
85+
setupLog.Error(err, "unable to create event recorder")
86+
os.Exit(1)
87+
} else {
88+
eventRecorder = er
89+
}
90+
}
91+
7792
metricsRecorder := metrics.NewRecorder()
7893
ctrlmetrics.Registry.MustRegister(metricsRecorder.Collectors()...)
7994

@@ -99,11 +114,12 @@ func main() {
99114
probes.SetupChecks(mgr, setupLog)
100115

101116
if err = (&controllers.ImageUpdateAutomationReconciler{
102-
Client: mgr.GetClient(),
103-
Log: ctrl.Log.WithName("controllers").WithName("ImageUpdateAutomation"),
104-
Scheme: mgr.GetScheme(),
105-
EventRecorder: mgr.GetEventRecorderFor("image-automation-controller"),
106-
MetricsRecorder: metricsRecorder,
117+
Client: mgr.GetClient(),
118+
Log: ctrl.Log.WithName("controllers").WithName("ImageUpdateAutomation"),
119+
Scheme: mgr.GetScheme(),
120+
EventRecorder: mgr.GetEventRecorderFor(controllerName),
121+
ExternalEventRecorder: eventRecorder,
122+
MetricsRecorder: metricsRecorder,
107123
}).SetupWithManager(mgr); err != nil {
108124
setupLog.Error(err, "unable to create controller", "controller", "ImageUpdateAutomation")
109125
os.Exit(1)

0 commit comments

Comments
 (0)