Sub topic 13
Building with kubebuilder and controller-runtime
This chapter assumes you know what a CRD is, how a reconcile loop works, and basic Go. You will scaffold a project and see which parts you write and which the tools generate.
The two pieces
controller-runtime is a Go library. It provides the manager, the cached client, and the controller machinery that calls your Reconcile function. kubebuilder is a command-line scaffolding tool built on it. It generates a project layout, a Makefile, and CRD manifests from your Go types. Operator SDK uses the same libraries and adds packaging features.
Scaffold a project
kubebuilder init --domain example.com --repo example.com/webapp-operator
kubebuilder create api --group web --version v1 --kind WebApp --resource --controllerThe API group is the group plus the domain, so this creates web.example.com. Earlier chapters used example.com alone, so adjust the names if you follow along.
The generated files that matter most:
api/v1/webapp_types.goholds yourSpecandStatusstructs.internal/controller/webapp_controller.goholds the reconciler.cmd/main.gocreates the manager and registers controllers.config/holds the generated CRD, RBAC and deployment manifests.
Define types with markers
Comments that start with +kubebuilder: are markers. controller-gen reads them to build the schema.
type WebAppSpec struct {
// +kubebuilder:validation:MinLength=1
Image string `json:"image"`
// +kubebuilder:validation:Minimum=0
// +kubebuilder:default=1
Replicas int32 `json:"replicas,omitempty"`
}
type WebAppStatus struct {
ReadyReplicas int32 `json:"readyReplicas,omitempty"`
}
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
type WebApp struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec WebAppSpec `json:"spec,omitempty"`
Status WebAppStatus `json:"status,omitempty"`
}Above Reconcile, RBAC markers produce the operator's permissions:
// +kubebuilder:rbac:groups=web.example.com,resources=webapps,verbs=get;list;watch;update;patch
// +kubebuilder:rbac:groups=web.example.com,resources=webapps/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;deleteThe development loop
make generate # regenerate DeepCopy methods
make manifests # regenerate CRD and RBAC YAML from markers
make install # apply the CRD to your current cluster
make run # run the manager locally, using your kubeconfigCommon errors
- Stale generated files. After you edit types or markers, run
make generate manifests. Otherwise the cluster keeps the old schema. - Missing RBAC markers. The operator works with
make run, which uses your admin credentials, then fails withforbiddenonce deployed. Add a marker for every resource you read or write. - Missing
Owns(...). Without it, changes to a child Deployment do not trigger a reconcile. - Reading right after writing. The client reads from a cache, so a read just after a write can return old data. Return
ctrl.Result{Requeue: true}or rely on the next event. - Conflict errors on status updates. The error
the object has been modifiedmeans your copy was stale. Return the error and the queue retries.
Exercise
You add an Owns(&appsv1.Deployment{}) call and your operator runs fine locally. After deploying it in a cluster, it logs forbidden when listing Deployments. What did you most likely forget, and which command regenerates the fix?
Show answer
You forgot the +kubebuilder:rbac marker for deployments. Locally, make run used your own kubeconfig credentials. Add the marker above Reconcile, then run make manifests to regenerate the RBAC role and redeploy.