Overview
Curated: · Written: · Reviewed:
Kubernetes Operators & CRDs
In one line
A CRD adds a typed resource to the API server. An operator is the controller that reads that spec and converges real state toward it. Reach for both when users need a long-lived domain object with lifecycle automation, not a one-shot command.
Key definitions
- CRD: registers a resource kind with a structural schema, storage, discovery, RBAC and watch. Does no work by itself.
- Controller: a level-triggered loop that re-reads spec and observed state, then acts.
- Status subresource: a separate
/statusendpoint. Main-resource writes ignore status. - Finalizer: an entry in
metadata.finalizersthat blocks deletion until the controller removes it. - ownerReference: links dependents to an owner so garbage collection deletes them when the owner is gone.
- storedVersions: the versions that may still exist in etcd. Migrate objects before removing a version.
How it works
- Events only wake the loop. Reconcile never acts on the event payload, because a dropped watch can hide several changes.
- Every side effect is keyed on a stable identifier such as the CR UID, so a repeat adopts the existing resource instead of creating another.
- Set
observedGenerationonly after side effects succeed. - A timeout means the outcome is unknown, not failed. Look up before retrying.
- On delete, the finalizer holds the object. The controller cleans up, then removes only its own finalizer.
When to use and when not to
| Situation | CRD plus controller | Prefer instead |
|---|---|---|
| Long-lived domain object users observe and reconcile | Yes | |
| Transient command such as a migration run | No | Job |
| Config a Deployment plus HPA already expresses | No | Built-in resources |
| Custom storage, protocol or API semantics | No | Aggregated API server |
| Only object-local rules | No | CEL rules in the CRD schema |
Traps interviewers probe
- Removing a finalizer to unstick deletion. The object disappears and external resources leak. Fix: let the controller finish cleanup. Force-removal is an audited runbook step that records impact.
- Acting on the event payload. After a dropped watch, one event can stand for three spec changes. Fix: re-read the current object and converge.
- Retrying a timed-out create under a new name. The first call may have succeeded. Fix: treat the timeout as unknown, look up by the UID label, and reuse a deterministic identity.
- Calling slow external APIs in a webhook. A 30 s provider call blocks every apply and times out at 10 s. Fix: validate and default in admission, and provision in the controller.
- Assuming a leader lease fences side effects. An old leader paused mid-call still lands its request. Fix: make the external call idempotent and carry a fencing token.
Interview tips
Open with the split: "Kubernetes gives me the API, storage and watch. I own the control loop that converges state."
Follow-ups to expect:
- What a CRD gives you for free: storage, validation, discovery, RBAC, watch, garbage collection. Not automation.
- The controller crashes mid-reconcile: the level-triggered re-read makes it safe.
- Cloud cleanup on delete: finalizer, UID binding, idempotent delete, status progress.
- Adding v1 after v1alpha1: a lossless conversion, the storage version, and a migrated
storedVersionsbefore removal. - Who may create the CR: the confused-deputy question. The controller must not turn create permission into unrestricted infrastructure authority.
- Status: write it through
/status, retry conflicts with a fresh read, and comparegenerationtoobservedGeneration.
30-second outline:
- Name the domain object and its spec/status split.
- Say reconcile is level-triggered and re-reads state.
- Name the idempotency key (UID) and how a lost response is handled.
- Describe deletion: finalizer, cleanup, then removal of only your finalizer.
- Name the failure you watch for: stuck Terminating, reconcile errors, stale generations.
- Close with scale: bounded workers, backoff with jitter, leader election plus idempotent work.
Verify feature-state notes against the target cluster version.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.