# Preview changes with `kix plan`

`kix plan` previews a deploy in two ways, and neither changes the cluster:

- `kix plan` runs the deploy with every write sent as a server-side dry run.
  Use it to preview a deploy you are about to run. It needs the same
  permissions as the deploy.
- `kix plan --read-only` only reads. Use it where the job has read-only
  credentials, such as a pull request check.

For a pull request check with no cluster access at all, compare against a
snapshot with `kix diff`; see
[Use snapshots for offline or CI diffs](/docs/how-to/operate-a-cluster/use-snapshots-for-offline-or-ci-diffs/).

## Preview a deploy with `kix plan`

Run the plan with the flags you will deploy with:

<Command
  commands={["kix plan how-to-application --prune"]}
  cwd="kix-examples/"
/>

The output is the package-grouped plan `kix deploy` prints before it asks for
confirmation. Each field change is what the apiserver says the apply would
produce. After the packages, the plan lists anything the deploy would stop
on: a field another manager owns, a webhook or admission policy denial, a
validation error, an immutable field, or an RBAC refusal. Every refusal is
listed, so you can fix them all before deploying.

The plan exits `0` when the deploy would change and delete nothing, `2` when
it would change or delete something, and `3` when it would stop. In CI, treat
`0` and `2` as success and block on `3`, a deploy the cluster would refuse;
[`kix plan`](/docs/reference/cli/plan/#exit-status) has a script that does
this.

Objects whose CRD or namespace the same deploy creates cannot be checked by a
dry run. The plan lists them under "Not checked by the dry run" with the
changes from comparing builds. On a cluster's first deploy most objects are
in this list.

## Compare desired and live resources with `kix plan --read-only`

Start from a cluster that matches what is deployed. Kix compares only the
fields it applied, so an unchanged cluster reports nothing and exits 0:

<Command {...diffClean} />

Now change the production message in the cluster definition and run the same
command:

<Command {...diffChange} />

The report names the ConfigMap you edited and shows the changed value. The
resources marked "tracking annotation only" have no content change of their
own. Their identity hash moves because they depend on the ConfigMap, so Kix
writes them again on the next deploy to update the annotation. Their pods do
not restart.

## Read the report

The report is grouped by package, and each line tells you something different:

- A package marked added or removed appears on only one side.
- A resource marked `~` has a content change, shown as a diff of the fields
  that differ.
- A resource marked "tracking annotation only" has no content change of its
  own. Only its identity hash moved, because something it depends on changed.
- A cluster-level group collects resources that belong to no package, such as
  namespaces and custom resource definitions.
- A deployment record line names the deployment record this change would create.

Read the packages you edited first, then the dependent resources, which tell
you how far the rollout reaches. A non-empty report exits with status 2,
which lets CI tell a change from a clean comparison.

When the deployed build is on this machine, a field someone changed on the
cluster is tagged `(reverts drift)` when the deploy puts it back, and a
Drift section at the end names the field manager that changed it. When there
are changes, the last line reminds you that a read-only plan cannot see
admission webhooks, field ownership conflicts, or deploy refusals. Run
`kix plan` without `--read-only` for those.

## Select the flake and context

By default, Kix evaluates the current directory and reads the current kubectl
context. Set either input explicitly when needed:

<Command
  commands={[
    "kix plan how-to-application --read-only --flake ./infrastructure --context kind-kix-demo",
  ]}
/>

The flake supplies the desired manifests. The Kubernetes context supplies the
live manifests being compared.

## Produce review-friendly output

Select Markdown output when attaching the result to a pull request or change
review:

<Command expandable {...diffMarkdown} />

Redirect it into a file for the job to publish:

<Command
  commands={["kix plan how-to-application --read-only --output markdown > kix-plan.md"]}
  cwd="kix-examples/"
/>

Without `--read-only`, `kix plan --output markdown` produces the same layout
with the write-path checks added, for a job that has the deployer's
credentials.

Use JSON when another program will process the result:

<Command
  commands={["kix plan how-to-application --read-only --output json > kix-plan.json"]}
  cwd="kix-examples/"
/>

The document is the one a `kix plan --out` [plan file](/docs/reference/cli/plan/#plan-file)
holds, so one consumer can read either. The checks a read-only plan cannot make read
`{ "status": "not_checked", "reason": "read_only" }`. By default the JSON
names each changed resource and counts its changed fields. Add
`--detail fields` to list each field change, old to new:

<Command
  commands={["kix plan how-to-application --read-only --output json --detail fields > kix-plan.json"]}
  cwd="kix-examples/"
/>

```json
{ "path": "$.spec.replicas", "change": "changed", "old": 2, "new": 3, "labels": ["intended"] }
```

`change` is `added`, `removed`, or `changed`. `labels` is present when the
deployed build is on this machine. Add `--detail manifests` to also include,
under `manifests` on each package, the old and new manifest of every resource
that changes, is added, or is removed, for a tool that shows a line-by-line
diff. A plan file holds those manifests too, one file each; in a `kix plan`
plan file the two manifests of an object the cluster checked are the live
object and the cluster's dry-run result:

<Command
  commands={["kix plan how-to-application --read-only --output json --detail manifests > kix-plan.json"]}
  cwd="kix-examples/"
/>

At both levels, Secret values read `(secret, not shown)`, or
`(secret, changed, not shown)` on the new side when the value changes. Secret
hashes and `checksum/*` annotations read `(hash, not shown)` or
`(hash, changed, not shown)`. A
field value larger than 16 KiB, or a manifest larger than 3 MiB (the apiserver's limit on a request), reads
`(N bytes, not shown)`. A password written as a literal container `env`
value or into a ConfigMap is shown as it is, so keep secrets in Secrets.

## Confirm a clean result

After deploying the intended change, run `kix plan` again, with or without
`--read-only`. A clean result confirms that Kix does not currently plan
another content change.

`kix plan --read-only` compares the fields Kix applied. Use
[`kix status <cluster> --check drift`](/docs/how-to/operate-a-cluster/check-resource-status/#find-changed-objects)
when you need to know who else owns a field and what they changed.

:::note[Reference]
See [`kix plan`](/docs/reference/cli/plan/) for the full plan document, labels,
and exit codes.
:::

:::note[Related guides]
See [Generate PR-review Markdown from `kix plan`](/docs/how-to/policy-ci-and-compliance/generate-pr-review-markdown-from-kix-plan/)
for CI usage and [Reconcile drift](/docs/how-to/operate-a-cluster/reconcile-drift/)
when live resources need to be reapplied.
:::