Skip to main content
The web app controller already creates a Deployment and a ConfigMap. The final child we need is a Service, which gives the pods a stable cluster-internal DNS name and a single network entry point for other components to reach the WebApp. In this lesson we will:
  • Add a helper named serviceFor that builds a *corev1.Service for a given WebApp.
  • Wire that helper into the reconciler so the controller creates a Service when it does not exist.
Below are the authoritative code examples and the reconcile pattern used by the controller. Why this matters: A Kubernetes Service provides stable networking for a set of pods selected by labels. A controller must construct the desired Service, attach an owner reference (so child resources are garbage-collected when the parent is removed), check if the Service already exists, and create it if the API returns NotFound. This is the same pattern used for ConfigMaps and Deployments. Context — existing ConfigMap helper
Add the Service helper
  • Use the same app label as the other children so the Service can select the same pods.
  • Set Service Type to ClusterIP.
  • Expose port 80 and route it to container port 80 with TCP.
The Service selector must exactly match the labels applied to the Deployment’s Pod template. If these labels differ, the Service will be present but will not route traffic to any pods.
Wire the Service creation into Reconcile Follow the same reconcile pattern used for the ConfigMap and Deployment:
  1. Build the desired object (serviceFor).
  2. Attach a controller owner reference with controllerutil.SetControllerReference.
  3. Attempt to Get the object.
  4. If Get returns apierrors.IsNotFound, Create the object.
  5. Handle other errors appropriately.
Complete reconcile snippet (reads the WebApp, ensures ConfigMap and Service exist):
Build and run the controller
Typical startup logs
Apply your sample custom resource
Verify the children were created Use the app label to list the Deployment, Service, and ConfigMap created by a single WebApp resource:
If you see entries for the Deployment, Service, and ConfigMap, the controller successfully executed the reconcile pattern: build desired objects, attach owner references, check for existence, and create missing children. Resources and references Summary table This completes adding a Service builder and wiring it into the reconcile loop so the WebApp controller manages Service lifecycle along with its other children.

Watch Video