Repository navigation
feat(client): patch the status instead of replacing it - #1351
Closed
kimpenhaus wants to merge 1 commit into
Closed
kimpenhaus wants to merge 1 commit into
kimpenhaus wants to merge 1 commit into
Conversation
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.
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
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.
This was referenced Dec 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
UpdateStatusAsyncreplaces the status with aPUT. The request carries theresourceVersionand fails with409 Conflictwhenever the entity changed in the meantime, and the field manager owns the whole status. This addsPatchStatusAsync, which writes only the changed status fields as a JSON merge patch to the/statussubresource.PatchStatusAsync(entity, change)applieschangeto the entity (in place) and patches only the fields it touched. One request, noGET, noresourceVersion:PatchStatusAsync(from, to)patches the differences between the status of two entities.PatchStatusAsync<TEntity>(patch, name, namespace, fieldManager, force, dryRun)sends an ownV1Patchto/status, e.g. a server-side apply patch.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.UpdateStatusAsyncis unchanged, for writes that must fail on an outdatedresourceVersion.<plural>/statusalready containspatch.Not breaking
All new
IKubernetesClientmembers have default implementations, so own implementations of the interface keep compiling.KubernetesClientoverrides theV1Patchoverload for the disposal check and its metadata cache; both share the request in the internalStatusPatch.SendAsync.Docs
building-blocks/controllers.mdx: example usesPatchStatusAsync; new section Writing the Status (merge patch semantics, arrays, when to useUpdateStatusAsync).troubleshooting.mdx: hint onPatchStatusAsyncin the 409 Conflict section.KubeOps.KubernetesClientREADME: 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 withfieldManager/force.KubernetesClient.FieldManager.Test.cs: the status patch carries the field manager; moved onto the sharedStubApiServer.KubernetesClient.PatchStatus.Integration.Test.cs(cluster):UpdateStatusAsyncwith an outdated entity fails with 409,PatchStatusAsyncsucceeds and keeps the spec change of the other writer. Not run locally (no cluster), runs in CI.Reviews
/code-review: replaced aPatchStatusAsync(entity)overload that diffed against a freshGETand 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/..., alsoUpdateStatusAsync)./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 intoKubeOps.Abstractionsas 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/statusonly.Note:
StubApiServer.csis identical to the one in #1350, so both PRs merge without conflict.migration.mdxwill need a trivial merge with #1348 (both add New in v15).