Controllers and the reconcile loop
How a Kubernetes controller works: watch, cache, queue, reconcile. Why it is level-triggered, idempotent and stateless, and why a missed event costs nothing.
Controllers and the reconcile loop
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A controller watches some kinds of objects, keeps a local cache of them, and runs a reconcile function that compares desired with actual state and closes the gap.
- Reconcile is level-triggered. It acts on the current state, not on the event that woke it, so missed or duplicated notifications do no harm.
- Informers keep one shared watch and cache per type, and a work queue holds object keys, deduplicated and retried with back-off, so one burst of changes means one reconcile.
- Reconcile must be idempotent and keep no state of its own. Everything it needs is in the API, which is why a restarted controller simply carries on.
- Controllers coordinate only through objects. The Deployment controller writes ReplicaSets, the ReplicaSet controller writes Pods, and neither calls the other.
- Event objects (kubectl events) are human-readable notes controllers leave behind. They are not how controllers learn about changes; watches are.
Who closes the gap
You write “three replicas” into a Deployment and three Pods appear. Somebody did that. A naive design would be a script triggered by your request: on create, make three Pods. It breaks at once. What if the script crashes after the second Pod? What if a node dies tomorrow and takes a Pod with it, when nobody has made a request? What if two requests arrive together?
Kubernetes uses controllers instead. A controller does not respond to requests. It keeps looking at the world and fixing whatever differs from what was declared. The orchestration survey built a toy one in Python. This lesson is about how real ones are built and why they are built that way.
The loop
Every controller has the same core:
- Observe the objects it owns and the objects it creates.
- Compare desired state (usually
spec) with actual state. - Act through the API to close the difference.
- Report what it saw in
status.
A thermostat is the usual picture, and it holds up. It does not remember that someone opened a window at 14:02; it reads the temperature now and turns the heater on or off. A controller reads the cluster now and acts.
Level-triggered, not edge-triggered
Electronics engineers distinguish edge-triggered logic, which reacts to a change, from level-triggered logic, which reacts to a state. Kubernetes controllers are level-triggered. The notification that wakes one up says only “look at this object again”. The reconcile function then reads the whole current state and works out what to do from scratch.
That is why controllers survive their own failures. The lab cluster made it easy to test. With the controller manager stopped, a 2-replica Deployment was scaled to 4 and both of its Pods were deleted:
# controller manager stopped
$ kubectl scale deploy api --replicas=4
$ kubectl delete pod -l app=api --wait=false
$ kubectl get deploy api -o jsonpath=... # spec, status, generation
spec=4 status=2 gen=3 observed=2
$ kubectl get rs -l app=api -o custom-columns=... # name, desired, current
api-5f8476ddb6 2 2
Nothing happened: the Deployment’s generation moved to 3 but observedGeneration stayed at 2. Then the controller manager was started again:
# 8 seconds after restart
spec=4 status=4 gen=3 observed=3
api-5f8476ddb6 4 4
api-5f8476ddb6-49xdt 0/1 Pending 0 6s
api-5f8476ddb6-pvjvp 0/1 Pending 0 6s
api-5f8476ddb6-rdw4w 0/1 Pending 0 6s
api-5f8476ddb6-vnfrj 0/1 Pending 0 6s
(The two deleted Pods were also still listed as Terminating, since no kubelet exists to confirm they stopped.)
The scale event and the two deletions happened while no controller was listening. It made no difference. On start, the controllers listed what existed, compared, and converged. An edge-triggered design would have lost those events for good.
Watch, cache, queue, reconcile
Polling the API server every second for every object would crush it. Real controllers, built on client-go or controller-runtime, use four parts:
- Watch. The controller lists a type once, then opens a long-lived watch that streams
ADDED,MODIFIEDandDELETEDnotifications from that resourceVersion on. - Informer and cache. An informer runs that list-and-watch and keeps an in-memory copy of every object. Reads come from the cache, not the API server. Controllers in one process share one informer per type, which is why the 49 controllers in
kube-controller-managerdo not open 49 watches on Pods. - Work queue. Event handlers do not do work. They push a key,
namespace/name, onto a queue. A key already waiting is not added twice, so five edits in a second become one entry. Failed keys are re-added with exponential back-off. - Reconcile. Workers pop a key, read the object from the cache, and do the compare-and-act step. Notice that reconcile gets a name, not the event. It cannot be edge-triggered even if you wanted it to be.
Informers also resync periodically, re-queueing every object they know about, so even a bug that drops a notification only delays convergence.
The rules that follow
Idempotent. Reconcile may run any number of times for the same state. Creating a Pod “because the event said a ReplicaSet was created” would make duplicates on every resync. Creating Pods “until there are N matching ones” is safe.
Stateless. Anything a controller needs to remember goes into the API: in status, in annotations, in owner references. A restarted controller has nothing to recover.
Careful with a stale cache. The cache lags the API server by a little. Right after creating Pods, the ReplicaSet controller’s cache may not show them yet, and a naive reconcile would create more. The built-in controllers use expectations: before creating N Pods the ReplicaSet controller records that it expects N creations, and skips that ReplicaSet until the informer has seen them, or until ExpectationsTimeout (5 minutes in controller_utils.go) passes. It also creates Pods in slow-start batches of 1, 2, 4, 8 and so on, so a broken template fails after one Pod instead of five hundred.
Talk through objects. The Deployment controller never calls the ReplicaSet controller. It writes a ReplicaSet; the ReplicaSet controller notices. The audit log in the control-plane lesson shows exactly that hand-off. This is what makes Kubernetes extensible: your own controller can watch Deployments, or anything else, exactly as the built-in ones do.
Events are not how controllers hear about changes
One common confusion is worth clearing up. Kubernetes has an Event object, the Scheduled, Pulled, FailedScheduling and ScalingReplicaSet lines in kubectl describe and kubectl events. Controllers write those as notes for humans. They are not the mechanism controllers use to learn about changes; the watch notifications on the objects themselves are. The cluster autoscaler, for instance, looks for Pods whose PodScheduled condition is False with reason Unschedulable, not for FailedScheduling Events. Events are also deleted after an hour by default.
About the video
The embed is a clip of Viktor Farcic’s “Kubernetes Controllers Deep Dive: How They Really Work” on DevOps & AI Toolkit (October 2025), from 11:56 to 21:01: the “Reconciliation Loop” and “Watch API” chapters. Farcic publishes a transcript, linked in deeper, so the clip was judged from that text and the chapters. It walks the reconcile function step by step (fetch by namespaced name, handle deletion, compare, act, update status, requeue with back-off), then informers, shared caches and per-controller work queues, which is this lesson’s middle. The clip starts after two earlier chapters that present controllers as consuming and emitting Event objects to coordinate, and that cite reasons such as DeploymentCreated and InsufficientResources that core Kubernetes does not emit. That is why the clip starts where it does. English was judged from the “English (US) original” audio track. Paolinelli’s talk in deeper goes through the same parts in Go.
What was run: the stopped-controller-manager experiment above ran against a local v1.37.3 control plane whose nodes run no containers, on 2026-11-28. The expectations timeout, the slow-start batch size and the burst limit were read from the v1.37.3 source (pkg/controller/controller_utils.go and pkg/controller/replicaset/replica_set.go). The informer and work-queue description follows client-go and sample-controller.
Where this fits
The controllers, API server and kubelet all move at the pace of Kubernetes releases. Next: releases, version skew and feature gates.
Check yourself
The controller manager is down while someone scales a Deployment from 2 to 4 and deletes its Pods. What happens when it comes back?
It lists the current state, sees 4 desired against what exists, and creates the missing Pods. It never needed the original scale event, because reconcile compares states rather than replaying changes. That is what level-triggered means.
A Deployment is edited five times in one second. How many times does its controller reconcile it?
Usually once or twice. Each change adds the same namespace/name key to the work queue, and the queue holds a key only once until a worker picks it up, so the burst collapses into a reconcile that sees the latest state.
Why must reconcile be idempotent?
It can run any number of times for the same state: after a resync, a retry, a restart, or a duplicate notification. If running it twice created two Pods or sent two emails, every one of those would be a bug. Reconcile must converge on the same result no matter how often it runs.
A cache can be slightly stale. What stops the ReplicaSet controller creating extra Pods because it has not yet seen the ones it just made?
Expectations. Before creating N Pods it records that it expects N creations, and skips syncing that ReplicaSet until its informer has observed them, or until a 5-minute timeout passes. Hand-written controllers usually lean on owner references and deterministic names instead.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.