Sub topic 5
Why controllers react to state, not to events
This chapter assumes you know Pods, Deployments and kubectl, and that you can read short YAML and Go snippets. It explains why Kubernetes controllers compare current state with desired state on each pass. They do not act on a stream of individual change events.
Two ways to react to change
A system can respond to change in two ways.
- Edge-triggered: the logic runs when something changes. The input is an event such as "a Pod was deleted". If the program misses the event, it never learns the change happened.
- Level-triggered: the logic runs against the current level of the system. The input is a question: "what does the world look like now, and what should it look like?" A missed notification costs only time, because the next pass reads the same facts.
Kubernetes controllers are level-triggered. A thermostat works the same way. It does not count "door opened" events. It reads the temperature and heats if the room is below the target.
What the controller actually receives
Events still exist. A watch tells the controller that an object changed, and that notification puts the object's key on a work queue. The key is a namespace and name, and it carries no description of what changed.
Look at the Reconcile signature from the controller-runtime library:
func (r *NamespaceDefaultsReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error)ctrl.Request holds only a NamespacedName. The function has no "Pod deleted" argument. It must read the object and its dependents, then decide what to do. The event only says "look at this object again".
Why this design is safer
Several failure modes become harmless.
- Missed notifications. A dropped watch connection or a controller restart loses events. After a restart the controller lists everything and reconciles every object, so it catches up.
- Bursts. If a Deployment changes ten times in one second, the queue holds one key for it. The controller reconciles once against the newest state. It does not replay ten steps.
- Out-of-order or duplicate events. Order does not matter because each pass reads the current state.
- Drift without events. Periodic resyncs trigger passes even when nothing was reported.
What it demands from you
Level-triggered logic only works if each pass is safe to repeat. Write reconcile code that computes "what is missing or wrong now" and fixes that. Avoid code that assumes "this runs once per change".
| Edge-style code (fragile) | Level-style code (correct) |
|---|---|
| On "Pod deleted", create one Pod | Count Pods, create `desired - actual` |
| On "created", send a notification once | Check whether it was sent, record that in status |
| Keep a private counter of events seen | Derive everything from the API server's objects |
The toy loop from earlier shows the idea. Each pass moves actual one step toward desired, and extra passes change nothing once the two match.
Exercise
A controller misses the watch event for a deleted Pod because its connection dropped for 30 seconds. Why does a level-triggered controller still repair the Deployment, and what would go wrong with an edge-triggered one?
Show answer
When the watch reconnects, or on the next resync, the controller reconciles the Deployment's key. It reads the current state, finds fewer Pods than spec.replicas, and creates the missing one. An edge-triggered controller acts only on the delete event, so after missing it, it never learns that a Pod is gone and the Deployment stays under-replicated.