Skip to content

Commit

Permalink
Fix reconciliation retry scheduler
Browse files Browse the repository at this point in the history
Log the reconciliation error instead of returning it, so that controller-runtime doesn't requeue immediately. Reconciliation failures should be scheduled at the specified retry interval.

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Jan 25, 2021
1 parent 62972be commit 8a03e79
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions controllers/kustomization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
r.recordReadiness(ctx, kustomization)
log.Info(msg)
// do not requeue immediately, when the source is created the watcher should trigger a reconciliation
return ctrl.Result{RequeueAfter: kustomization.Spec.Interval.Duration}, nil
return ctrl.Result{RequeueAfter: kustomization.GetRetryInterval()}, nil
} else {
// retry on transient errors
return ctrl.Result{Requeue: true}, err
Expand All @@ -173,7 +173,7 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
r.recordReadiness(ctx, kustomization)
log.Info(msg)
// do not requeue immediately, when the artifact is created the watcher should trigger a reconciliation
return ctrl.Result{RequeueAfter: kustomization.Spec.Interval.Duration}, nil
return ctrl.Result{RequeueAfter: kustomization.GetRetryInterval()}, nil
}

// check dependencies
Expand Down Expand Up @@ -215,36 +215,33 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques

// reconcile kustomization by applying the latest revision
reconciledKustomization, reconcileErr := r.reconcile(ctx, *kustomization.DeepCopy(), source)
if reconcileErr != nil {
// broadcast the error
r.event(ctx, reconciledKustomization, source.GetArtifact().Revision, events.EventSeverityError, reconcileErr.Error(), nil)
}

// update status
if err := r.patchStatus(ctx, req, reconciledKustomization.Status); err != nil {
log.Error(err, "unable to update status after reconciliation")
return ctrl.Result{Requeue: true}, err
}
r.recordReadiness(ctx, reconciledKustomization)

// requeue
// broadcast the result and requeue
if reconcileErr != nil {
// record the reconciliation error
r.recordReadiness(ctx, reconciledKustomization)
return ctrl.Result{RequeueAfter: kustomization.GetRetryInterval()}, reconcileErr
log.Error(reconcileErr, fmt.Sprintf("Reconciliation failed after %s, next try in %s",
time.Now().Sub(reconcileStart).String(),
kustomization.GetRetryInterval().String()),
"revision",
source.GetArtifact().Revision)
r.event(ctx, reconciledKustomization, source.GetArtifact().Revision, events.EventSeverityError,
reconcileErr.Error(), nil)
return ctrl.Result{RequeueAfter: kustomization.GetRetryInterval()}, nil
} else {
log.Info(fmt.Sprintf("Reconciliation finished in %s, next run in %s",
time.Now().Sub(reconcileStart).String(),
kustomization.Spec.Interval.Duration.String()),
"revision",
source.GetArtifact().Revision,
)
r.event(ctx, reconciledKustomization, source.GetArtifact().Revision, events.EventSeverityInfo,
"Update completed", map[string]string{"commit_status": "update"})
return ctrl.Result{RequeueAfter: kustomization.Spec.Interval.Duration}, nil
}

log.Info(fmt.Sprintf("Reconciliation finished in %s, next run in %s",
time.Now().Sub(reconcileStart).String(),
kustomization.Spec.Interval.Duration.String()),
"revision",
source.GetArtifact().Revision,
)

// record the reconciliation result
r.event(ctx, reconciledKustomization, source.GetArtifact().Revision, events.EventSeverityInfo,
"Update completed", map[string]string{"commit_status": "update"})
r.recordReadiness(ctx, reconciledKustomization)
return ctrl.Result{RequeueAfter: kustomization.Spec.Interval.Duration}, nil
}

func (r *KustomizationReconciler) reconcile(
Expand Down

0 comments on commit 8a03e79

Please sign in to comment.