Sub topic 12
Errors, retries and requeueing with backoff
This chapter assumes you know the work queue and the shape of a Reconcile function from earlier chapters. It covers what a controller should return when something goes wrong, and what the framework does with that return value.
The return value is the retry policy
A reconciler does not retry in a loop of its own. It returns a result to the work queue, and the queue decides when to call it again.
| Return value | Meaning | What the queue does |
|---|---|---|
| `ctrl.Result{}, nil` | Converged, or nothing to do | Forgets the failure history for this key |
| `ctrl.Result{}, err` | Something failed | Requeues with exponential backoff |
| `ctrl.Result{RequeueAfter: d}, nil` | Healthy, but check again later | Requeues after `d` and resets the backoff |
The key being retried is a namespaced name, not a snapshot of the object. Each retry reads the latest state from the cache, so it is never acting on stale event data.
Backoff
In controller-runtime, the default rate limiter combines two limits. Each key gets its own exponential delay, starting at 5 ms and doubling up to a cap of about 1000 seconds. A shared token bucket also limits the whole queue to roughly 10 requeues per second. One broken object therefore backs off without delaying healthy ones. A successful return resets that object's delay.
Classifying errors
Treat each error class differently:
- Not found. The object was deleted before you read it. Return success. Retrying cannot help.
- Conflict. Another writer changed the object after you read it, so your write used an old
resourceVersion. Return the error. The retry reads fresh state and tries again. - Transient. Network failures, timeouts, or a dependency that is not ready yet. Return the error and let backoff absorb it.
- Permanent. An invalid spec will fail every time. Record it in status and wrap it with
reconcile.TerminalError, so the controller does not retry it.
package controllers
import (
"context"
apierrors "k8s.io/apimachinery/pkg/api/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
func handle(ctx context.Context, err error) (ctrl.Result, error) {
switch {
case err == nil:
return ctrl.Result{}, nil
case apierrors.IsNotFound(err):
return ctrl.Result{}, nil
case apierrors.IsInvalid(err):
return ctrl.Result{}, reconcile.TerminalError(err)
default:
// Conflicts and transient failures: retry with backoff.
return ctrl.Result{}, err
}
}Common mistakes
- Sleeping inside Reconcile. This blocks a worker. Return
RequeueAfterinstead. - Swallowing errors. Logging and returning
nilstops retries, so the object stays broken. - Retrying forever on bad input. Without a terminal error, an invalid object retries until the delay hits the cap, and it fills logs and metrics along the way.
- Using polling to hide missing watches. If you need
RequeueAfterto notice changes in objects you own, check that the controller watches them.
Exercise
A reconciler hits a Conflict error while updating status and returns ctrl.Result{}, nil after logging it. What goes wrong, and what should it return?
Show answer
Returning nil tells the queue the work succeeded, so the failed status update is never retried. The status stays stale until some other event triggers a reconcile. It should return the error. The retry then reads the latest object from the cache and applies the update again, with backoff.