Sub topic 4
The control loop: observe, compare, act
This chapter assumes you know basic Kubernetes objects such as Pods and Deployments, can read YAML, and can follow short Go code. It also assumes you know that an object has a spec and a status, which the previous chapter covered.
The three steps
A controller is a process that runs one loop forever. Each pass has three steps:
- Observe. Read the desired state (the
spec) and the actual state of the cluster. - Compare. Work out the difference between the two.
- Act. Make the calls that move the actual state one step toward the desired state.
Then the loop starts again. A controller never "finishes". It keeps watching, so it also repairs drift that happens later, such as a node failing or someone deleting a Pod by hand.
A toy loop
The program below models a ReplicaSet controller. The desired count is 3 and the actual count starts at 0. Each pass compares the two and takes one step.
package main
import (
"fmt"
"time"
)
func main() {
desired := 3
actual := 0
for i := 0; i < 5; i++ {
if actual < desired {
actual++
fmt.Printf("created replica, actual=%d desired=%d\n", actual, desired)
} else if actual > desired {
actual--
}
time.Sleep(100 * time.Millisecond)
}
}Three passes create replicas. The fourth and fifth passes find actual == desired and change nothing. This is the property that makes the pattern safe: running the loop extra times does no harm.
The loop does not ask "what happened since last time?". It only asks "what is different right now?". The next chapter returns to this point as level-triggered logic.
Seeing it on a cluster
You can watch a real controller do the same thing. Create a Deployment, then delete its Pods:
kubectl create deployment web --image=nginx:1.27 --replicas=3
kubectl get pods -l app=web -w &
kubectl delete pod -l app=web --field-selector=status.phase=Running --wait=false
kubectl get replicaset -l app=web
kubectl describe deployment webThe ReplicaSet controller observes fewer Pods than spec.replicas. It creates replacements until the counts match. You never asked it to recreate anything. You only declared that three should exist.
Common mistakes
- Treating one pass as a transaction. A pass may be cut short by an error. The next pass must be able to pick up from whatever state it finds.
- Remembering what you did last time. A controller that stores "I already created replica 2" in memory breaks after a restart. Derive everything from observed state.
- Expecting instant results. The loop runs after a delay, so
statuscan lag behindspec. Comparemetadata.generationwithstatus.observedGenerationto tell whether the controller has seen your latest change.
Exercise
In the toy loop, suppose someone sets actual to 7 while desired is 3. What does each pass do, and how many passes does it take to converge?
Show answer
Each pass finds actual > desired and decrements actual by one. It takes 4 passes to go from 7 to 3. After that, passes change nothing.
Exercise
A Deployment shows generation: 2 but status.observedGeneration: 1. What does this tell you?
Show answer
The controller has not yet processed the latest spec change. The status you are reading describes the older spec, so it may not reflect your update.