Q&A-3Design spec, status, and conditions for a CRD.
Answer
First 90 seconds. Before designing fields I'd pin down two things: who is allowed to write each field, and what a client is allowed to gate on. My assumptions: a controller-based operator on a current Kubernetes (1.29+), one CRD, and at least one other system that reads this resource to decide whether to act. Given that, the split is: spec is the user's desired state, status is the controller's report of observed state, and nothing derivable lives in spec. Clients gate on status.observedGeneration plus conditions: never on a phase string or an Event stream.
Spec. Declarative intent only. Every field that is mutable after creation says so in the OpenAPI schema, and anything with a sensible default gets a default: in the structural schema so the stored object is unambiguous. Fields that must not change after creation are enforced with CEL (x-kubernetes-validations with oldSelf), not with a webhook: fewer moving parts, and the rule travels with the CRD. If a value is computed from other fields, it belongs in status, not spec.
Status. Enable the subresource (subresources: status: {}). That gives you two things: a spec write by a user with update on the resource can't clobber status, and RBAC can grant the controller status write without granting it spec write. Status carries observedGeneration (copied from metadata.generation), any stable external IDs the controller allocated, a summarized effective state, and conditions.
Conditions. Use metav1.Condition rather than a hand-rolled struct: it's the shape every Kubernetes tool already parses. Types are positive polarity (Ready, not NotReady), reason is a stable CamelCase token a machine can switch on, message is one actionable sentence for a human, and lastTransitionTime only moves when status flips.
status:
observedGeneration: 7
conditions:
- type: Ready
status: "True"
observedGeneration: 7
lastTransitionTime: "2024-05-02T10:14:03Z"
reason: ReconcileSucceeded
message: "3/3 replicas available"
- type: Progressing
status: "False"
observedGeneration: 7
lastTransitionTime: "2024-05-02T10:14:03Z"
reason: ReconcileComplete
message: "no pending changes"Alternatives, and when to deviate.
| Option | Good when | Cost |
|---|---|---|
Single phase enum | Strictly linear lifecycle, one consumer, no orthogonal state | Can't express "serving but degraded"; every new state is a breaking enum change |
| Conditions | Multiple independent dimensions, several consumers | Verbose; clients must know which type to gate on; no ordering between types |
| Events for state | Audit trail, debugging | Best-effort, TTL'd (~1h), aggregated: never gate on them |
| Status in the main resource (no subresource) | Throwaway CRD, single writer | Any spec update clobbers status; RBAC can't separate the two |
I'd deviate to a phase only if the lifecycle is genuinely a state machine with one consumer, and even then I'd keep conditions alongside it.
Failure modes.
- Stale status trusted. Client reads before the controller has reconciled. Detect by comparing
status.observedGenerationtometadata.generation; if they differ, the status is from an older spec. - Status write conflicts. Concurrent writers hit
resourceVersionconflicts. Use a merge or server-side apply patch, or retry on conflict: don't read-modify-write the whole object. - Hot-looping status writes. Writing on every reconcile churns every watcher. Only write when a field actually changes semantically.
- Transient error blanks status. Keep the last known facts and set
Ready=False, reason=ReconcileErrorinstead of clearing the object; consumers lose their last good signal otherwise. - Secrets or unbounded text in status. Status is often readable by anyone with
geton the resource. Never put credentials there, and capmessagelength: full detail goes to logs or Events.
What would change the decision. If no external system reads this resource, conditions are overkill and a phase plus Events is enough. If two controllers must write status, I'd split the CRD rather than share the subresource. And if the CRD is cluster-scoped and world-readable, I'd re-audit every status field for leakage before shipping.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.