DEVOURED
How the controller-runtime Cache Actually Works, and Why Your Controller Does Not Crash the API Server
Kubernetes controllers rely on an in-memory cache populated by list-and-watch, making local reads fast but requiring careful management of consistency and memory.
What: The article explains that controller-runtime avoids overloading the API server by reading from an internal cache rather than performing live queries. It highlights the risks of this design, including stale reads and high memory consumption, and provides tips for indexing fields to optimize search performance.
Why it matters: This deep dive demystifies the 'magic' of Kubernetes reconciliation, emphasizing that understanding memory consumption and index performance is critical for scaling controllers to manage large object sets.
Takeaway: If your controller performs complex lookups, register a custom `IndexField` with the manager at startup instead of iterating over every object in the store.
Deep dive
- Cache architecture: Controllers read from an indexed in-memory store, not directly from the API server.
- Performance impact: Large volumes of resources (e.g., Pods/Events) can consume gigabytes of memory; use
TransformorByObjectto filter data. - Concurrency:
r.Updategoes to the API server and usesresourceVersionfor optimistic locking, protecting against race conditions. - Indexing: Use
IndexFieldto create inverted indexes for efficient lookups by specific fields (e.g., node name). - Event handlers: Predicates and handlers receive pointers to objects in the shared cache; you MUST call
DeepCopy()before mutating them to avoid polluting other controllers.
Decoder
- List-and-watch: A Kubernetes pattern where a client fetches an initial snapshot (list) and then processes a continuous stream of changes (watch) to keep a local state in sync.
- Optimistic concurrency control: A mechanism where updates include a version number; if the server's version has changed since your read, the update is rejected.
Original article
Full article content is not available for inline reading.
Explore all 49 articles in today's digest
Fresh dev news, AI-digested daily. Never miss a trend at devoured.fyi