Sub topic 11
Reporting state with status and conditions
This chapter assumes you know the spec/status split, have read the status subresource chapter, and can read the WebApp reconciler from earlier. Go helps for the code.
Why status needs structure
spec holds what the user wants. status holds what the controller observed. A single field such as readyReplicas tells you a number but not whether the app is healthy, still rolling out, or broken. Tools and people need a shared way to read that. Kubernetes uses conditions for this.
A condition is one entry in a status.conditions list. It describes one aspect of the object, such as Available or Progressing.
| Field | Meaning |
|---|---|
| `type` | The aspect being reported, in CamelCase, such as `Ready`. |
| `status` | `True`, `False` or `Unknown`. |
| `reason` | A short CamelCase machine-readable cause, such as `DeploymentUnavailable`. |
| `message` | A human-readable explanation. |
| `observedGeneration` | The `metadata.generation` this condition was computed from. |
| `lastTransitionTime` | When `status` last changed. |
Add conditions to the API type
Use the standard metav1.Condition type. That gives you the fields above and the schema markers that kubebuilder needs.
type WebAppStatus struct {
ReadyReplicas int32 `json:"readyReplicas,omitempty"`
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
Conditions []metav1.Condition `json:"conditions,omitempty"`
}Set conditions in the reconciler
The helper meta.SetStatusCondition adds or updates a condition by type. It changes lastTransitionTime only when status changes, so repeated reconciles do not create noise.
package controllers
import (
"context"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
examplev1 "example.com/webapp/api/v1"
)
func (r *WebAppReconciler) updateStatus(ctx context.Context, app *examplev1.WebApp, deploy *appsv1.Deployment) error {
cond := metav1.Condition{
Type: "Available",
Status: metav1.ConditionTrue,
Reason: "MinimumReplicasReady",
Message: "The Deployment has the requested replicas ready",
ObservedGeneration: app.Generation,
}
if deploy.Spec.Replicas == nil || deploy.Status.ReadyReplicas < *deploy.Spec.Replicas {
cond.Status = metav1.ConditionFalse
cond.Reason = "ReplicasNotReady"
cond.Message = "Waiting for the Deployment to become ready"
}
meta.SetStatusCondition(&app.Status.Conditions, cond)
app.Status.ReadyReplicas = deploy.Status.ReadyReplicas
app.Status.ObservedGeneration = app.Generation
return r.Status().Update(ctx, app)
}Call it at the end of Reconcile, in place of the plain Status().Update call.
Conventions that avoid common errors
- Write status through
r.Status(). A normalUpdateignoresstatusonce the status subresource is enabled. - Set
observedGeneration. Without it, a reader cannot tell whetherReady=Truerefers to the currentspecor an older one. - Make
reasonstable and CamelCase. Scripts match on it, so do not put variable text there. Put variable text inmessage. - Report current state, not history. A condition should be recomputed on every reconcile, not appended.
- Use
Unknownwhen you cannot tell, for example before the first reconcile. - Expect conflict errors. If another writer changed the object,
Status().Updatefails and the reconcile retries. This is safe because the reconciler is idempotent.
Consumers can then wait on a condition:
kubectl wait webapp/hello --for=condition=Available --timeout=60s
kubectl get webapp hello -o jsonpath='{.status.conditions[?(@.type=="Available")].reason}'Exercise
A user edits spec.image, and kubectl get immediately shows Available=True. Which status field lets you tell that this condition is stale, and how does it show that?
Show answer
observedGeneration. The edit increments metadata.generation. The condition's observedGeneration still holds the old value until the controller reconciles again, so a mismatch means the condition describes an older spec.