Skip to main content
The WebApp custom resource accepts a desired replica count in 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 int32 json:“readyReplicas,omitempty”toWebAppStatus`.
  • Ensure the WebApp type includes Spec and Status fields.
  • In the reconciler, copy Deployment.Status.ReadyReplicas into WebApp.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.
Files touched
  1. Update the API type
Open the WebApp API (typically api/v1/webapp_types.go) and locate WebAppStatus. Initially the types looked like this:
Add 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:
  1. Update the reconciler
The reconciler already ensures the child Deployment exists. After you have the existing Deployment in memory, copy its 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:
Why the guard matters
  • 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.
If the status update fails, return the error so controller-runtime retries the reconcile.
  1. Regenerate and install CRDs
Because the Go type is only the source of truth during code generation, regenerate the CRD manifests and install the CRD so the API server knows about the new field. The CRD schema must include readyReplicas before the API server will persist that field.
  1. Run the manager and verify
Start your manager locally, apply the namespace and a sample 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:
That number came from the child Deployment, but it is now visible on the parent resource. The WebApp can answer this common operational question directly without requiring the operator to inspect the child Deployment. References

Watch Video