
Add imports
Open the controller file and add imports for the built-in Kubernetes types we’ll use. These provide the Deployment, Pod template types, and metadata helpers.Reconcile skeleton and helper signature
Keep the Reconcile skeleton that reads the WebApp resource. We’ll add a helper nameddeploymentFor that builds the Deployment object for a given *webappv1.WebApp.
Build labels
Use a Go map to hold labels that will be applied to both the Deployment selector and the pod template. The selector and template labels must match so the Deployment can find and manage its Pods.Ensure the Deployment selector and the Pod template labels are identical. If they differ, the Deployment will not manage the Pods you expect.
Replicas as a pointer
Kubernetes expectsspec.replicas to be an optional pointer (*int32). Create a local variable, then pass its address into the Deployment spec.
replicas must be passed as a pointer (e.g., Replicas: &replicas) because the API type defines it as *int32. Using a plain numeric literal will cause compile-time errors.Build the Deployment struct
Create theappsv1.Deployment struct literal with metadata, selector, and a Pod template. The Pod template contains a corev1.PodSpec with a single container named web. The container image comes from the WebApp spec and we expose port 80 inside the Pod.
Types used
Build and verify
Rungo build ./... in your module root. A clean build (no compiler errors) verifies imports, field names, and pointer usage. If the build fails, check for:
- Missing imports (e.g.,
metav1,appsv1,corev1) - Incorrect field names or types
- Passing a non-pointer where a pointer is required
deploymentFor from Reconcile and create or update the Deployment via the controller-runtime client.