- Add a helper named
serviceForthat builds a*corev1.Servicefor a givenWebApp. - Wire that helper into the reconciler so the controller creates a Service when it does not exist.
NotFound. This is the same pattern used for ConfigMaps and Deployments.
Context — existing ConfigMap helper
- Use the same
applabel as the other children so the Service can select the same pods. - Set Service
TypetoClusterIP. - Expose port
80and route it to container port80with 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.- Build the desired object (
serviceFor). - Attach a controller owner reference with
controllerutil.SetControllerReference. - Attempt to
Getthe object. - If
Getreturnsapierrors.IsNotFound,Createthe object. - Handle other errors appropriately.
WebApp, ensures ConfigMap and Service exist):
app label to list the Deployment, Service, and ConfigMap created by a single WebApp resource:
- Kubernetes Services overview
- controller-runtime: OwnerReferences and controllerutil
- Kubernetes Labels and Selectors
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.