Skip to main content
This lab completes Section Four (MVP). The WebApp controller will create three child resources for each WebApp instance:
  • Deployment
  • Service
  • ConfigMap
Each child resource will have an owner reference pointing back to the WebApp custom resource, enabling Kubernetes to garbage collect the children when their parent is deleted. Imports used by the controller:
Note the RBAC markers required by kubebuilder for the WebApp controller:
Overview of the reconcile pattern
  • Build the desired child object from the parent WebApp spec.
  • Set the WebApp as the owner of the child via controllerutil.SetControllerReference.
  • Try to Get the existing child.
    • If not found, Create the child.
    • If found, optionally Update if desired state differs.
  • Return any real errors encountered.
This pattern — build desired, set controller reference, get current, create-if-missing — is repeated for each child type (Deployment, Service, ConfigMap). Once you understand it for one resource, it applies to the others.
Start by setting the owner reference on the Deployment before attempting to create it:
The controller must check SetControllerReference for an error and return it if present. Handling errors and checking for existing Deployment:
Add the Service helper
  • The Service helper returns a pointer to a corev1.Service.
  • The Service selector must match the Pod template labels in the Deployment so traffic routes correctly.
  • Use corev1.ServiceTypeClusterIP and expose port 80 / target port 80.
  • Use intstr.FromInt(80) (or intstr.FromInt32(80)) to wrap the numeric target port.
Example signature and skeleton:
Service helper (full example includes metadata, labels and ports):
Add the ConfigMap helper
  • The ConfigMap name should be the WebApp name suffixed with -config.
  • The data is a Go map where the key is welcome.html and the value contains a small HTML string that can include the WebApp name.
Example ConfigMap helper:
Wiring all children in Reconcile
  • For each child type: build desired, set controller reference, get current, create if missing, and return errors when they happen.
  • This keeps the controller simple and consistent.
Example code for wiring the Service (pattern applies also to Deployment and ConfigMap):
Setup the controller with the manager:
Build and run the controller locally (before touching the cluster):
Open another terminal and verify created resources by label:
Check that owner references were added to each child. Each child should report kind: WebApp with controller: true. Sample WebApp CR to create:
Verify ownerReferences via jsonpath:
A full check showing the controller flag (example):
Quick reference: resource responsibilities Links and references
Ensure your controller has the proper RBAC permissions for creating and updating Deployments, Services, and ConfigMaps. Missing RBAC rules will surface as permission-denied errors when the controller attempts resource operations.
This pattern — repeated for Deployment, Service, and ConfigMap — makes the controller predictable and easy to extend. Later sections will build on this by adding updates, status updates, and more advanced reconciliation logic.

Watch Video