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 <michael@weave.works>
This commit is contained in:
Michael Bridgen 2020-12-01 11:34:34 +00:00
parent 31f1e62cd4
commit 64177cc6f5
2 changed files with 44 additions and 9 deletions

View File

@ -71,10 +71,11 @@ const imagePolicyKey = ".spec.update.imagePolicy"
// ImageUpdateAutomationReconciler reconciles a ImageUpdateAutomation object
type ImageUpdateAutomationReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
EventRecorder kuberecorder.EventRecorder
MetricsRecorder *metrics.Recorder
Log logr.Logger
Scheme *runtime.Scheme
EventRecorder kuberecorder.EventRecorder
ExternalEventRecorder *events.Recorder
MetricsRecorder *metrics.Recorder
}
// +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
if r.EventRecorder != nil {
r.EventRecorder.Event(&auto, "Normal", severity, msg)
}
if r.ExternalEventRecorder != nil {
objRef, err := reference.GetReference(r.Scheme, &auto)
if err != nil {
r.Log.WithValues(
"request",
fmt.Sprintf("%s/%s", auto.GetNamespace(), auto.GetName()),
).Error(err, "unable to send event")
return
}
if err := r.ExternalEventRecorder.Eventf(*objRef, nil, severity, severity, msg); err != nil {
r.Log.WithValues(
"request",
fmt.Sprintf("%s/%s", auto.GetNamespace(), auto.GetName()),
).Error(err, "unable to send event")
return
}
}
}
func (r *ImageUpdateAutomationReconciler) recordReadinessMetric(auto *imagev1.ImageUpdateAutomation) {

26
main.go
View File

@ -27,6 +27,7 @@ import (
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
imagev1alpha1_reflect "github.com/fluxcd/image-reflector-controller/api/v1alpha1"
"github.com/fluxcd/pkg/runtime/events"
"github.com/fluxcd/pkg/runtime/logger"
"github.com/fluxcd/pkg/runtime/metrics"
"github.com/fluxcd/pkg/runtime/probes"
@ -37,6 +38,8 @@ import (
// +kubebuilder:scaffold:imports
)
const controllerName = "image-automation-controller"
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
@ -54,6 +57,7 @@ func init() {
func main() {
var (
metricsAddr string
eventsAddr string
healthAddr string
enableLeaderElection bool
logLevel string
@ -62,6 +66,7 @@ func main() {
)
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&eventsAddr, "events-addr", "", "The address of the events receiver.")
flag.StringVar(&healthAddr, "health-addr", ":9440", "The address the health endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. "+
@ -74,6 +79,16 @@ func main() {
ctrl.SetLogger(logger.NewLogger(logLevel, logJSON))
var eventRecorder *events.Recorder
if eventsAddr != "" {
if er, err := events.NewRecorder(eventsAddr, controllerName); err != nil {
setupLog.Error(err, "unable to create event recorder")
os.Exit(1)
} else {
eventRecorder = er
}
}
metricsRecorder := metrics.NewRecorder()
ctrlmetrics.Registry.MustRegister(metricsRecorder.Collectors()...)
@ -99,11 +114,12 @@ func main() {
probes.SetupChecks(mgr, setupLog)
if err = (&controllers.ImageUpdateAutomationReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("ImageUpdateAutomation"),
Scheme: mgr.GetScheme(),
EventRecorder: mgr.GetEventRecorderFor("image-automation-controller"),
MetricsRecorder: metricsRecorder,
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("ImageUpdateAutomation"),
Scheme: mgr.GetScheme(),
EventRecorder: mgr.GetEventRecorderFor(controllerName),
ExternalEventRecorder: eventRecorder,
MetricsRecorder: metricsRecorder,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ImageUpdateAutomation")
os.Exit(1)