kube-controller-manager
Creates (initializes) and runs controllers
Core Initialization (run function)
CreateControllerContext- Creates
SharedInformerFactoryandClientshared by all controllers
- Creates
BuildControllers- Creates and registers informers
- Controllers register handlers (subscribe) to informers
InformerFactory.Start- Starts the
InformerFactoryto begin cache synchronization
- Starts the
RunControllers- Iterates through each controller's loop
Controller
An infinite loop that aligns the desired state with the current state
Exists only for resources that require active state management (Reconciliation)
Role
- Orchestrates multiple controllers (existing per resource)
- Manages shared dependencies (Informers, Clients)
- Handles High Availability (Leader Election)
Leader ElectionThree
kube-controller-managerprocesses (instances) are launched, one on each of 3 Master Nodes.
- Only one of these is elected as the leader (election criteria: first come, first served) and creates/runs all controllers.
- The leader renews a document called
Leaseevery 2 seconds (for periodic liveness checks).- The others act as standby, taking over when the leader fails.
- Since the original source of truth for all data is stored only in etcd, no handover is necessary.
Operation
Only the
keyis put into thequeue, and aWorkerretrieves it from the queue to execute business logic (sync).
- Registers EventHandler to the informer
- The EventHandler executes the logic to put the key into the queue.
- Uses a Rate Limiting Queue
workqueue.TypedRateLimitingInterface[string]- Retries become slower as the number of retries increases.
- Key format
- "Namespace/Name"
- Data is always looked up just before processing.
- Data is looked up through the lister from the informer's local cache.
Informer
"Instead of directly querying the DB, let's subscribe to changes and keep a copy in our local memory (Cache)."
Exists for almost every resource type (Kind)
Role
- API (Kubernetes API Server) Watch
- Creates a connection with a single request and continuously receives the response body.
- Cache Construction
- Local Cache
cache.cacheStorage:treadSafeMap- lock (sync.RWMutex): Concurrency control
- items (
map[string]any): Actual data storage - index (storeIndex): Auxiliary index for fast searching
- Local Cache
- Notifies subscribed controllers of events
sharedInformerFactoryManages informers shared by controllers in a map,
ensuring that only one informer (watch, cache) exists for the same resource.