Stage 5 · Platform
Operators, CRDs & Controllers
Operator SDK
Kubebuilder, controller-runtime, envtest, RBAC markers, and generated manifests.
Operator SDK Overview
Operator SDK provides tools for building Kubernetes operators. Kubebuilder scaffolds the project structure, controller-runtime provides the framework, and envtest enables integration testing. Together they eliminate boilerplate and enforce patterns.
# Initialize a new project
operator-sdk init \
--domain=example.com \
--repo=github.com/my-org/my-operator
# Create a new API (CRD + controller)
operator-sdk create api \
--group=app \
--version=v1 \
--kind=WebApp \
--resource=true \
--controller=true
# Generate CRD manifests
make generate
make manifestsoperator-sdk init creates the project structure. create api generates the CRD type and controller. generate creates deepcopy methods. manifests generates CRD YAML and RBAC rules.
Kubebuilder Scaffolding
Kubebuilder scaffolds the project with standard patterns: api/ for CRD types, internal/controller/ for controllers, config/ for Kubernetes manifests, and hack/ for build scripts. Follow these conventions for maintainable operators.
my-operator/
api/
v1/
webapp_types.go # CRD type definitions
groupversion_info.go # API group registration
internal/
controller/
webapp_controller.go # Reconciler logic
config/
crd/bases/ # Generated CRD YAML
rbac/ # Generated RBAC rules
manager/ # Manager deployment
samples/ # Sample CRs
hack/
boilerplate.go.txt # License header
Dockerfile
Makefile
PROJECT # Kubebuilder metadataEach directory has a purpose. api/ defines the types. internal/controller/ implements the logic. config/ contains deployment manifests. The PROJECT file tracks scaffolding metadata.
Controller-Runtime
Controller-runtime is the framework for building controllers. It provides: Manager (handles lifecycle), Reconciler (business logic), Informers (watch resources), and Client (CRUD operations). It's the foundation of Kubebuilder operators.
envtest Testing
envtest provides a real API server and etcd for testing controllers. It creates a temporary cluster, installs CRDs, and runs tests. This catches issues that unit tests miss.
RBAC Markers
RBAC markers generate ClusterRole rules from comments. Add markers to the Reconciler and make manifests generates the RBAC YAML. This keeps RBAC in sync with actual controller needs.
Generated Manifests
# Generate deepcopy methods
make generate
# Generate CRD and RBAC manifests
make manifests
# Build and push the operator image
make docker-build docker-push IMG=ghcr.io/my-org/my-operator:v1
# Deploy to cluster
make deploy IMG=ghcr.io/my-org/my-operator:v1
# Undeploy
make undeploymake generate creates DeepCopy methods. make manifests generates CRD YAML and RBAC rules. make deploy applies the generated manifests to the cluster.
Package your operator as an OLM (Operator Lifecycle Manager) bundle for OperatorHub. This enables one-click installation in OpenShift and Kubernetes clusters. Use operator-sdk bundle create to generate the bundle.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.