# Declare readiness for custom resources

Use a readiness declaration when a custom resource's controller does not set a
standard `Ready` or `Available` condition. Declare the controller's convention
on the CRD or CRD reference, then create resources through its typed builder:

<Snippet {...customResourceReadiness} />

`path` is a JSON pointer into the live custom resource. `equals` is the string
value that indicates readiness. In this example, Kix waits until
`status.phase` is `Ready`.

A controller that reports a condition instead of a plain status field takes
the condition form:

```nix
readiness = {
  condition = {
    type = "Bootstrapped";
    status = "True";
  };
};
```

Kix then waits until `status.conditions` holds an entry with that `type` and
`status`. Use this form for operators such as StackGres (`Bootstrapped`) and
for Gateway API kinds (`Accepted` on a GatewayClass, `Programmed` on a
Gateway), which never set `Ready` or `Available`.

Keep this declaration in the package that owns or imports the CRD. Every custom
resource created through `widgetCrd.out.mkCR` then receives the same readiness
rule, along with its dependency on the CRD. A resource can replace the rule
with its own (see [Give one resource its own rule](#give-one-resource-its-own-rule)).

## Declare the states that mean failure

Some controllers report that they reject a resource: Cilium sets `Valid=False`
on a network policy whose rules it cannot apply. Such a resource never becomes
ready until its spec changes, so waiting for it only delays the failure. List
those states in `failed` beside the ready rule, and `kix deploy` stops waiting
as soon as the controller reports one:

```nix
readiness = {
  condition = {
    type = "Valid";
    status = "True";
  };
  failed = [
    {
      condition = {
        type = "Valid";
        status = "False";
      };
    }
  ];
};
```

A `failed` entry is a condition (`type`, `status`, and an optional `reason`
that narrows the match) or a status field (`path` and `equals`, for a
controller that reports a phase such as `Failed`). Declare only states the
controller does not leave on its own. A `Ready=False` that the controller
retries, or a `Pending` while it waits for something else, is not one of
them: declaring it would fail deploys that would have succeeded.

A condition entry is read as the controller's verdict on the spec, and the
failure message says to change the object. A `path` entry is read as a
runtime outcome, such as a backup that failed or a storage claim that was
lost, unless it sets `rejected = true`. Give a runtime outcome a `hint` that
says what to do next, and name in `message` the status fields to quote (one
JSON pointer or a list):

```nix
failed = [
  {
    path = "/status/phase";
    equals = "FailedValidation";
    rejected = true;
    message = "/status/validationErrors";
  }
  {
    path = "/status/phase";
    equals = "Failed";
    message = "/status/failureReason";
    hint = "Velero does not run a failed backup again. Fix the cause shown in its status, then create the backup again under a new name.";
  }
];
```

Kix trusts such a state once it knows the controller has seen the current
spec. It checks, in this order: the condition's `observedGeneration` (or
the field that [`observedGeneration`](#name-the-field-that-reports-the-generation)
names) has reached `metadata.generation`; or, when the
controller reports no generation, the condition's `lastTransitionTime` is
later than the moment this deploy applied the object (a state that changed
before the apply is the verdict on the previous spec, and Kix keeps waiting
for a new one); or, when the status carries neither field (a phase such as
`status.phase = "Failed"`), the state has held for ten seconds of polling. An
object the deploy left unchanged needs no such check. One case falls through
these checks: an object edited from one invalid spec to another may keep its
old transition time, because the condition did not change. Kix then waits
until the readiness timeout, and the timeout message still shows the current
reason. The failure message quotes the controller's condition, reason, and
message, or the status fields the entry names, and ends with what to do next.

## Name the field that reports the generation

Right after `kix deploy` changes a custom resource, its status still describes
the previous spec until the controller acts on the change. Kix tells the two
apart by comparing `metadata.generation` with the generation the controller
reports it has acted on, and waits until that has caught up. The Kubernetes API
conventions name that field `status.observedGeneration`, and Kix reads it by
default.

Some controllers report it in another field, or give
`status.observedGeneration` another meaning. Check the field's description in
the CRD and the controller code that sets it. When it is not the generation the
controller has acted on, name the right field in `observedGeneration` beside
the ready rule. The example declares a `Machine` kind that stands in for such
an operator kind:

<Snippet
  {...customResourceGenerationField}
  elide={{ customResourcePendingRestart: '            # pendingRestart: see the next section' }}
/>

KubeVirt is such a controller. On a VirtualMachine, `status.observedGeneration`
is the generation the running guest started from, so it stays behind after any
edit until the guest restarts, and `status.desiredGeneration` is the generation
the controller has processed.

Kix waits while a declared field is absent, because the controller has not
acted on the spec yet. The deploy then says, for example, `no
status.desiredGeneration yet`. With the default field, an absent
`status.observedGeneration` imposes no wait, because many controllers never
write it.

Set `observedGeneration = false` when the kind reports no such generation. Kix
then reads the status as current. A condition's own `observedGeneration`, when
the controller sets one, still comes first. The `failed` and `pendingRestart`
entries use the same field.

## Report a restart the workload needs

Some controllers accept a change to the spec but keep the running workload on
the previous spec until someone restarts it. KubeVirt does this for a change
that it cannot apply to a running guest, and sets `RestartRequired=True`. Name
that state in `pendingRestart` beside the ready rule, with a `hint` that says
how to restart:

<Snippet {...customResourceGenerationField} />

`pendingRestart` takes the two shapes of a `failed` entry: a condition (`type`,
`status`, and an optional `reason`) or a status field (`path`, `equals`, and an
optional `message` that names the status fields to quote). Kix reads it only
once the resource is ready and its status describes the current spec. When the
kind reports no generation, a condition that last changed before this deploy
applied the resource describes the previous spec, and Kix does not report it.
When the state matches, the deploy reports the resource as ready with a restart
pending, quotes the controller's message and the hint, and goes on. Restarting the workload is left
to you, because it interrupts what runs in it.

## Give one resource its own rule

When the ready state depends on the resource's own spec, pass `readiness` to
`scope.mkCR`. It replaces the kind's rule for that resource, and Kix checks it
the same way:

<Snippet {...customResourceOwnReadiness} />

A stopped KubeVirt VirtualMachine behaves the same way: it has no running
guest, so its `status.observedGeneration` stops advancing, and Kix would wait on
it after every edit. `observedGeneration = false` is the safe declaration for a
stopped VM.

## Check the generated custom resource

Build the cluster as JSON and inspect the custom resource. The output excerpt
shows the readiness annotation Kix generated from the declaration:

<Command {...widget} />

Run the normal pre-deploy checks as well:

<Command commands={["kix check how-to-composition"]} cwd="kix-examples/" />

During deployment, Kix reads the live object at the declared path and waits for
the expected value. Make the path match the status field written by the
controller, including its exact capitalization.

:::note[Explanation]
See [The DAG-driven deployment scheduler](/docs/explanation/the-dependency-graph/#dag-driven-deployment-scheduler)
for when resource readiness is sufficient and when to add an end-to-end health
check.
:::