controller-runtime: The Engine for Kubernetes Operators
Think of controller-runtime as the standard library for writing Kubernetes controllers. It handles the boilerplate of watching resources and reconciling state, forming the foundation for tools like Kubebuilder and Operator SDK.
Why it exists
Writing a Kubernetes controller from scratch is complex. You need to handle watching API resources, managing a local cache to avoid overwhelming the API server, queuing work items, and ensuring only one controller instance is active (leader election). controller-runtime was created to solve this by providing robust, reusable components for this common logic.
The mental model
controller-runtime is the standard engine block for a custom Kubernetes controller. Instead of building the pistons, crankshaft, and transmission (API watches, caches, work queues) yourself, you get a pre-built engine. You just need to add your specific business logic—the 'car' built around the engine—that defines how to reconcile a specific resource.
How it works
The library provides several key abstractions. A 'Manager' sets up shared components like connections to the API server, caches, and leader election. You then attach one or more 'Controllers' to the Manager. Each Controller specifies which resources to 'Watch' (e.g., Deployments, or a custom resource) and provides a 'Reconcile' function. This function contains your logic and is called whenever a watched object changes. The library handles the event-driven plumbing to trigger your reconcile loop.
When to use it
You use controller-runtime when building custom Kubernetes controllers or operators in Go. It's the de-facto standard and is the underlying library for major tools like Kubebuilder and the Operator SDK. If you need fine-grained control that a higher-level framework doesn't expose, you'll work with its APIs directly.
When not to use it
Don't use it for simple, one-off scripts that interact with the Kubernetes API; a client library like client-go is more appropriate. For most new operator projects, starting with a higher-level framework like Kubebuilder or Operator SDK is recommended, as they provide scaffolding and code generation on top of controller-runtime.
One canonical example
A database operator defines a 'Database' Custom Resource. The controller, built with controller-runtime, watches for these objects. When a user creates a 'Database' object named 'my-prod-db', the reconcile loop is triggered. The loop's logic then creates a corresponding StatefulSet, Service, and Secret to run the actual database, bringing the cluster's state in line with the user's request.
Interview question
Which statement best describes the primary function of controller-runtime in Kubernetes operator development?
- a.It serves as a low-level client library for direct interaction with the Kubernetes API server.
- b.It is primarily used for deploying and managing existing applications within a Kubernetes cluster.
- c.It abstracts away common boilerplate like API watching, caching, and work queuing for custom controllers.Correct
- d.It provides a high-level framework for generating complete operator code from specifications.
Why? this is the answer
The card states controller-runtime "handles the boilerplate of watching resources and reconciling state" and provides "reusable components for this common logic" like API watches, caches, and work queues. Distractor A describes higher-level frameworks like Kubebuilder, which are built on top of controller-runtime, not controller-runtime itself.
Just read this? Test yourself on what you have been reading.
Read the original → github.com
- #kubernetes
- #go
- #operators
- #controller
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on kubernetes — each one lists the topics its interview covers.
See open roles