How-to guide / Adopt existing resources
Handle server-side-apply conflicts safely
These docs describe Kix's main branch, which can include changes not yet released. Read the docs for the latest release.
Kubernetes records which field manager owns each field of an object. When another manager changes a field Kix applied, it becomes that field’s owner. Kix reports this as drift before changing anything. Decide who should own the field, then either let Kix take ownership back or cede the field to the other manager.
The example below starts with a Kix-managed Deployment. A separate manager
named autoscaler then takes ownership of spec.replicas and changes it from
two to three.
See what changed without changing the cluster
Section titled “See what changed without changing the cluster”Run a plan:
❱ kix plan how-to-adoption-takeover
Building cluster 'how-to-adoption-takeover'...
Cluster how-to-adoption-takeover: 9 manifests
Connecting to context kind-kix-docs (https://127.0.0.1:42793)...
Current deployment: how-to-adoption-takeover-1wv1i1x4fclp (build 1wv1i1x4...)
takeover
~ legacy-web 1.0.0 (1 changed)
~ Deployment.apps/legacy-web@takeover
~ $.spec.replicas: (reverts drift)
old: 3
new: 2
Plan: 1 updated, 2 unchanged
Resources: 1 changed in content, 0 tracking annotation only (no spec change or restart)
Drift: 1 field(s) on the live cluster differ from the deployed build:
↺ Deployment.apps/legacy-web@takeover spec.replicas: 3 live, 2 in deployed build, set by autoscaler
this deploy overwrites the field and takes ownership back from autoscaler; if autoscaler should keep owning it, list the field in `ownership.cede`
Not checked by any preview: whether Pods start, pull their images, and pass their readiness probes.
Objects: 1 to write, 0 to delete (sha256:65d9604b54b703d78f404f3e055c1d70650324709f640f50d96dbcb188053a69)
↺ 1 object(s) changed on the cluster since Kix applied them; this deploy restores the build's values and takes back fields modified by other field managers
(exit code: 2) The plan labels the change (reverts drift) and lists it under Drift:. It
names three things:
- the resource,
Deployment.apps/legacy-web@takeover; - the other field manager,
autoscaler; - the field and its two values,
spec.replicas:3live and2in the deployed build.
The ↺ line under Objects: counts the objects that changed on the cluster
since Kix applied them. A plain kix deploy reapplies each of them and takes
the changed fields back from the other manager. You do not need --reconcile
or --force-conflicts for that.
Inspect the other manager
Section titled “Inspect the other manager”Fetch the resource with its managed fields:
❱ kubectl get deployment legacy-web -n takeover -o yaml --show-managed-fields Find the named manager under metadata.managedFields, then determine what owns
it. Common managers include an operator, an autoscaler, kubectl, Helm, and
another deployment system.
Also inspect the current field value and the Kix definition. Drift is not evidence that the other value is wrong.
Choose the field owner
Section titled “Choose the field owner”If the other controller should continue managing the field, change the Kix package or its configuration so it no longer emits that field. When the package cannot omit it, because the field is required on create or the whole object comes from upstream, declare the field as ceded on the resource:
1scope.mkResource {2 # ...3 ownership.cede = [ "spec.replicas" ];4}Kix stamps the paths as kix.run/cede-fields. A ceded field never counts as
drift, and Kix never takes it back, so the other manager keeps it. See
Cede fields to a controller.
Choose one manager either way, rather than letting both repeatedly take
ownership from each other.
If Kix should manage the field, first disable or reconfigure the other manager for this resource. Otherwise, it will overwrite the field again after the deploy, and the next plan will report the same drift.
Take the field back
Section titled “Take the field back”Run a plain deploy:
❱ kix deploy how-to-adoption-takeover -y
Building cluster 'how-to-adoption-takeover'...
Cluster how-to-adoption-takeover: 9 manifests
Connecting to context kind-kix-docs (https://127.0.0.1:42793)...
Current deployment: how-to-adoption-takeover-1wv1i1x4fclp (build 1wv1i1x4...)
takeover
~ legacy-web 1.0.0 (1 changed)
Plan: 1 updated, 2 unchanged
Resources: 1 changed in content, 0 tracking annotation only (no spec change or restart)
Drift: 1 field(s) on the live cluster differ from the deployed build:
↺ Deployment.apps/legacy-web@takeover spec.replicas: 3 live, 2 in deployed build, set by autoscaler
this deploy overwrites the field and takes ownership back from autoscaler; if autoscaler should keep owning it, list the field in `ownership.cede`
Not checked by any preview: whether Pods start, pull their images, and pass their readiness probes.
Objects: 1 to write, 0 to delete (sha256:65d9604b54b703d78f404f3e055c1d70650324709f640f50d96dbcb188053a69)
↺ 1 object(s) changed on the cluster since Kix applied them; this deploy restores the build's values and takes back fields modified by other field managers
plan: 9 nodes
~ Deployment.apps/legacy-web@takeover configured
✔ Deployment.apps/legacy-web@takeover ready
• activation 'how-to-adoption-takeover-1wv1i1x4fclp' → Active
Deploy complete: 0 created, 1 configured, 8 unchanged, 0 failed The deploy reapplies the drifted Deployment with force, transferring
spec.replicas back to the Kix cluster’s field manager and restoring the
replica count from your cluster definition. Unchanged resources are not touched.
Kix checks which fields the forced apply will take back during planning and
again right before writing (using an unforced dry run). Updates to status do
not block the apply. If the conflicting field, its owning manager, or its live
value changed after the plan was made, the deploy stops, names the object, and
tells you to run the command again.
Plan again to confirm that nothing is left to do:
❱ kix plan how-to-adoption-takeover
Building cluster 'how-to-adoption-takeover'...
Cluster how-to-adoption-takeover: 9 manifests
Connecting to context kind-kix-docs (https://127.0.0.1:42793)...
Current deployment: how-to-adoption-takeover-1wv1i1x4fclp (build 1wv1i1x4...)
Plan: 3 unchanged
Objects: 0 to write, 0 to delete (sha256:187cec1a8007b0b561a0732a533677801153428153dd70ecfebce19c0ab6c563) Where a conflict still stops the deploy
Section titled “Where a conflict still stops the deploy”Kix forces only the objects the plan labels as reverting drift. Other ownership disputes still stop the plan and the deploy with a field ownership conflict:
- An object Kix never applied. On a first deploy over a resource that
kubectl, Helm, or another tool created, those tools own the fields, and Kix has nothing to revert. The plan lists the conflict with the owning manager and fields. Pass--force-conflictsonce, after review, to take the fields. See Take ownership of kubectl-managed resources and Take ownership of Helm-managed resources. - A field applied by another Kix cluster. Kix never forces fields away from
another Kix cluster. When two Kix clusters set different values for the same
field, the deploy stops with
[K5636]. Update the cluster definitions to match.
--force-conflicts forces every apply in the run, so do not pass it on
routine deploys where it could hide a new ownership conflict. Even with
--force-conflicts, Kix leaves fields listed in ownership.cede with the
manager that owns them.