Sub topic 7
The status subresource
This chapter assumes you can read a CRD manifest, run kubectl apply, and know the spec/status split from built-in objects like Deployments. You should also know what a structural schema is.
Why status needs its own endpoint
By convention, spec is the desired state that users write, and status is the observed state that controllers write. Without extra configuration, a custom resource is one object. Anyone who can update it can change both fields, and every write bumps metadata.generation.
Enabling the status subresource splits the object into two endpoints:
PUT /apis/example.com/v1/namespaces/default/webapps/helloignores changes tostatus.PUT /apis/example.com/v1/namespaces/default/webapps/hello/statusignores changes to everything exceptstatus.
This gives you two benefits. RBAC can grant users write access to the main resource and give the operator write access to webapps/status. Also, metadata.generation only increases when something other than status changes, so a controller can tell a user edit from its own status write.
Enabling it
Add subresources.status to each version in the CRD. The WebApp CRD from this tutorial already does this:
versions:
- name: v1
served: true
storage: true
subresources:
status: {}
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
image:
type: string
status:
type: object
properties:
readyReplicas:
type: integerThe empty object {} is the whole setting. The schema still has to declare the status fields you want to keep. Fields missing from a structural schema are pruned.
What changes for users
After you enable the subresource, kubectl apply with a status block no longer stores that block. The API server drops it silently. This surprises people who try to hand-edit status for testing. To write it, use the subresource:
kubectl patch webapp hello --subresource=status --type=merge \
-p '{"status":{"readyReplicas":2}}'Reading is unchanged: kubectl get webapp hello -o jsonpath='{.status.readyReplicas}'.
What changes for the controller
In the Go reconciler, status is written through a separate client:
app.Status.ReadyReplicas = deploy.Status.ReadyReplicas
return ctrl.Result{}, r.Status().Update(ctx, &app)r.Update would not save the status change. r.Status().Update saves only status and fails on a stale resourceVersion with a conflict error, which the controller handles by requeueing.
Common errors
- Status updates do nothing. The CRD lacks
subresources.status, or the code callsr.Updateinstead ofr.Status().Update. Forbiddenwhen updating status. The operator's role needs thewebapps/statusresource with theupdateorpatchverb. Permission onwebappsalone is not enough.- Status disappears after apply. A manifest applied with
kubectl applycannot carry status, so keep it out of your manifests.
Exercise
A controller calls r.Update(ctx, &app) after setting app.Status.ReadyReplicas. The CRD has subresources.status: {}. Is the new value stored? What call fixes it?
Show answer
No. With the status subresource enabled, the main endpoint ignores changes to status. Use r.Status().Update(ctx, &app), or a status patch, and make sure the role allows update on webapps/status.