# `diff`

`kix diff` evaluates and builds a cluster, or reads the build from a bundle
with `--bundle`, then compares the result with a baseline: a directory
passed with `--from`, or a bundle passed with `--from-bundle`. With
`--bundle` and no baseline, it compares the bundle with nothing, so every
package is added. It never contacts a cluster, so it needs no cluster access
at all. `kix diff <CLUSTER>` with none of these flags is refused.

To compare a build with the live cluster, use
[`kix plan --read-only`](/docs/reference/cli/plan/#read-only-plans), which
reads the cluster with `get` and `list`. To preview a deploy you are about to
run, use [`kix plan`](/docs/reference/cli/plan/), which sends each write as a
server-side dry run.

This page is hand-maintained. Check `cli/kix-cli/src/cli.rs` and
`cli/kix-cli/src/commands/diff.rs` when in doubt.

## Synopsis

```text
kix diff <CLUSTER> --from <PATH> [--bundle <FILE>]
         [-o text|json|yaml|markdown]
         [--detail plain|fields|manifests] [--out <FILE>]
         [--flake <FLAKE>]
kix diff <CLUSTER> --bundle <FILE> [--from-bundle <FILE>] [...]
```

| Argument or flag | Meaning |
| --- | --- |
| `<CLUSTER>` | Cluster name to evaluate and build from the flake. This is the new side of the comparison. |
| `--bundle <FILE>` | Read the new side from a bundle written by `kix bundle` instead of building from the flake. |
| `--from <PATH>` | Use a local build or manifest directory as the baseline. See [Baseline](#baseline). |
| `--from-bundle <FILE>` | Use a bundle as the baseline. Needs `--bundle`. Not with `--from`. |
| `-o`, `--output` | `text` (default), `json`, `yaml` (the JSON document as YAML), or `markdown` for pull request comments. |
| `--detail <LEVEL>` | How much `json` and `yaml` say about each change: `plain` (default) names each changed resource and counts its changed fields, `fields` also lists each field change, and `manifests` also includes the old and new manifest of each changed, added, or removed resource. Text and Markdown are the same at every level. |
| `--out <FILE>` | Also write the comparison to `FILE` as a [plan file](/docs/reference/cli/plan/#plan-file), whatever `--detail` and `--output` say. It is for reading, by people and Kix Enterprise; `kix deploy --plan` takes only a `kix plan` plan file. |
| `--flake <FLAKE>` | Flake to evaluate. Defaults to the current directory. |

## Baseline

| Baseline | How to select it |
| --- | --- |
| Bundled build | `--from-bundle <FILE>`, with `--bundle` for the new side |
| Nothing (every package is added) | `--bundle <FILE>` alone |
| Saved manifests | `--from <DIR>` where `<DIR>` holds YAML written by `kix snapshot` or `kix export --with-kix-metadata` |
| Previous build | `--from <PATH>` where `<PATH>` is the activation index path (`kix.run/built-via`) of a build in Kix's local store, or a directory containing `kix-packages.json` whose referenced store paths exist in Kix's local store |

Each bundle is checked against its content as it is read, and must be a build
of `<CLUSTER>`, as for `kix plan --bundle`. A bundled baseline is a build, so
the report is the one `--from <build>` gives.

A manifest directory must carry the Kix annotations that name each resource's
package and identity hash. The default `kix export` output strips them, so use
`--with-kix-metadata` when the baseline comes from an export.

Every baseline goes through the same comparison that
[`kix plan --read-only`](/docs/reference/cli/plan/#read-only-plans) runs
against the live cluster. The live cluster and a snapshot of it produce the
same report.

## What is compared

The report is organised by package. Each package lands in one group:

| Group | Meaning |
| --- | --- |
| added | The package is in the build but not in the baseline |
| removed | The package is in the baseline but not in the build |
| changed | The package exists on both sides and at least one resource differs |
| unchanged | Every resource in the package matches |

Within a changed package, each resource is reported as one of:

| Resource line | Meaning |
| --- | --- |
| `+` added or `-` removed | The resource exists on one side only |
| `~` modified | The content differs. Text output shows each differing path with a unified diff of the values. |
| `~` tracking annotation only | Only the identity hash changed, because a dependency of this resource changed. The content is the same, so pods do not restart. Text and Markdown output mark these "tracking annotation only"; JSON calls them `dep_affected_resources`. |

Two more sections follow the packages:

- **Cluster-level resources** groups resources that belong to no package, such
  as namespaces and custom resource definitions. Text output labels the
  group `cluster-level resources`; JSON uses the key `_cluster`.
- **Activation** shows the old and new activation record names when they
  differ. A build whose content matches the baseline keeps the same record
  name, so this line appears only alongside other changes.

When a stateful resource appears to have moved between packages or names,
the report adds a "Stateful workload replacements" section. It is informational;
Kix does not migrate data.

## Objects Kix did not apply

Kix sets the label `app.kubernetes.io/managed-by=kix` on every object it applies. Every command that reads the live cluster (`kix plan`, `kix plan --read-only`, `kix snapshot`, `kix status`, `kix gc`, and `kix deploy`), along with the Kix operator, lists only objects carrying that label.

| Command | Objects Kix applied that left the build | Objects Kix never applied (no Kix label) |
| --- | --- | --- |
| `kix diff` | Shown as removed (`-`) when present in the baseline (an earlier build, a bundle, a `kix snapshot` directory, or a `kix export --with-kix-metadata` directory) and absent from the new build. | Never contacts the cluster. A `kix snapshot` baseline uses the same label selector, so unlabelled objects never appear. |
| `kix plan` | Lists each orphan and its [prune fate](/docs/reference/cli/plan/#prune-fates), read from the live objects listed in the `Active` record's revisions, its kept-orphans revision (from deploys run without `--prune`), and any `Failed` or `Partial` record's revisions. | Never listed or compared, unless an object in the build has the same kind, namespace, and name (see below). |
| `kix plan --read-only` | Shown as removed (`-`) in the package report when the live object carries the Kix label and one of this cluster's deployment records references it (via revisions, or via `kix.run/activations` on clusters not yet upgraded to revisions). Prune fates are not checked. | Never listed or compared. |
| [`kix status`](/docs/reference/cli/status/) `--check presence` / `--check drift` | Never reported. Checks only the objects listed in the current deploy record, reporting missing or changed objects. | Never listed, compared, or counted. |
| [`kix gc`](/docs/reference/cli/gc/) | Deletes orphans found by comparing the deployed builds against a fresh build. | Never listed or deleted. |
| `kix gc --full-scan` | Scans every labelled object of the named cluster and deletes those whose `kix.run/identity-hash` is absent from the fresh build. | Never listed or deleted. |

Even when an object carries `app.kubernetes.io/managed-by=kix`, Kix separates what it wrote from objects created by other tools or controllers:

- **Managed objects** carry `kix.run/identity-hash` and a `managedFields` entry for a Kix field manager (`kix/<cluster>`, or `kix` from an earlier version of Kix).
- **Unstamped objects** carry the label and a `managedFields` entry for a Kix field manager, but no `kix.run/identity-hash` (excluding controller-owned child objects and Service `Endpoints`, which Kix ignores). Kix wrote them, but cannot match them to a build.
- **Copies** carry the label without a `managedFields` entry for a Kix field manager (excluding, for objects without `kix.run/identity-hash`, the ignored child objects and `Endpoints` above). Another controller or tool copied Kix's labels, and sometimes its annotations, onto a new object, such as a database operator's volume claim or an Event posted for a custom resource.

If Kubernetes serves the same object under multiple API groups (such as `Event` under `v1` and `events.k8s.io/v1`), Kix includes it once, preferring the API group Kix applied it with.

[`kix gc --full-scan`](/docs/reference/cli/gc/) lists unstamped objects, copies, and unattributed objects (objects with no matching Activation record on the cluster) and keeps them unless you include them with `--include` (for example, `--include unstamped,copies,unattributed`).

The one exception for unlabelled objects is a name collision: if an unlabelled live object has the same kind, namespace, and name as a resource in the build, `kix plan` and `kix deploy` target that object. `kix plan` reports field ownership conflicts and names each field's current manager, and `kix deploy` stops on those conflicts unless `--force-conflicts` is passed.

Kix has no command to report unlabelled objects in namespaces it manages. Objects without the Kix label, such as a Flux `HelmRelease` applied with `kubectl apply`, never appear in `kix diff`, `kix plan`, `kix status`, or `kix gc` output. To find objects lacking Kix's label, query `kubectl` with `app.kubernetes.io/managed-by!=kix` (in Kubernetes, `key!=value` also matches objects without the key). Query specific kinds rather than all built-in kinds, which also return controller-created Pods, ReplicaSets, and EndpointSlices:

<Command commands={["kubectl get helmreleases.helm.toolkit.fluxcd.io -A -l 'app.kubernetes.io/managed-by!=kix'"]} />

## Labels and drift

Against a previous build, every field change is labelled `intended`, and
drift is not checked. The labels, and the Drift section that
`kix plan --read-only` adds against the live cluster, are described under
[Field change labels](/docs/reference/cli/plan/#field-change-labels).

## Output formats

### Text

A summary line, one block per namespace with each package's status and
resource lines, the cluster-level group, the activation line, then either the
migration section or `No differences found.` A field change that puts back a
drifted field is tagged `(reverts drift)`, and one the deploy leaves alone is
tagged `(drift; deploy leaves this object alone)`. The Drift section follows
when any field drifted. When there are changes, the output ends with a note
that the comparison cannot see admission webhooks, field ownership conflicts,
or what the deploy refuses. Colour
follows `--no-color` and the `NO_COLOR` environment variable.

### JSON and YAML

The `Plan` document a [plan file](/docs/reference/cli/plan/#the-plan-document)
holds, with JSON Schema `cli/kix-cli/schema/plan.schema.json`. That page
describes the top-level keys, the checked-section shape, and the
`not_checked` reasons. For `kix diff`:

| Key | Value from `kix diff` |
| --- | --- |
| `made_by` | `live_read` with a snapshot or export directory; `builds` with `--from <build>` or `--from-bundle` |
| `build` | The new build, as for `kix plan` |
| `deployed` | The baseline's Activation record: the `--from` build's record, or the record in a `--from` snapshot. `null` when there is none |
| `flags` | `null`: a diff plans no deploy |
| `migrations` | Checked: the stateful migrations the comparison found |
| `drift` | `not_checked`: `deployed_build_unavailable` for a snapshot, `builds_only` with `--from <build>` |
| `conflicts`, `admission`, `prune` | `not_checked`, reason `read_only` for a snapshot, `builds_only` with `--from <build>` |
| `readiness` | `not_checked`, reason `not_previewable` |
| `not_checked` | Always empty |

`packages` holds the package-grouped changes:

| Key | Content |
| --- | --- |
| `packages.summary` | Counts of `added`, `removed`, `changed`, and `unchanged` packages |
| `packages.added`, `packages.removed` | Arrays of `{ key, version, resources, kept_resources, manifests }`. `resources` counts every resource in the package; `kept_resources` lists the resources that stay on the cluster: Namespaces, CustomResourceDefinitions, and objects another Kix cluster's current build contains (always empty on `added`). `manifests` is present at `--detail manifests`: the new manifest of each resource of an added package, or the old manifest of each resource a removed package deletes |
| `packages.changed` | Array of package changes: `key`, `old_version`, `new_version`, `added_resources`, `removed_resources`, `kept_resources`, `modified_resources`, `dep_affected_resources`, and at `--detail manifests` `manifests`. A resource that left the build and stays on the cluster is listed in `kept_resources`, not `removed_resources` |
| `packages.changed[].manifests` | Object keyed by resource, each `{ old, new }`, for every modified, added, and removed resource. A side is absent when the resource does not exist there |
| `packages.changed[].modified_resources` | Array of `{ resource, diff_count, changes }`. `changes` is present at `--detail fields` and `--detail manifests`. Each entry of `changes` is `{ path, change, old, new, labels }`; `change` is `added`, `removed`, or `changed`, `old` or `new` is absent when that side has no value, and `labels` is absent when the deployed build is not on this machine |
| `packages.cluster_level` | The same shape as one changed package, with key `_cluster`, or `null` |
| `packages.activation` | `{ old, new }` record names, or `null` when unchanged |
| `packages.unchanged` | Number of unchanged packages |

Stateful migrations are under the top-level `migrations` section, each
`{ slot, source, target, reason }`.

A Secret value reads `(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)`.

Resource identifiers have the form `Kind.group/name@namespace`, such as `Deployment.apps/web@shop`. Core `v1` kinds omit `.group` (`Service/web@shop`), and cluster-scoped objects omit `@namespace` (`Namespace/shop`). Package keys have
the form `namespace/name`.

### Markdown

GitHub-flavoured Markdown: a plan summary, a `<details>` block per changed
package, the cluster-level and activation sections, and the migration section
when present.

## Exit status

| Code | Meaning |
| --- | --- |
| `0` | No differences |
| `2` | At least one difference, or a stateful migration was detected |
| `1` | Evaluation or build error |

The status is the same for every output format, so a CI job can write
Markdown to a file and still branch on the result.

:::note[Reference]
See [`kix snapshot`](/docs/reference/cli/snapshot/) for how a snapshot
directory is produced, and [`kix export`](/docs/reference/cli/export/) for
`--with-kix-metadata`.
:::

:::tip[Use it in a task]
See [Preview changes with `kix plan`](/docs/how-to/operate-a-cluster/preview-changes-with-kix-plan/)
for the live workflow and
[Use snapshots for offline or CI diffs](/docs/how-to/operate-a-cluster/use-snapshots-for-offline-or-ci-diffs/)
when the comparison must run without cluster access.
:::