Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate Warning from controller runtime #222

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}