Appearance

Build an idempotent operator

Reconcile one WebApp into ordinary Kubernetes resources and verify that repeated runs preserve identity.

What you will understand

  • Derive child resources from the current WebApp specification.
  • Explain why reconciliation must tolerate a crash between writes.
  • Verify generated resource identity and status across repeated passes.

Builds on: CRDs, schemas, and conversion

How a WebApp becomes running resources

A user changes a WebApp from one replica to two. They expect that edit to become a Deployment change, and they expect recovery if a managed child is deleted. An operator combines Kubernetes APIs and controller logic to manage an application or operational process. Its reconcile loop reads desired and observed state and makes changes that move the system toward the goal.

In this lab the WebApp holds a replica count and a message. The lab reconciler writes a ConfigMap for the page content and a Deployment for the requested replicas. A repeatable or idempotent reconciliation can run again and reach the same intended state. It can find an existing object, check ownership, and update the relevant fields instead of creating duplicates. The scene shows which child changes when you edit the parent’s specification.

Turn a WebApp into Kubernetes resources

The lab reconciler turns the message into a ConfigMap and its replica count into a Deployment. It can apply the same desired fields again safely.

  1. ReconcilerRead the WebApp spec and UID
    WebApp m16The spec supplies the message and desired replica count.
  2. ReconcilerApply the owned ConfigMap
    ConfigMap m16The requested page content is available.
  3. ReconcilerApply the Deployment that mounts the page
    Deployment m16The Deployment controller manages attainment of the requested replica count.
Follow the sequence

The lab reconciler turns the message into a ConfigMap and its replica count into a Deployment. It can apply the same desired fields again safely.

The downloadable controller polls through kubectl and intentionally omits production informer and queue machinery.

Try the mechanism with your own inputs
The replica target already agreesDesired and observed replica targets agree. Repeating reconciliation preserves the same stable specification. The lab operator may still apply an unchanged object.. WebApp desired, Replicas: 1, active. Operator, Target agrees, ready. Deployment target, spec.replicas: 1, readyWebApp desired: Replicas: 1
WebApp desiredReplicas: 1In focus
Operator: Target agrees
OperatorTarget agreesReady
Deployment target: spec.replicas: 1
Deployment targetspec.replicas: 1Ready
WebApp desiredReplicas: 1In focus
OperatorTarget agreesReady
Deployment targetspec.replicas: 1Ready
The replica target already agrees

Desired and observed replica targets agree. Repeating reconciliation preserves the same stable specification. The lab operator may still apply an unchanged object.

apiVersion: labs.kv.example/v1alpha1
kind: WebApp
metadata:
  name: example
  namespace: kv-lab
spec:
  replicas: 1
  message: Hello from the control loop
# Child Deployment.spec.replicas target: 1
Explore another case

Recover after writing only some children

Each Kubernetes object write has its own outcome. Suppose the reconciler writes the ConfigMap and then exits before writing the Deployment. The next pass can find the existing ConfigMap and create the missing Deployment. Status tells the reader what the reconciler observed at a point in time. A successful change to desired replicas can precede the availability of the new Pods.

apiVersion: labs.kv.example/v1alpha1
kind: WebApp
metadata:
  name: m16
  namespace: kv-lab
  labels:
    lab: m16
spec:
  replicas: 2
  message: repeated-reconcile-is-safe

How can you tell which update the operator processed?

Repeated reconciliation keeps owned object identity stable. Status can say which generation was observed even if the Pods are still starting.

  1. Reapply the same child intent

    Another pass applies the same child name and ownership instead of creating a replacement Deployment.

  2. Read Deployment status

    The operator reads readyReplicas after applying; rollout may still be progressing.

  3. Report observedGeneration

    Status records the desired generation processed and the ready count actually observed, which may still be zero.

The minimal API lacks a Ready condition; sampled readyReplicas may reflect an earlier Deployment generation and does not prove current-revision readiness.

Explore another case

Run reconciliation again and compare UIDs

Start with the module resources applied. Use the example WebApp spec for a change to two replicas. Run the lab script once and wait for the generated Deployment. Record its UID. Then run three more passes and read the UID and WebApp status again. An unchanged UID supports that the Deployment was kept in place during reconciliation. Inspect the replica count and the child resources too, since UID alone cannot establish that every desired field was applied.

# Run after installing the m16 CRD and resources.
python3 webapp-reconciler.py --once
kubectl --context kind-kv-lab -n kv-lab rollout status deployment/m16 --timeout=180s
kubectl --context kind-kv-lab -n kv-lab get deployment m16 -o jsonpath='{.metadata.uid}{"\n"}'
python3 webapp-reconciler.py --iterations 3 --interval 1
kubectl --context kind-kv-lab -n kv-lab get deployment m16 -o jsonpath='{.metadata.uid}{"\n"}'
kubectl --context kind-kv-lab -n kv-lab get webapp m16 -o yaml

In the operator lab, deliberately remove a generated object after a good run. Run the reconciler again to check that the missing child returns while existing children stay owned and intact.

What a deployed operator needs beyond the lab

The lab script is a polling exercise you run from your machine. A deployed operator usually needs stable API permissions, proper watches, retries, pacing, concurrent update handling, and observable conditions. If it calls an external service, test what happens when the service fulfills a request but the controller times out before seeing the reply. Stable ownership and idempotent operations matter more than the number of loop iterations.

Check your understanding

The reconciler wrote the ConfigMap but crashed before writing the Deployment. What should its next pass do?

It can read the current WebApp and the existing ConfigMap, update owned fields if needed, and create the missing Deployment. That keeps working resources stable and recovers from a partial pass.

Go to the source

Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise

Search the book

Search loads when you open it.

Keyboard shortcuts

Ctrl + K / ⌘ + K
Search the book
?
Open this guide
Tab / Shift + Tab
Move between controls
Enter / Space
Activate a focused button or disclosure
← / →
Focused visual timeline · previous or next step
Home / End
Focused visual timeline · first or last point
Esc
Close the open dialog or menu