Controller reconciliation
Reconcile the current state, not the event
A controller repeatedly compares a desired configuration with observations and takes a limited action to reduce the difference. In our bookstore, a BookRequest asks for three books. A mock inventory reports one. The controller reports Pending; when the observation reaches three it reports Fulfilled. Nothing orders books in this lab. The CRD from Chapter 44 supplies the API contract, while this model supplies a tiny decision rule.
Desired: 3Observed: 1 -> PendingObserved: 3 -> FulfilledSeparate desired, observed and reported values
Read spec.quantity for the desired count, obtain an independent observation from a mock inventory, and compare it with status.fulfilledQuantity and status.phase. Previously reported status is not a new observation. A missing object is a normal end state for a queued key. A deletion timestamp should divert the controller from ordinary work. The sample has no external assets to clean up and therefore needs no finalizer.
spec.quantity: 3mock observation: 2status.fulfilledQuantity: 1Decision: report 2, PendingMake the decision idempotent
Idempotence here means that after the intended status update succeeds, another reconcile with the same inputs requests no new write. The pure Go function Plan returns a decision instead of making an API call. The actual caller must persist a status change and handle failure. Comparing both phase and count avoids writing unchanged status on every event. The following excerpt assumes Snapshot and Decision from the supplied source file.
phase := "Pending"if s.Observed >= s.Desired { phase = "Fulfilled" }if s.Phase == phase && s.Reported == s.Observed { return Decision{Action: "noop", Phase: phase, Reported: s.Observed}}return Decision{Action: "update-status", Phase: phase, Reported: s.Observed}Handle ownership, deletion and status honestly
If a later controller creates a dependent ConfigMap, a valid owner reference identifies the owner UID; a matching name alone is not ownership. Namespaced dependents cannot point to an owner in a different namespace. A finalizer is appropriate only for required cleanup that cannot be handled by garbage collection. Add it before creating external state, make cleanup repeatable, and remove only your own key after successful cleanup. Never use a finalizer as a lock. A production status schema can expose conditions with observedGeneration to show which spec revision was evaluated; the inherited toy CRD has only phase and fulfilledQuantity, so do not write undeclared condition fields.
Current schema: status.phase, status.fulfilledQuantityFuture schema revision: conditions, observedGeneration (not implemented)Practice and next step
Read reconcile.go and reconcile_test.go in lab-fixtures/controller-model. Predict the outcome of an unchanged status, an increased desired count and an absent resource. Run the offline tests as described in the README. Explain why a successful function test does not show that status was persisted. Next: informer cache and workqueues.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Controller reconciliation — knowledge check
Design an idempotent reconciliation decision using a BookRequest and independent mock observations.
A BookRequest wants 3 and mock inventory reports 1. What phase is appropriate?
What should a repeated reconcile request when phase and reported count already match?
Which value provides the current independent evidence in this model?
What identifies an owner instance in an owner reference?
When should a finalizer be removed?