- Deployment
- Service
- ConfigMap
- Build the desired child object from the parent WebApp spec.
- Set the WebApp as the owner of the child via
controllerutil.SetControllerReference. - Try to
Getthe existing child.- If not found,
Createthe child. - If found, optionally
Updateif desired state differs.
- If not found,
- 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.
SetControllerReference for an error and return it if present.
Handling errors and checking for existing Deployment:
- 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.ServiceTypeClusterIPand expose port 80 / target port 80. - Use
intstr.FromInt(80)(orintstr.FromInt32(80)) to wrap the numeric target port.
- The ConfigMap name should be the WebApp name suffixed with
-config. - The data is a Go map where the key is
welcome.htmland the value contains a small HTML string that can include the WebApp name.
- 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.
kind: WebApp with controller: true.
Sample WebApp CR to create:
controller flag (example):
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.