spec. The child Deployment reports how many replicas are actually ready, but that observed value was not surfaced on the parent WebApp resource. Today, an operator must inspect the child Deployment to see the observed readyReplicas.
This guide shows how to add an observed status field, readyReplicas, to the WebApp status and how to populate it from the child Deployment during reconciliation. The field belongs in status because it represents observed state rather than user intent.
Summary of changes
- Add
ReadyReplicas int32json:“readyReplicas,omitempty”toWebAppStatus`. - Ensure the
WebApptype includesSpecandStatusfields. - In the reconciler, copy
Deployment.Status.ReadyReplicasintoWebApp.Status.ReadyReplicas. - Only update the API server when the status value actually changes.
- Regenerate and install CRD manifests so the API server accepts the new status field.
- Update the API type
api/v1/webapp_types.go) and locate WebAppStatus. Initially the types looked like this:
ReadyReplicas as an int32 field with the JSON name readyReplicas. Kubernetes replica counts use 32-bit integers, so the custom status field should use int32. The JSON tag controls the field name stored by the API server, which is why users will later read status.readyReplicas.
Replace the type definitions with:
- Update the reconciler
Status.ReadyReplicas into the WebApp.Status.ReadyReplicas. Only call r.Status().Update when the value changes to avoid unnecessary writes and extra reconcile traffic.
A concise, correct Reconcile implementation demonstrating these steps:
- Status updates write to the API server and can trigger additional reconcile loops. Only updating when the observed value actually changes reduces noise and unnecessary API usage.
Use
r.Status().Update(ctx, &obj) to write only the status subresource. This keeps observed state separate from the desired state in spec, which users edit.- Regenerate and install CRDs
readyReplicas before the API server will persist that field.
- Run the manager and verify
WebApp, and wait for the Deployment to become available. Once the Deployment controller reports ready replicas, the WebApp reconciler will copy that value into status.
For example, after the Deployment becomes available you can read:
WebApp can answer this common operational question directly without requiring the operator to inspect the child Deployment.
References