Stage 7 · Master
Self-Service Platform API & CLI
Kubernetes Controllers for Platform
Build controllers with controller-runtime: reconcile loops, finalizers, status conditions, and webhook validation.
Controller Pattern
Kubernetes controllers implement the control loop: observe actual state, compare to desired state, take action to converge. Platform controllers manage custom resources (Service, Database, Environment) and orchestrate infrastructure.
Reconcile Loop
Finalizers
Finalizers prevent resource deletion until cleanup completes. Platform resources (databases, DNS, certificates) need explicit cleanup.
Status Conditions
Kubernetes-style conditions provide structured, machine-readable status. Each condition has: type, status (True/False/Unknown), reason, message, lastTransitionTime.
| Condition Type | When True | When False | Reason Examples |
|---|---|---|---|
| Provisioned | Infrastructure created | Not yet created / failed | Provisioned, ProvisionFailed |
| PolicyCompliant | Passes all policies | Policy violation | PolicyViolation, PolicyUpdated |
| Deployed | ArgoCD synced & healthy | Sync failed / degraded | Deployed, SyncFailed, Degraded |
| Ready | All conditions True | Any condition False | Ready, NotReady |
| DeletionFailed | Cleanup failed | Cleanup succeeded | DeletionFailed, Deleted |
Admission Webhooks
Validating webhooks reject invalid resources at admission time. Mutating webhooks can set defaults. Both run before controller sees the resource.
Testing Controllers
- Unit tests: Test reconcile logic with fake client (controller-runtime/pkg/client/fake)
- Integration tests: envtest (spins up real API server + etcd in memory)
- Webhook tests: Test admission.Decoder with valid/invalid objects
- E2E tests: Kind cluster, deploy CRD + controller, create resources, assert state
- Chaos tests: Kill controller mid-reconcile, verify idempotency and finalizer cleanup
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.