Skip to content

feat(operator): write with an explicit field manager configured per operator - #1346

Merged
kimpenhaus merged 1 commit into
mainfrom
feat/field-manager
Nov 24, 2026
Merged

kimpenhaus merged 1 commit into
mainfrom
feat/field-manager

Conversation

@kimpenhaus

Copy link
Copy Markdown
Collaborator

Closes #1345

What

Every write (POST, PUT, PATCH) KubeOps sends to the API server now carries an explicit fieldManager, configured once per operator. Writes are recorded in metadata.managedFields under the operator name instead of kubernetes-csharp.

builder.Services
    .AddKubernetesOperator(settings => settings
        .WithName("my-operator")
        .WithFieldManager("my-operator")); // optional, defaults to the operator name

How

  • OperatorSettings.FieldManager — set through WithFieldManager(string) / OperatorSettingsBuilder.FieldManager. Without one, it resolves to Name, also for settings copied with with { Name = ... }. Not required, so existing object initializers keep compiling.
  • Validation (FieldManagerName) mirrors the API server: not empty, at most 128 bytes (UTF-8), printable characters only (Go's unicode.IsPrint). The builder setter, WithFieldManager and the record's init reject invalid values with an ArgumentException. If no field manager is set and the operator name is not a valid one, Build() throws an InvalidOperationException that points to WithFieldManager, instead of failing later when IKubernetes is resolved.
  • FieldManagerHandler (public DelegatingHandler in KubeOps.KubernetesClient) appends fieldManager=<name> to write requests. It leaves requests unchanged that
    • already carry a fieldManager (matched case-sensitively, so an explicit manager, e.g. of a server-side apply through PatchAsync, always wins), or
    • target a connect subresource (proxy, exec, attach, portforward). These do not write an object, and the API server forwards the query of a proxy request to the backend.
  • OperatorBuilder registers the default IKubernetes with 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).
  • KubeOps CLI (install, uninstall, api-version) writes as kubeops-cli.

Behavioural changes for users

No public API is removed or changed. Visible differences:

  • managedFields entries written after the upgrade use the operator name instead of kubernetes-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.
  • An operator that registers its own IKubernetes has to add FieldManagerHandler itself (documented).
  • An operator name that is not a valid field manager (over 128 bytes or containing non-printable characters) now needs an explicit WithFieldManager(...). Before, such a name would only surface as an error once a write reached the API server.

Docs

  • New page docs/docs/operator/configuration/field-manager.mdx, linked from the Advanced Configuration index
  • KubeOps.KubernetesClient README (rendered in the package docs)
  • CLI page
  • Migration guide ("New in v14 (non-breaking)")

Tests

  • Unit tests, with Area Configuration / KubernetesClient / Cli:
    • FieldManagerName rules
    • builder defaults and validation, with copies
    • handler behaviour: methods, existing / case-sensitive parameters, connect subresources, path prefixes, escaping
    • KubernetesClient writes through the real k8s client pipeline with a stub handler
    • OperatorBuilder wiring
    • CLI factory
  • Integration test (KubernetesClient.FieldManager.Integration.Test.cs): checks managedFields against a real cluster. It runs in CI with kind; I could not run it locally.
  • dotnet build -c Release, dotnet format --verify-no-changes and all unit tests pass locally.

…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
kimpenhaus enabled auto-merge (squash) November 24, 2026 08:00
@kimpenhaus
kimpenhaus merged commit 4bf6fb2 into main Nov 24, 2026
7 checks passed
@kimpenhaus
kimpenhaus deleted the feat/field-manager branch November 24, 2026 08:03
@github-actions

Copy link
Copy Markdown

🎉 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature]: Write with an explicit field manager configured per operator

1 participant