Kubernetes List/Watch, Informer Mechanism, and Writing Controllers for Pods and Custom Resources
This article explains how Kubernetes uses the List/Watch API and the Informer client library to monitor resources, compares direct HTTP Watch with Informer, provides Go code examples for pod controllers, shared informers, custom CRD controllers, and introduces higher‑level frameworks such as controller‑runtime and Kubebuilder.
The article begins by describing Kubernetes as a container orchestration system and introduces the controller concept, where a controller watches resources and reconciles the actual state to the desired state.
It details the HTTP List and Watch mechanisms, showing how to list pods via GET /api/v1/namespaces/default/pods and watch changes with the ?watch=true query parameter, including sample curl commands and raw JSON event streams (ADDED, MODIFIED, DELETED).
Next, a minimal Go program that directly uses the HTTP API to watch pods is presented, followed by a more idiomatic controller built with the Kubernetes Go client, which creates a watcher , processes events in a work queue, and logs pod lifecycle events.
The Informer mechanism is then introduced as a higher‑level abstraction that caches resources locally, reduces API‑server load, improves performance, and simplifies controller code. The article outlines the internal components (Reflector, FIFO queue, Indexer) and the typical event‑handling flow.
SharedInformer is explained as a way to share a single List/Watch call and cache among multiple controllers, with code showing how to create a shared informer factory, register event handlers, and start the informer.
For custom resources, the article shows how to define a CRD (Foo), generate clientset, informers, and listers using k8s.io/code-generator , register the types in a scheme, and write a controller that watches Foo objects using the generated informer.
Finally, the article mentions higher‑level frameworks—controller‑runtime and Kubebuilder—that build on informers to provide leader election, reconciliation loops, and code generation, offering an even more streamlined way to develop Kubernetes operators.
Cloud Native Technology Community
The Cloud Native Technology Community, part of the CNBPA Cloud Native Technology Practice Alliance, focuses on evangelizing cutting‑edge cloud‑native technologies and practical implementations. It shares in‑depth content, case studies, and event/meetup information on containers, Kubernetes, DevOps, Service Mesh, and other cloud‑native tech, along with updates from the CNBPA alliance.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.