Sub topic 11
Resource versions and conflict errors
This chapter assumes you know what a Deployment is, can read YAML, and understand the control loop from earlier chapters. You should also be able to read short Go snippets.
Why conflicts exist
Many writers update the same object at the same time. A user runs kubectl edit, the Deployment controller updates status, and an autoscaler changes spec.replicas. If each writer simply overwrote the object, the last write would silently erase the others.
Kubernetes avoids this with optimistic concurrency. Nothing is locked. Each writer reads the object, changes it, and sends it back. The API server accepts the write only if the object has not changed since the read.
What a resource version is
Every object carries metadata.resourceVersion. It is an opaque string that the API server takes from etcd's revision counter, and it changes on every write to the object. Treat it as a token. Do not parse it, do not do arithmetic on it, and compare two versions only for equality.
kubectl get deployment web -o jsonpath='{.metadata.resourceVersion}'Do not confuse it with metadata.generation. The generation increases only when the spec changes. The resource version changes on any write, including status updates and label changes.
How a conflict happens
When you send an update, the object includes the resource version you read. If it does not match the stored version, the API server rejects the write with HTTP 409 Conflict. The message looks like this: the object has been modified; please apply your changes to the latest version and try again.
Handling conflicts in a controller
A conflict is expected, not a bug. The data you read was stale, so the fix is to read again and redo the change. Two approaches work.
- Return the error from
Reconcile. The work queue retries with backoff, and the next pass starts from fresh state. This is the simplest option, and it fits the level-triggered model because reconcile recomputes everything from current state. - Retry only the write, using
retry.RetryOnConflictfrom client-go. It re-reads and reapplies the change in a short loop.
package controllers
import (
"context"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func scale(ctx context.Context, c client.Client, key client.ObjectKey, replicas int32) error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
var d appsv1.Deployment
if err := c.Get(ctx, key, &d); err != nil {
return err
}
d.Spec.Replicas = &replicas
return c.Update(ctx, &d)
})
}The Get sits inside the retried function. If you read once outside the loop, every retry would resend the same stale version and fail again.
Common mistakes
- Reusing a stale object across retries instead of re-reading it.
- Treating a conflict as a failure and alerting on it. A few conflicts per minute are normal.
- Reading from the informer cache and assuming it is current. The cache can lag, so a write based on it may conflict. That is safe: the server rejects the write, and the retry sees newer data.
- Dropping
resourceVersionfrom an update to "force" it through. An update without it overwrites unconditionally for some resources, which brings back lost updates.
Exercise
A controller reads a Deployment from its cache, changes spec.replicas, and calls Update. The API server returns 409 Conflict. What does this tell you, and what should the controller do?
Show answer
The object changed in the API server after the controller's copy was read, so the resource version it sent no longer matches. The controller should not force the write. It should return the error so the work queue retries with backoff, or re-read the object and reapply the change, for example with retry.RetryOnConflict. The next attempt starts from newer data.