Sub topic 6
List and watch: how controllers hear about changes
This chapter assumes you know the control loop from the previous chapters, can use kubectl, and can read JSON. It covers how a controller learns that something changed, without scanning the whole cluster in a tight loop.
Why not just poll?
A controller could call "list all Pods" every second. With thousands of objects and dozens of controllers, that would overload the API server and still react late. Kubernetes gives clients two operations on every resource type instead.
- List returns all matching objects at one point in time, plus a
resourceVersionfor that snapshot. - Watch opens a long-lived HTTP connection. The API server streams one event for each change after a given
resourceVersion.
A resourceVersion is an opaque string that marks a position in etcd's history. Treat it as a bookmark and never do arithmetic on it.
The list-then-watch pattern
A client lists first, then watches from the version the list returned. That way no change falls between the two calls.
Each watch event has a type and the full object after the change.
| Event type | Meaning |
|---|---|
| `ADDED` | The object was created, or matches your selector for the first time. |
| `MODIFIED` | Any field changed, including `status`. |
| `DELETED` | The object was removed. |
| `BOOKMARK` | No object changed. The server is advancing your `resourceVersion`. |
| `ERROR` | The watch failed, for example with a status of 410. |
Watching by hand
You can see the raw stream through kubectl proxy.
kubectl proxy --port=8001 &
sleep 1
RV=$(kubectl get pods -n default -o jsonpath='{.metadata.resourceVersion}')
curl -sN "http://127.0.0.1:8001/api/v1/namespaces/default/pods?watch=true&resourceVersion=${RV}&allowWatchBookmarks=true"In another terminal, run kubectl run probe --image=nginx:1.27. You will see one JSON line for each change: ADDED, then several MODIFIED events as the Pod is scheduled, starts, and becomes ready.
Common errors
- 410 Gone. etcd compacts old history, so a watch from an old
resourceVersionis refused. The client must list again and resume from the new version. Client libraries do this for you. - Dropped connections. Watches end because of timeouts, API server restarts and load balancers. A controller must expect this and reconnect from the last
resourceVersionit saw. - Treating events as commands. A watch can be interrupted and restarted, so you may miss intermediate states. This is why controllers react to current state and not to the sequence of events, as the previous chapter explained.
- Watching too broadly. Label and field selectors, such as
labelSelector=app=web, are applied on the server and cut traffic.
In practice you rarely write this loop yourself. Client libraries wrap it in a component called a reflector, which the next chapter covers along with informers and caches.
Exercise
A controller's watch connection drops, and when it reconnects with its last known resourceVersion the API server returns 410 Gone. What should the controller do, and why can't it just resume?
Show answer
It should list the resources again and start a new watch from the resourceVersion returned by that list. It can't resume because etcd has compacted the history between its last version and now, so the server can no longer replay the missed events. The fresh list gives it the current state, which is all a level-triggered controller needs.