Kubernetes Operators & The Controller Reconciliation Loop (Kubebuilder / Operator SDK)
Build operators that continuously reconcile custom resources to desired state.
In plain words
An operator is an on-call engineer modeled as a loop: observe, compare, act, record status, and then sleep until the next event. The simple Kubernetes rule is: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.
Think of it like…
An operator is an on-call engineer modeled as a loop: observe, compare, act, record status, and then sleep until the next event.
Where you’ve seen it
A platform team reviews a promoted source revision and runs kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}'. Expected evidence: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion. If the run instead shows this symptom, stop and restore the last known state: A non-idempotent reconcile repeats side effects on retries, and a missing finalizer status update can trap a resource in Terminating forever.
Words you’ll hear
- Operator
- For this lesson, Operator is the controlling idea: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.
- Reconcile Loop
- Reconcile Loop is the checkpoint; run
kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}'and inspect the owner chain. A non-idempotent reconcile repeats side effects on retries, and a missing finalizer status update can trap a resource in Terminating forever. - Kubebuilder
- Kubebuilder is the outcome to preserve; the cost is explicit: Operators encode expert operations but controller bugs, API churn, and external credentials turn the automation into a production dependency.
- Verification evidence
- The evidence command is
kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}'. It connects the declaration to this behavior: Build operators that continuously reconcile custom resources to desired state.
Visual diagram
Theory & deep dive
How Operator reaches the intended state
Build operators that continuously reconcile custom resources to desired state. An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.
The concrete checkpoint is kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}'; compare the Operator owner, Reconcile Loop state, and Kubebuilder outcome instead of treating YAML acceptance as proof.
Run kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}' after the change and correlate the result with Events and the owning object. An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.
Reconcile Loop: failure boundary and production cost
A non-idempotent reconcile repeats side effects on retries, and a missing finalizer status update can trap a resource in Terminating forever. The prerequisite contract is concrete: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.
Operators encode expert operations but controller bugs, API churn, and external credentials turn the automation into a production dependency. A safe design limits blast radius, records the last good configuration, and uses kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}' as a deterministic rollback checkpoint.
Capture the current state with kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}', then correct the controlling manifest rather than patching a child object; the relevant divergence signal is specific: A non-idempotent reconcile repeats side effects on retries, and a missing finalizer status update can trap a resource in Terminating forever.
How it works, step by step
Step 1 of 41. Choose the source of truth
Version the source and describe the review, render, and reconciliation boundary: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.
- Goal
- Build operators that continuously reconcile custom resources to desired state.
- Owner
- Operator
When to use it & trade-offs
What it is
Build operators that continuously reconcile custom resources to desired state. An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.
Why it matters: A non-idempotent reconcile repeats side effects on retries, and a missing finalizer status update can trap a resource in Terminating forever. The invariant at risk is specific: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion. The quickest fact check before changing the design is kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}'.
When to reach for it
- Use Operator to enforce this invariant: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.
- Use Reconcile Loop evidence from
kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}'in reviews and incident timelines. Build operators that continuously reconcile custom resources to desired state. - Choose the platform operator workflow route only when this cost is justified by governance or scale: Operators encode expert operations but controller bugs, API churn, and external credentials turn the automation into a production dependency.
When to avoid or be careful
- Avoid assuming Operator is complete after YAML submission; the required behavior is concrete: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.
- Avoid treating the next symptom as random; check the owner and state with
kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}'first: A non-idempotent reconcile repeats side effects on retries, and a missing finalizer status update can trap a resource in Terminating forever. - Avoid adopting a heavier platform wrapper before measuring its cost: Operators encode expert operations but controller bugs, API churn, and external credentials turn the automation into a production dependency.
Native, manual, and managed paths for Operator
kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}'.Strengths: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.
Watch out: Operators encode expert operations but controller bugs, API churn, and external credentials turn the automation into a production dependency. Recovery must also recognize this signal: A non-idempotent reconcile repeats side effects on retries, and a missing finalizer status update can trap a resource in Terminating forever.
kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}'; the expected invariant is: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.Strengths: kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}' gives immediate feedback without first building a platform layer.
Watch out: A manual change can hide drift, and unlike Operator, it does not make this symptom self-documenting: A non-idempotent reconcile repeats side effects on retries, and a missing finalizer status update can trap a resource in Terminating forever.
kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}'.Strengths: Platform controls can wrap this exact behavior in policy or dashboards: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.
Watch out: The added platform ownership buys value at this cost: Operators encode expert operations but controller bugs, API churn, and external credentials turn the automation into a production dependency.
Interview signals & numbers to know
When this topic comes up
- Interview anchor: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.
- Trace Operator to Kubebuilder through Reconcile Loop; the invariant is: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.
- Recognize the symptom: A non-idempotent reconcile repeats side effects on retries, and a missing finalizer status update can trap a resource in Terminating forever.
- Justify the cost: Operators encode expert operations but controller bugs, API churn, and external credentials turn the automation into a production dependency.
- Verify with kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}' instead of only inspecting YAML.
Benchmarks & rules of thumb
- 4
- loop phases: observe, compare, act, record
- 1
- reconcile should be idempotent at each retry
- 0
- business side effects should depend on an ordered queue without deduplication
Common mistakes & follow-ups
Common interview mistakes
- Debugging only the child object misses the Operator owner contract: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion.
- Ignoring this telltale condition leads to unrelated changes: A non-idempotent reconcile repeats side effects on retries, and a missing finalizer status update can trap a resource in Terminating forever.
- Repeating
kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}'without an owner for remediation wastes retry cost. Operators encode expert operations but controller bugs, API churn, and external credentials turn the automation into a production dependency.
Likely interviewer follow-ups
- How would API output and runtime evidence prove this behavior: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion?
- Which owner object, logs, and Events would you inspect for this symptom-A non-idempotent reconcile repeats side effects on retries, and a missing finalizer status update can trap a resource in Terminating forever-?
- At what threshold would this cost become unacceptable: Operators encode expert operations but controller bugs, API churn, and external credentials turn the automation into a production dependency?
kubectl get pods -n operators; kubectl logs deploy/db-operator -n operators; kubectl get db app-db -o jsonpath='{.status}', name the failure, then justify the cost. Invariant: An operator watches custom resources, computes desired external or Kubernetes state, performs idempotent work, updates status, and uses finalizers only for cleanup that must outlive deletion. Failure: A non-idempotent reconcile repeats side effects on retries, and a missing finalizer status update can trap a resource in Terminating forever. Cost: Operators encode expert operations but controller bugs, API churn, and external credentials turn the automation into a production dependency.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Kubernetes Operators & The Controller Reconciliation Loop (Kubebuilder / Operator SDK) with generative AI interview Q&A and hands-on practice.