# `snapshot`

`kix snapshot` saves what Kix currently manages in a cluster to a directory.
The directory is the baseline of a later `kix diff --from`, so a build can be
compared with the cluster without cluster access.

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

## Synopsis

```text
kix snapshot <CLUSTER> --out <DIR> [--context <CONTEXT>] [--quiet]
```

| Argument or flag | Meaning |
| --- | --- |
| `<CLUSTER>` | Kix cluster whose resources to save: either its key in `flake.nix` or the name on its Activation records (`kix.run/cluster`). Kix checks the Kubernetes cluster for Activation records labeled `<CLUSTER>` first. If none match, it evaluates `flake.nix` to look up the cluster's deployed `name`. Fails if the cluster has no records while other Kix clusters do. |
| `--out <DIR>` | Directory to write. Required. Kix stages the complete snapshot beside this path before replacing an existing directory. Paths containing `..` are rejected. |
| `--context <CONTEXT>` | Kubeconfig context to read. Defaults to the current context. |
| `-q`, `--quiet` | Suppress the progress lines on stderr. |

When Activation records on the Kubernetes cluster already match `<CLUSTER>`,
Kix reads the cluster directly without evaluating `flake.nix`. `--no-cache` has
no effect, and `--flake` and `--override-input` apply only when Kix evaluates
`flake.nix` to look up the cluster's `name`.

## What is saved

The save lists every resource carrying the Kix managed-by label, keeps the
ones Kix applied for the named cluster, and writes each one as the fields Kix
applied. A resource belongs to the cluster when one of that cluster's Activation
records references it via revisions (or via the resource's `kix.run/activations`
annotation on clusters not yet upgraded to revisions). Resources of other
Kix clusters on the same Kubernetes cluster are left out, and the save prints
how many it skipped per cluster. A resource both clusters deploy is saved.
Kubernetes records which tool set each field through server-side apply, and
Kix reads that record to keep only its own fields. Server defaults, status,
and fields written by controllers or by `kubectl` are left out.

Included:

- every namespaced and cluster-scoped resource Kix applied for the cluster,
  including `PackageInstance` records and the cluster's `Activation` records;
- the Kix annotations that identify each resource's package, identity hash,
  and dependencies.

Excluded:

- `status` and anything a controller wrote;
- server-side defaults Kix never sent;
- the annotations `kix.run/activations` and `kix.run/applied-hash`, which
  earlier versions of Kix wrote after applying and which changed on every
  deploy without being a content change;
- resources that only carry the label because a controller copied it, such as
  Pods and EndpointSlices.

A resource Kix manages can be a `Secret`. `owned_view::kix_owned_manifests`
redacts every `data` and `stringData` value on `Secret` manifests to
`"(live value differs from the build, not shown)"`, while `ConfigMap` data and
literal values in other resources are written as applied.

## Directory layout

```text
<DIR>/
  README.md                       # timestamp, cluster, context, resource count
  <namespace>/<name>-<kind>.yaml  # one file per namespaced resource
  _cluster/<name>-<kind>.yaml     # one file per cluster-scoped resource
```

Kinds are lower-cased in file names, for example
`how-to-app/production-deployment.yaml`.

## Exit status

| Code | Meaning |
| --- | --- |
| `0` | Snapshot written |
| `1` | The cluster could not be reached, API discovery failed, a listing failed, or the named cluster has no Activation records while other Kix clusters do |

## Using a snapshot

Pass the directory to `kix diff`:

<Command commands={["kix diff <CLUSTER> --from <DIR>"]} />

That comparison runs without Kubernetes access. Save a fresh snapshot after
each deploy; a snapshot describes the cluster at the moment it was taken.

:::note[Reference]
See [`kix diff`](/docs/reference/cli/diff/) for the other baselines
and the output formats.
:::

:::tip[Use it in a task]
See [Use snapshots for offline or CI diffs](/docs/how-to/operate-a-cluster/use-snapshots-for-offline-or-ci-diffs/)
for the day-to-day workflow, or
[Save and diff snapshots for offline CI](/docs/how-to/policy-ci-and-compliance/save-and-diff-snapshots-for-offline-ci/)
to run the comparison in a pipeline without cluster credentials.
:::