Skip to content

Commit

Permalink
Eliminate Warning: Reconciler returned both a non-zero result and a n…
Browse files Browse the repository at this point in the history
…on-nil error (#222)
  • Loading branch information
tardieu authored Jul 30, 2024
1 parent 3f52f70 commit fffb22b
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions internal/controller/appwrapper/appwrapper_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (r *AppWrapperReconciler) Reconcile(ctx context.Context, req ctrl.Request)
now := time.Now()
deadline := whenDetected.Add(gracePeriod)
if now.Before(deadline) {
return ctrl.Result{RequeueAfter: deadline.Sub(now)}, r.Status().Update(ctx, aw)
return requeueAfter(deadline.Sub(now), r.Status().Update(ctx, aw))
} else {
r.Recorder.Eventf(aw, v1.EventTypeNormal, string(workloadv1beta2.Unhealthy), "FoundFailedPods: %v failed pods", podStatus.failed)
return r.resetOrFail(ctx, aw, podStatus.terminalFailure, 1)
Expand Down Expand Up @@ -332,7 +332,7 @@ func (r *AppWrapperReconciler) Reconcile(ctx context.Context, req ctrl.Request)
Reason: "SufficientPodsReady",
Message: fmt.Sprintf("%v pods running; %v pods succeeded", podStatus.running, podStatus.succeeded),
})
return ctrl.Result{RequeueAfter: time.Minute}, r.Status().Update(ctx, aw)
return requeueAfter(time.Minute, r.Status().Update(ctx, aw))
}

// Not ready yet; either continue to wait or giveup if the warmup period has expired
Expand All @@ -346,7 +346,7 @@ func (r *AppWrapperReconciler) Reconcile(ctx context.Context, req ctrl.Request)
graceDuration = r.admissionGraceDuration(ctx, aw)
}
if time.Now().Before(whenDeployed.Add(graceDuration)) {
return ctrl.Result{RequeueAfter: 5 * time.Second}, r.Status().Update(ctx, aw)
return requeueAfter(5*time.Second, r.Status().Update(ctx, aw))
} else {
meta.SetStatusCondition(&aw.Status.Conditions, metav1.Condition{
Type: string(workloadv1beta2.Unhealthy),
Expand Down Expand Up @@ -405,7 +405,7 @@ func (r *AppWrapperReconciler) Reconcile(ctx context.Context, req ctrl.Request)
now := time.Now()
deadline := whenReset.Add(pauseDuration)
if now.Before(deadline) {
return ctrl.Result{RequeueAfter: deadline.Sub(now)}, r.Status().Update(ctx, aw)
return requeueAfter(deadline.Sub(now), r.Status().Update(ctx, aw))
}

meta.SetStatusCondition(&aw.Status.Conditions, metav1.Condition{
Expand Down Expand Up @@ -435,7 +435,7 @@ func (r *AppWrapperReconciler) Reconcile(ctx context.Context, req ctrl.Request)
now := time.Now()
deadline := whenDelayed.Add(deletionDelay)
if now.Before(deadline) {
return ctrl.Result{RequeueAfter: deadline.Sub(now)}, r.Status().Update(ctx, aw)
return requeueAfter(deadline.Sub(now), r.Status().Update(ctx, aw))
}
}

Expand Down Expand Up @@ -469,7 +469,7 @@ func (r *AppWrapperReconciler) Reconcile(ctx context.Context, req ctrl.Request)
now := time.Now()
deadline := whenSucceeded.Add(deletionDelay)
if now.Before(deadline) {
return ctrl.Result{RequeueAfter: deadline.Sub(now)}, r.Status().Update(ctx, aw)
return requeueAfter(deadline.Sub(now), r.Status().Update(ctx, aw))
}

if !r.deleteComponents(ctx, aw) {
Expand Down Expand Up @@ -896,3 +896,12 @@ func (r *AppWrapperReconciler) SetupWithManager(mgr ctrl.Manager) error {
Named("AppWrapper").
Complete(r)
}

// requeueAfter requeues the request after the specified duration
func requeueAfter(duration time.Duration, err error) (ctrl.Result, error) {
if err != nil {
// eliminate "Warning: Reconciler returned both a non-zero result and a non-nil error."
return ctrl.Result{}, err
}
return ctrl.Result{RequeueAfter: duration}, nil
}

0 comments on commit fffb22b

Please sign in to comment.