ReadyReplicas into the parent status. This is a common pattern for Kubernetes operators built with controller-runtime.
Summary of the change
- Read the live child Deployment from the API server.
- Copy
dep.Status.ReadyReplicasintowebapp.Status.ReadyReplicas. - Persist that observation with
r.Status().Update(ctx, &webapp).
- The status subresource represents the controller’s observed state. By updating
statuswith the child’s observed values, users can inspect the parent resource alone to determine runtime health without inspecting children directly. - Use the status client (
r.Status()) rather thanr.Updateto avoid unintentionally changing spec fields.
- The controller first reads the parent WebApp instance. If it no longer exists, reconciliation ends.
- It then ensures the desired child Deployment and Service exist, creating them if necessary and setting owner references so garbage collection and controller-runtime can manage lifecycle.
- The live child Deployment is read from the API server and acts as the source of truth for
ReadyReplicas. - If reading the Deployment fails, the Reconcile returns the error, causing controller-runtime to retry. The controller cannot truthfully report readiness without reading the child.
- Status updates must use the status client:
r.Status().Update(...). The status subresource is for the controller’s observed state and is distinct from spec fields that users edit.
Always use
r.Status().Update (the status client) to change .status fields. Using r.Update modifies the whole object and is intended for spec changes.
Run and validate locally
- Build and run your manager locally to validate compilation and behavior.
- Sample manager logs (trimmed):
- In another terminal, apply the sample WebApp and wait for the child Deployment to become available:
- The Deployment controller reports
ReadyReplicasfirst. The WebApp controller reads that observed value and publishes it onto the parent WebApp status. - After reconciliation,
webapp.status.readyReplicasshould match the Deployment’s ready replica count (for example,2), allowing users to inspect the parent resource alone for availability information.
- controller-runtime: https://pkg.go.dev/sigs.k8s.io/controller-runtime
- Kubernetes API concepts: https://kubernetes.io/docs/concepts/overview/