Repository navigation
feat(operator): write with an explicit field manager configured per operator - #1346
Merged
Merged
Conversation
…perator Every POST, PUT and PATCH the operator sends to the API server now carries a fieldManager, so its writes are recorded in metadata.managedFields under the operator name instead of kubernetes-csharp. - OperatorSettings.FieldManager, set with WithFieldManager, defaults to the operator name and is validated like the API server does (at most 128 bytes, printable characters only) - public FieldManagerHandler adds the parameter unless the request already carries one or targets a connect subresource (proxy, exec, attach, portforward) - OperatorBuilder registers the default IKubernetes with the handler, which covers finalizers, events, CRD and webhook installation, leader election and application code - KubernetesClient gets a constructor that takes a field manager - the KubeOps CLI writes as kubeops-cli Closes #1345
kimpenhaus
force-pushed
the
feat/field-manager
branch
from
November 24, 2026 07:56
79054e4 to
900a891
Compare
kimpenhaus
enabled auto-merge (squash)
November 24, 2026 08:00
|
🎉 This PR is included in version 14.1.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
kimpenhaus
added a commit
that referenced
this pull request
Nov 28, 2026
## What The operator identifies itself to the Kubernetes API server with its own name instead of the generic user agent of the C# client: ```text # before kubernetes-csharp/21.0.8+75f253d651 # after my-operator/1.2.0 kubeops/15.0.0 kubernetes-csharp/21.0.8+75f253d651 ``` Build metadata (`+<commit>`) is dropped from the operator and KubeOps versions; the C# client token stays as the client sends it. The API server records the user agent in its audit logs, so requests of an operator can now be told apart from those of other clients built on the C# client. This complements the explicit field manager from #1346: the API server derives the field manager of a write without `fieldManager` from the part of the user agent before the first `/`. The operator always sends an explicit field manager, so `managedFields` do not change. ## How - **`KubernetesUserAgent`** (public static class in `KubeOps.KubernetesClient`) with two members: - `Create(product, version)` returns `<product>[/<version>] kubeops/<version> <client default>`. Characters that are not allowed in an HTTP product token (RFC 9110 `tchar`), e.g. spaces, `/` or CR/LF, are replaced by `-`. An empty version is left out, build metadata is dropped. - `Apply(config, product, version)` sets that user agent on a `KubernetesClientConfiguration` unless it already carries an own one. An empty user agent and the client default count as not configured. - **`OperatorBuilder`**: the default `IKubernetes` applies the user agent from the operator name and the informational version of the entry assembly to the configuration before it creates the client. This also covers a `KubernetesClientConfiguration` registered by the user, as long as it has no own user agent; the registered configuration is changed in place (documented). The user agent always starts with the operator name, independent of `WithFieldManager`. With an empty operator name the client default is kept. - **KubeOps CLI** identifies itself as `kubeops-cli/<version>`. No new setting: an own user agent is set through `KubernetesClientConfiguration.UserAgent`, which already exists. ## Behavioural changes for users No public API is removed or changed. Visible differences: - The `User-Agent` header of the operator and the CLI changes, e.g. in API server audit logs. Anything that matches on `kubernetes-csharp` in the user agent still matches, because the client token stays at the end. - An `IKubernetes` registered by the user keeps the user agent of its configuration; `KubernetesUserAgent` can be used to set it (documented). - `KubernetesClient` constructed directly (outside the operator) is unchanged. ## Docs - New page `docs/docs/operator/configuration/user-agent.mdx`, linked from the Advanced Configuration index and from the Field Manager page - `KubeOps.KubernetesClient` README - CLI page - Migration guide ("New in v15 (non-breaking)") ## Tests - Unit tests (`Area` `KubernetesClient` / `Configuration` / `Cli`): - format, omitted version, dropped build metadata, token sanitisation (incl. CR/LF), empty product - `Apply`: replaces a missing, empty and default user agent, keeps an own one - `OperatorBuilder` wiring: operator name independent of the field manager, own configured user agent, empty operator name - CLI factory - `dotnet build -c Release`, `dotnet format --verify-no-changes` and the unit tests of `KubeOps.KubernetesClient.Test`, `KubeOps.Operator.Test` and `KubeOps.Cli.Test` pass locally, and the docs build (`npm run build`, `onBrokenLinks: throw`) succeeds. I did not run the integration tests locally. ## Reviews Run before merge: `/code-review`, `/simplify`, `/security-review`, docs sync. - **`/code-review`**: fixed - an empty operator name with an explicit field manager made resolving `IKubernetes` throw, because the product is required; the user agent is now skipped in that case - build metadata (`+<sha>`) ended up in the user agent, unlike the documented format; it is now dropped - an empty `UserAgent` counted as configured; it now counts as not configured - the in-place change of a user-registered configuration is now documented - **`/simplify`**: public surface reduced from four members (`ClientDefault`, `Create`, `CreateFromAssembly`, `SetIfNotConfigured`) to `Create` and `Apply`; string building simplified; redundant tests (incl. a wire-level test of upstream behaviour) and duplicated docs removed. Efficiency: no findings. - Skipped: changing the existing public `KubernetesClient(config, fieldManager)` constructor to also set the user agent. It was released in 14.1.0, and mutating the caller's configuration from a constructor would be a surprising behaviour change; tools call `KubernetesUserAgent.Apply` instead. - Skipped: sharing `BuildProvider` / the stub API server between test classes (small, test-only duplication). - **`/security-review`**: no findings. Product and version pass through the token sanitiser, so no header/CRLF injection; only `UserAgent` of the configuration is changed, never TLS or credentials. - **Docs**: synced with the final API (user-agent page, README, migration guide). ## Kubernetes client 21.0.41 `main` moved to `KubernetesClient` 21.0.41 (#1349) while this PR was open. Docs only: - The user agent example shows `kubernetes-csharp/21.0.41+d05b908e20`. - `migration.mdx` (v15): the client now rejects an API server certificate that does not match the server address when a CA from the kubeconfig is used ([kubernetes-client/csharp#1964](kubernetes-client/csharp#1964)), with how to fix a kubeconfig (`server` or `tls-server-name`). Our code has no own certificate validation and needs no change.
This was referenced Nov 30, 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.
Closes #1345
What
Every write (
POST,PUT,PATCH) KubeOps sends to the API server now carries an explicitfieldManager, configured once per operator. Writes are recorded inmetadata.managedFieldsunder the operator name instead ofkubernetes-csharp.How
OperatorSettings.FieldManager— set throughWithFieldManager(string)/OperatorSettingsBuilder.FieldManager. Without one, it resolves toName, also for settings copied withwith { Name = ... }. Notrequired, so existing object initializers keep compiling.FieldManagerName) mirrors the API server: not empty, at most 128 bytes (UTF-8), printable characters only (Go'sunicode.IsPrint). The builder setter,WithFieldManagerand the record'sinitreject invalid values with anArgumentException. If no field manager is set and the operator name is not a valid one,Build()throws anInvalidOperationExceptionthat points toWithFieldManager, instead of failing later whenIKubernetesis resolved.FieldManagerHandler(publicDelegatingHandlerinKubeOps.KubernetesClient) appendsfieldManager=<name>to write requests. It leaves requests unchanged thatfieldManager(matched case-sensitively, so an explicit manager, e.g. of a server-side apply throughPatchAsync, always wins), orproxy,exec,attach,portforward). These do not write an object, and the API server forwards the query of a proxy request to the backend.OperatorBuilderregisters the defaultIKuberneteswith the handler.KubernetesClient(now registered through an explicit factory, so the new constructor overload cannot make DI ambiguous), the leader elector and all internal writers use that instance. This covers finalizers, events, CRD installation, webhook configuration /caBundle, the leader election lease and application code.KubernetesClient(KubernetesClientConfiguration, string fieldManager)for use outside the operator (CLIs, migration tools).install,uninstall,api-version) writes askubeops-cli.Behavioural changes for users
No public API is removed or changed. Visible differences:
managedFieldsentries written after the upgrade use the operator name instead ofkubernetes-csharp. Existing entries keep their old manager until the fields they own are written again. Update operations never conflict on ownership, and server-side apply already required an explicit manager, so this does not change who wins a conflict.IKuberneteshas to addFieldManagerHandleritself (documented).WithFieldManager(...). Before, such a name would only surface as an error once a write reached the API server.Docs
docs/docs/operator/configuration/field-manager.mdx, linked from the Advanced Configuration indexKubeOps.KubernetesClientREADME (rendered in the package docs)Tests
AreaConfiguration/KubernetesClient/Cli:FieldManagerNameruleswithcopiesKubernetesClientwrites through the real k8s client pipeline with a stub handlerOperatorBuilderwiringKubernetesClient.FieldManager.Integration.Test.cs): checksmanagedFieldsagainst a real cluster. It runs in CI with kind; I could not run it locally.dotnet build -c Release,dotnet format --verify-no-changesand all unit tests pass locally.