Skip to content

feat(client): patch the status instead of replacing it - #1351

Closed
kimpenhaus wants to merge 1 commit into
mainfrom
feat/status-patch
Closed

kimpenhaus wants to merge 1 commit into
mainfrom
feat/status-patch

Conversation

@kimpenhaus

Copy link
Copy Markdown
Collaborator

Summary

UpdateStatusAsync replaces the status with a PUT. The request carries the resourceVersion and fails with 409 Conflict whenever the entity changed in the meantime, and the field manager owns the whole status. This adds PatchStatusAsync, which writes only the changed status fields as a JSON merge patch to the /status subresource.

  • PatchStatusAsync(entity, change) applies change to the entity (in place) and patches only the fields it touched. One request, no GET, no resourceVersion:
    entity = await client.PatchStatusAsync(entity, e => e.Status.Phase = "Ready", cancellationToken);
  • PatchStatusAsync(from, to) patches the differences between the status of two entities.
  • PatchStatusAsync<TEntity>(patch, name, namespace, fieldManager, force, dryRun) sends an own V1Patch to /status, e.g. a server-side apply patch.
  • Removed fields are sent as null; a changed array (e.g. conditions) is sent as a whole and replaces entries others added. For lists shared by several writers, server-side apply is the right tool; this is documented.
  • Without a status change, no request is sent and the entity is returned.
  • UpdateStatusAsync is unchanged, for writes that must fail on an outdated resourceVersion.
  • RBAC: the generated rule for <plural>/status already contains patch.

Not breaking

All new IKubernetesClient members have default implementations, so own implementations of the interface keep compiling. KubernetesClient overrides the V1Patch overload for the disposal check and its metadata cache; both share the request in the internal StatusPatch.SendAsync.

Docs

  • building-blocks/controllers.mdx: example uses PatchStatusAsync; new section Writing the Status (merge patch semantics, arrays, when to use UpdateStatusAsync).
  • troubleshooting.mdx: hint on PatchStatusAsync in the 409 Conflict section.
  • KubeOps.KubernetesClient README: status example.
  • migration.mdx: New in v15 (non-breaking) entry.

Tests

  • StatusPatch.Test.cs: merge patch of changed, removed, nested and array fields; changes outside the status are ignored.
  • KubernetesClient.PatchStatus.Test.cs: path, content type and body for namespaced and cluster-scoped entities, no request without changes, server-side apply with fieldManager/force.
  • KubernetesClient.FieldManager.Test.cs: the status patch carries the field manager; moved onto the shared StubApiServer.
  • KubernetesClient.PatchStatus.Integration.Test.cs (cluster): UpdateStatusAsync with an outdated entity fails with 409, PatchStatusAsync succeeds and keeps the spec change of the other writer. Not run locally (no cluster), runs in CI.

