Log and emit events on successful reconciliation

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
This commit is contained in:
Stefan Prodan 2022-11-10 10:20:48 +02:00 committed by Hidde Beydals
parent caec764ccc
commit 012faa2a08
3 changed files with 41 additions and 10 deletions

View File

@ -117,13 +117,23 @@ func (r *AlertReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resu
r.Metrics.RecordDuration(ctx, obj, reconcileStart)
r.Metrics.RecordSuspend(ctx, obj, obj.Spec.Suspend)
// Issue warning event if the reconciliation failed.
// Emit warning event if the reconciliation failed.
if retErr != nil {
r.Event(obj, corev1.EventTypeWarning, meta.FailedReason, retErr.Error())
}
// Log and emit success event.
if retErr == nil && conditions.IsReady(obj) {
msg := fmt.Sprintf("Reconciliation finished in %s",
time.Since(reconcileStart).String())
log.Info(msg)
r.Event(obj, corev1.EventTypeNormal, meta.SucceededReason, msg)
}
// Patch finalizers, status and conditions.
retErr = r.patch(ctx, obj, patcher)
if err := r.patch(ctx, obj, patcher); err != nil {
retErr = kerrors.NewAggregate([]error{retErr, err})
}
}()
if !controllerutil.ContainsFinalizer(obj, apiv1.NotificationFinalizer) {
@ -158,7 +168,6 @@ func (r *AlertReconciler) reconcile(ctx context.Context, alert *apiv1.Alert) (ct
}
conditions.MarkTrue(alert, meta.ReadyCondition, meta.SucceededReason, apiv1.InitializedReason)
ctrl.LoggerFrom(ctx).Info("Alert initialized")
return ctrl.Result{}, nil
}

View File

@ -99,13 +99,23 @@ func (r *ProviderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (r
r.Metrics.RecordDuration(ctx, obj, reconcileStart)
r.Metrics.RecordSuspend(ctx, obj, obj.Spec.Suspend)
// Issue warning event if the reconciliation failed.
// Emit warning event if the reconciliation failed.
if retErr != nil {
r.Event(obj, corev1.EventTypeWarning, meta.FailedReason, retErr.Error())
}
// Log and emit success event.
if retErr == nil && conditions.IsReady(obj) {
msg := fmt.Sprintf("Reconciliation finished in %s, next run in %s",
time.Since(reconcileStart).String(), obj.Spec.Interval.Duration.String())
log.Info(msg)
r.Event(obj, corev1.EventTypeNormal, meta.SucceededReason, msg)
}
// Patch finalizers, status and conditions.
retErr = r.patch(ctx, obj, patcher)
if err := r.patch(ctx, obj, patcher); err != nil {
retErr = kerrors.NewAggregate([]error{retErr, err})
}
}()
if !controllerutil.ContainsFinalizer(obj, apiv1.NotificationFinalizer) {
@ -140,7 +150,6 @@ func (r *ProviderReconciler) reconcile(ctx context.Context, obj *apiv1.Provider)
}
conditions.MarkTrue(obj, meta.ReadyCondition, meta.SucceededReason, apiv1.InitializedReason)
ctrl.LoggerFrom(ctx).Info("Provider initialized")
return ctrl.Result{RequeueAfter: obj.Spec.Interval.Duration}, nil
}

View File

@ -106,13 +106,23 @@ func (r *ReceiverReconciler) Reconcile(ctx context.Context, req ctrl.Request) (r
r.Metrics.RecordDuration(ctx, obj, reconcileStart)
r.Metrics.RecordSuspend(ctx, obj, obj.Spec.Suspend)
// Issue warning event if the reconciliation failed.
// Emit warning event if the reconciliation failed.
if retErr != nil {
r.Event(obj, corev1.EventTypeWarning, meta.FailedReason, retErr.Error())
}
// Log and emit success event.
if retErr == nil && conditions.IsReady(obj) {
msg := fmt.Sprintf("Reconciliation finished in %s, next run in %s",
time.Since(reconcileStart).String(), obj.Spec.Interval.Duration.String())
log.Info(msg)
r.Event(obj, corev1.EventTypeNormal, meta.SucceededReason, msg)
}
// Patch finalizers, status and conditions.
retErr = r.patch(ctx, obj, patcher)
if err := r.patch(ctx, obj, patcher); err != nil {
retErr = kerrors.NewAggregate([]error{retErr, err})
}
}()
if !controllerutil.ContainsFinalizer(obj, apiv1.NotificationFinalizer) {
@ -145,6 +155,7 @@ func (r *ReceiverReconciler) reconcile(ctx context.Context, obj *apiv1.Receiver)
token, err := r.token(ctx, obj)
if err != nil {
conditions.MarkFalse(obj, meta.ReadyCondition, apiv1.TokenNotFoundReason, err.Error())
obj.Status.URL = ""
return ctrl.Result{Requeue: true}, err
}
@ -153,9 +164,11 @@ func (r *ReceiverReconciler) reconcile(ctx context.Context, obj *apiv1.Receiver)
// Mark the resource as ready and set the URL
conditions.MarkTrue(obj, meta.ReadyCondition, meta.SucceededReason, msg)
obj.Status.URL = receiverURL
ctrl.LoggerFrom(ctx).Info(msg)
if obj.Status.URL != receiverURL {
obj.Status.URL = receiverURL
ctrl.LoggerFrom(ctx).Info(msg)
}
return ctrl.Result{RequeueAfter: obj.Spec.Interval.Duration}, nil
}