# Handle server-side-apply conflicts safely

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

Run a plan:

<Command {...drift} />

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`: `3` live and `2` in 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

Fetch the resource with its managed fields:

<Command
  commands={[
    "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

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:

```nix
scope.mkResource {
  # ...
  ownership.cede = [ "spec.replicas" ];
}
```

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](/docs/how-to/adopt-existing-resources/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

Run a plain deploy:

<Command {...resolve} />

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:

<Command {...clean} />

## 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-conflicts` once, after review, to take
  the fields. See
  [Take ownership of kubectl-managed resources](/docs/how-to/adopt-existing-resources/take-ownership-of-kubectl-managed-resources/)
  and [Take ownership of Helm-managed resources](/docs/how-to/adopt-existing-resources/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.

:::note[Use it for a migration]
See [Take ownership of kubectl-managed resources](/docs/how-to/adopt-existing-resources/take-ownership-of-kubectl-managed-resources/)
for the full first-deployment checklist, including selector preservation and a
backup of the live object.
:::