Reviews

  • /code-review: replaced a PatchStatusAsync(entity) overload that diffed against a fresh GET and therefore silently wrote outdated fields back; documented that arrays are replaced as a whole. Pre-existing, not changed here: the status of core group types (/apis//v1/..., also UpdateStatusAsync).
  • /simplify: default implementation instead of an abstract member (no breaking change), shared request code, compare before clone in the merge diff, shared test stub, trimmed docs. Skipped: moving the merge diff into KubeOps.Abstractions as public API.
  • /security-review: no findings. Name and namespace are path parameters of the official client; the patch always has the form {"status": …} and goes to /status only.

Note: StubApiServer.cs is identical to the one in #1350, so both PRs merge without conflict. migration.mdx will need a trivial merge with #1348 (both add New in v15).

PatchStatusAsync(entity, change) applies the change and patches only the status fields it touched, PatchStatusAsync(from, to) the differences between two entities. Both send a JSON merge patch to the /status subresource without a resourceVersion, so they do not fail with 409 Conflict when the entity changed in the meantime. PatchStatusAsync(patch, name, namespace, ...) sends an own patch, e.g. server-side apply. UpdateStatusAsync is unchanged. All new members have default implementations, so IKubernetesClient stays non-breaking.
kimpenhaus added a commit that referenced this pull request Nov 25, 2026
## Summary

#1047 (v10.3.0) added `fieldManager`, `force` and `dryRun` to every
`PatchAsync` overload. Three of them always send a JSON patch:

- `PatchAsync(entity, operationsFilter, fieldManager, force, dryRun)`
- `PatchAsync(from, to, operationsFilter, fieldManager, force, dryRun)`
- `PatchAsync(entity, JsonPatch, fieldManager, force, dryRun)`

The API server rejects `force` for every patch type except apply, even
`force: false`
([`ValidatePatchOptions`](https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/validation/validation.go)):

```go
default:
    if options.Force != nil {
        allErrs = append(allErrs, field.Forbidden(field.NewPath("force"), "may not be specified for non-apply patch"))
    }
```

So `force` on these overloads could only ever produce `422 Unprocessable
Entity`. It is removed there; the `V1Patch` overloads keep it for
server-side apply.

Also fixed:
- Docs of `fieldManager` (applies to every patch, not only server-side
apply) and `force` (only valid for `ApplyPatch`).
- The server-side apply example in the `KubeOps.KubernetesClient` README
did not compile (missing comma) and passed namespace and name in the
wrong order.

## Breaking change

The three JSON patch overloads no longer take `force`. A call that
passes it fails to compile instead of failing with 422 at runtime; no
working call changes behaviour. Documented in `migration.mdx` (v15),
with server-side apply as the replacement.

## Checks

- Release build without warnings, `dotnet format` clean, unit tests
`KubeOps.KubernetesClient.Test` (84) and `KubeOps.Operator.Web.Test`
(172) green; docs build green.
- `/code-review`, `/simplify`, `/security-review` done by hand for the
small diff: positional forwarding between the overloads checked, no new
overload ambiguity, no findings.

Note: touches the same `PatchAsync` lines and `operationsFilter` docs as
#1350, and `migration.mdx` like #1348/#1351; whichever merges later
needs a small rebase.
@kimpenhaus

Copy link
Copy Markdown
Collaborator Author

Superseded: instead of new PatchStatusAsync overloads, UpdateStatusAsync itself will write the status with server-side apply (no new API). A new PR follows.

@kimpenhaus kimpenhaus closed this Nov 25, 2026
@kimpenhaus
kimpenhaus deleted the feat/status-patch branch November 25, 2026 23:14
kimpenhaus added a commit that referenced this pull request Nov 27, 2026
## Summary

`UpdateStatusAsync` replaced the status with a `PUT`. The request
carried the `resourceVersion` and failed with `409 Conflict` whenever
the resource had changed in the meantime, e.g. its spec, and the field
manager owned the whole status.

`UpdateStatusAsync` now writes the status with **server-side apply**,
without a new method:

- The apply configuration contains `apiVersion`, `kind`,
`metadata.name`, `metadata.namespace` (if any) and `status`, and goes to
the `/status` subresource.
- It carries no `resourceVersion`, so an outdated entity no longer fails
with 409. The spec and the status fields that are not sent stay as they
are.
- `force: true`, as the Kubernetes docs recommend for controllers ("It
is strongly recommended for controllers to always force conflicts on
objects that they own and manage"). Without it, the first write after
the upgrade would conflict with the operator's own `Update` entry from
the `PUT`.
- The field manager comes from the client, as for every other write: the
`FieldManagerHandler` of the operator, or the one passed to `new
KubernetesClient(config, fieldManager)`. There is no fallback; without a
field manager the API server rejects the apply.

Supersedes #1351, which added `PatchStatusAsync` overloads instead.

## Breaking change

- `UpdateStatusAsync` no longer fails with 409 on an outdated entity.
- It needs a field manager. Within an operator with the default
`IKubernetes` nothing changes; an own `IKubernetes` needs the
`FieldManagerHandler` (already documented since 14.1); a
`KubernetesClient` outside an operator needs to be created with a field
manager.
- Status fields written before the upgrade stay with the `Update` entry
of the `PUT` until they are sent again.

All documented in `migration.mdx` (v15).

## Tests

- `KubernetesClient.UpdateStatus.Test.cs`: path with `?force=true`,
`application/apply-patch+yaml`, and the apply configuration for
namespaced and cluster-scoped entities.
- `KubernetesClient.FieldManager.Test.cs`: the status write is now a
`PATCH` with the field manager.
- `KubernetesClient.UpdateStatus.Integration.Test.cs` (cluster): an
outdated entity is written without 409, the spec change of the other
writer is kept, and `managedFields` shows `Apply` on `status`. Two
existing integration tests now use a client with a field manager. Not
run locally (no cluster), runs in CI.

## Docs

`migration.mdx`, `troubleshooting.mdx` (409 only for `UpdateAsync`),
`field-manager.mdx`, `KubeOps.KubernetesClient` README.

## Reviews

- `/code-review`: the reconcile pipeline already loads the entity fresh
before each reconcile and skips a recreated object
(`EntityQueueBackgroundService`), so a stale status is limited to a
running reconcile. Documented the upgrade case (fields owned by the old
`Update` entry) and the own-`IKubernetes` case. Not changed: core group
types (`/apis//v1/...`, pre-existing, tracked in the roadmap); a type
with a `null` status applies an empty status.
- `/simplify` and `/security-review` by hand: no new type or method;
name and namespace are escaped path parameters of the official client,
the body is a `JsonObject`; `force` only affects fields of our own
status, and the generated RBAC rule for `<plural>/status` already
contains `patch`.

Note: `migration.mdx`, `field-manager.mdx` and the client README are
also changed by #1348; whichever merges second needs a small rebase,
which I will do.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant