kubectl "Error from server (Conflict): Operation cannot be fulfilled" in CI - Fix it
Kubernetes uses optimistic concurrency: every update carries the resourceVersion it read. If another writer (a controller or a parallel job) changed the object first, your stale update is rejected with a Conflict and you must re-read and retry.
What this error means
A kubectl apply, patch, or replace fails with Error from server (Conflict): Operation cannot be fulfilled on deployments.apps "api": the object has been modified; please apply your changes to the latest version and try again.
Error from server (Conflict): Operation cannot be fulfilled on
deployments.apps "api": the object has been modified; please apply your changes
to the latest version and try againCommon causes
A concurrent write bumped the resourceVersion
A controller (HPA, operator) or a parallel pipeline updated the object between your read and write, invalidating your version.
A stale object from kubectl replace
kubectl replace with an old manifest carries an outdated resourceVersion and loses the race against a fresher state.
How to fix it
Re-read and retry, or use apply
- Re-run
kubectl applyso it merges against the current object. - Add a small retry around the update to absorb transient concurrent writes.
- Avoid
kubectl replacewith a hardcoded resourceVersion in CI.
for i in 1 2 3; do kubectl apply -f deploy.yaml && break; sleep 2; doneUse server-side apply to reduce conflicts
Server-side apply merges field ownership instead of replacing whole objects, which avoids many resourceVersion races.
kubectl apply --server-side -f deploy.yaml --field-manager=ciHow to prevent it
- Prefer
kubectl applyoverreplaceso updates merge against current state. - Add a short retry around updates that race controllers.
- Avoid two pipelines writing the same object at once.