When creating a Kubernetes controller, you will often need to check whether the cluster matches what we’ve asked for. A resource might still be starting, become unhealthy, or receive a configuration change. To respond, a controller needs an up-to-date view of the resources it manages.
It could repeatedly query the Kubernetes API, but many responses would contain nothing new. Informers, part of Kubernetes’ Go client library, client-go, maintain a local cache and notify controller code when that view changes. Let’s follow a machine controller to see how this works.
Suppose we’re writing a machine controller that creates virtual machines from Machine custom resources and tracks their readiness. Each Machine describes the configuration we want. Once the Machine custom resource is created, work remains: it needs to be assigned to a node, boot, join the cluster, and become ready. A Node represents it in the cluster, and our controller records that Node’s reference on the Machine.
Informers provide a way to keep a local view of those resources and receive notifications as that view changes. They’re part of the Kubernetes Go client, and they handle much of the work involved in keeping a controller informed about the cluster.
A small history of informers
Informers have been around since before Kubernetes 1.0.pkg/controller/framework package already included a function called NewInformer. It returned a local store and a controller that populated it while delivering notifications about resource changes. Replicating this machinery across controllers became another part of the design. If several controllers in the same process needed to watch Pods, maintaining separate watches and copies of the same objects would duplicate work.
Today, these building blocks live in client-go. A shared informer lets multiple handlers use the same cache and underlying resource watch within a process.
How does an informer work?
I find it useful to start with the two things our machine controller needs to know. For our machine controller, the Node informer needs to discover the Nodes that already exist, then follow changes to them. One way the informer establishes this view is through the Kubernetes API’s LIST and WATCH operations.
First, the informer lists the Nodes and uses the response to populate its cache. The response also includes a resourceVersion, which identifies the point represented by that list. The informer uses this value to begin watching from that point, so it can receive subsequent additions, updates, and deletions. These requests happen inside the informer, without separate list-and-watch code in our controller.
The initial list populates the cache, and the watch supplies subsequent changes.
Kubernetes also supports streaming the initial objects through a watch. With either approach, the informer first establishes a view of the resources and then keeps it updated. As it updates the cache, the informer calls our handlers. We supply these through AddFunc, UpdateFunc, and DeleteFunc. The add handler also receives Nodes discovered during startup, so an addition doesn’t necessarily mean the Node was just created.
Applying this to our machine controller
Suppose a Node’s Ready condition becomes true. The Node informer observes the updated object, updates its cache, and notifies our handler. Our handler finds the Machine whose Node reference matches and adds that Machine’s namespace and name to a work queue. A worker then picks it up, reads the Machine and Node, and decides whether the Machine’s status needs updating.
This keeps the handler brief and gives the controller somewhere to manage processing and retries. Our controller uses the same pattern as Kubernetes’ sample controller where the handler queues the Machine’s key, and a worker reads its state when processing that key.
For our controller, I’d treat the notification as a reason to check the Machine again. If its Node is ready when we check, we can act on that observation. The controller should also reach the same result after restarting and discovering a Node that was already ready.
The handler queues the Machine’s key. The worker reads from the queue and checks the observed resource state.
Taking this a step further, we can use client-go to watch the Nodes in our cluster. The snippets below show the pieces we would add to a controller, using client-go v0.34.0. They assume we already have a Kubernetes client called clientset and a context called ctx for shutting down the controller.
In Kubernetes controller development, a factory most commonly refers to the SharedInformerFactory (from the Kubernetes client-go informers package). It is a design pattern used to create, cache, and manage the lifecycle of informers.
We can create the factory and request a Node informer like this:
factory := informers.NewSharedInformerFactory(clientset, 0)
nodes := factory.Core().V1().Nodes()
nodeInformer := nodes.Informer()Here, informers is the k8s.io/client-go/informers package. The factory manages the informers we request from it. Asking the same factory for the same resource type reuses the informer, so different parts of our controller can share its cache.
Nodes are cluster-scoped, so there is no namespace option in this example. Creating a factory doesn’t start watching every resource Kubernetes supports. We request the informers we need and then start them.
Registering handlers
Before starting the factory, we register the functions that should receive notifications. For our controller, they would identify the affected Machine and queue it for reconciliation.

The following is an illustrative wiring example. enqueueMachineForNode represents our own helper. It still needs to find Machines whose recorded Node reference matches the Node’s name and UID, then add each Machine’s namespace and name to the queue. A custom index on those references would avoid scanning every Machine for each notification.
_, err := nodeInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: enqueueMachineForNode,
UpdateFunc: func(oldObj, newObj interface{}) {
enqueueMachineForNode(oldObj)
enqueueMachineForNode(newObj)
},
DeleteFunc: enqueueMachineForNode,
})
if err != nil {
return err
}cache comes from k8s.io/client-go/tools/cache. Because we registered these handlers on the Node informer, AddFunc, UpdateFunc, and DeleteFunc describe additions, updates, and deletions of Nodes. Passing both objects on an update lets our helper account for a Node replaced by another with the same name but a different UID.
All three handlers queue only the Machine’s key, such as default/worker-0. The key carries no add, update, or delete instruction. It tells the worker to check that Machine again. Our reconciliation logic reads the Machine and its referenced Node, then decides what needs creating, updating, or deleting. For example, a Node deletion might lead to marking the Machine as not ready; calling DeleteFunc does not tell the worker to delete the Machine.
For a Node deletion, the helper normally receives a *corev1.Node. However, the informer can lose its watch connection and miss the deletion. If a later list shows that a previously cached Node is missing, the informer may pass cache.DeletedFinalStateUnknown instead. This wrapper holds the last known Node object because its final state before deletion is unknown. Our helper must therefore unwrap the notification before checking that it contains a Node. A direct obj.(*corev1.Node) assertion would panic if obj were the wrapper. Once we have the Node, we can use its name and UID to find the associated Machine and queue its key.
Starting the informer
Once the handlers are registered, we can start the factory and wait for the Node cache to initialize:
factory.Start(ctx.Done())
if !cache.WaitForCacheSync(ctx.Done(), nodeInformer.HasSynced) {
return fmt.Errorf("Node cache did not sync before shutdown")
}This only starts the Node informer. Our controller also needs a separate informer for its custom Machine resources, so changes to a Machine can enqueue work directly. We wait for both caches before starting the queue workers. The built-in factory above does not create an informer for our custom resource.
We start our queue workers after those checks. Otherwise, an empty cache during startup could be mistaken for a cluster with no Nodes. Synchronization confirms that the initial population has completed, though later reads can still lag behind the API server. Cache synchronization
Reading from the cache
When a worker needs to inspect a Node, it can use the informer’s lister:
node, err := nodes.Lister().Get("worker-0")This lookup reads the local cache. It doesn’t make another request to the API server. The worker still needs to handle the error, including the possibility that the Node is no longer present.
Objects returned by the lister are shared and must be treated as read-only. If we need to prepare a change, we first call node.DeepCopy() and modify the copy. Persisting that change still requires an API request.
For our machine controller, the worker would read the Machine’s requested configuration and the referenced Node’s conditions before updating the Machine’s status. A notification tells us that there is something to reconsider. The resources we observe during reconciliation determine what we do next.
Summary
Informers are essential to how controllers handle various events that occur during the lifecycle of any resource it manages or is concerned with.
In this blog, we went over the basics of why they exist, how to use them in a controller, as well as some patterns Kubernetes recommends.

