Repository navigation
feat(abstractions)!: add controller references and deduplicate owner references - #1355
Merged
Merged
Conversation
…references Add MakeControllerReference and WithControllerReference, which set controller: true and blockOwnerDeletion: true and allow only one controller per resource. WithOwnerReference and WithControllerReference replace a reference to the same owner (API group, kind and name) instead of adding it again, and throw an ArgumentException for an owner without name or uid and for a namespaced owner of a resource with no or another namespace. MakeOwnerReference and MakeObjectReference take a missing apiVersion or kind from the entity attribute of the type, e.g. for list items. BREAKING CHANGE: WithOwnerReference no longer adds a second reference to the same owner and throws for owners in another namespace and for owners without name or uid.
…ference WithOwnerReference replaces a reference to the same owner but keeps its controller and blockOwnerDeletion, so ensuring an owner no longer turns a controller reference into a plain one.
|
🎉 This PR is included in version 15.0.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
This was referenced Dec 2, 2026
kimpenhaus
added a commit
that referenced
this pull request
Dec 4, 2026
## Summary For built-in types, the API server sets `apiVersion` and `kind` only on the list, not on its items, and `KubernetesClient.ListAsync` returned the items unchanged. Owner and object references fill both in from the type since #1355, but logging and other consumers saw empty values. `ListAsync` now sets both from the entity type's metadata on every item. Custom resources already come with both fields; they are set to the same values. The fallback in `KubernetesExtensions` from #1355 stays: core types created with `new` and lists read through `IKubernetes` directly still lack both fields. Its XML docs no longer name list items as the example, and the `<returns>` doc of `IKubernetesClient.ListAsync` states that items carry `apiVersion` and `kind`. No API change and nothing users need to do, so no docs or migration guide update. ## Tests - `KubernetesClient.CoreTypes.Test.cs`: `List_Sets_ApiVersion_And_Kind_Of_The_Items` for a namespaced and an all-namespaces list of `V1ConfigMap` whose items lack both fields. Fails without the fix. ## Checks - `/code-review` (low): no findings. - `/simplify`: the comment now says that only built-in types lack the fields; the test moved from its own file with a custom entity (a response a real server does not send for CRs) to the core type tests; the XML docs of `MakeObjectReference`/`MakeOwnerReference` and `ListAsync` were updated; `GroupWithVersion` is read once per call. Altitude: list items are the only responses without both fields, and the operator's only list call (`ScopeAwareResourceWatcher.RelistAsync`) goes through `ListAsync`. - `/security-review`: no findings. - `dotnet build -c Release` without warnings, `dotnet format --verify-no-changes`, all tests of `KubeOps.KubernetesClient.Test` (194) and `KubeOps.Operator.Test` (712) pass on net10.0 and net11.0, including the integration tests against a local OrbStack cluster.
kimpenhaus
added a commit
that referenced
this pull request
Dec 4, 2026
…1372) ## Summary Built-in types created with `new` (e.g. `new V1Secret { Metadata = ... }`) carry no `kind` and `apiVersion`. The operator logged `entity.Kind` directly, so such an entity, e.g. one put into `ITimedEntityQueue<V1Secret>` by a controller, showed up as `null/my-secret` or `/my-secret` in logs. This matches #989, which could not be reproduced with an entity from a watch event, since watch events always carry `kind`. - **New public API** in `KubeOps.Abstractions.Entities.KubernetesExtensions`: `GetKind()` and `GetApiVersion()` return the value of the object, or the one of its `[KubernetesEntity]` attribute if the object carries none. They were private helpers for owner and object references since #1355. - **Logging:** `ToIdentifierString()` and the `Kind` field of the entity logging scope use `GetKind()`. - **Exceptions:** the three owner reference exceptions (`already controlled by`, `has no namespace`, `must be cluster-scoped`) name the resource with `GetKind()`; a child resource is usually created with `new`. - **Docs:** one sentence in the owner references section of `controllers.mdx`; the XML docs say "built-in type" consistently. Relates to #989. Items from `ListAsync` carry both fields since #1371, so this covers the remaining case. Public instead of `internal` with `InternalsVisibleTo`: `KubeOps.Operator` only requires a minimum version of `KubeOps.Abstractions`, so an internal API could break at runtime with mixed package versions, and the repo grants `InternalsVisibleTo` only to test projects. ## Tests - `KubernetesExtensions.TypeMeta.Test.cs` (new): values of the object, fallback for `null` and empty values, group and version for `V1Deployment`. - `KubernetesExtensions.OwnerReferences.Test.cs`: the child `ConfigMap` is created without `kind`/`apiVersion`, and the exception messages must contain `ConfigMap resource` (5 tests fail with `resource.Kind`). - `KubernetesObjectExtensions.Test.cs` and `EntityLoggingScope.Test.cs` (new): a `V1Secret` without `kind` is logged as `Secret/my-secret` (4 tests fail without the logging change). ## Checks - `/code-review` (low): no findings. - `/simplify`: switched the three exception messages to `GetKind()` (altitude), kept `null` in the logging scope like the other fields instead of `""`, unified the wording to "built-in type". Not changed: no cache for the attribute lookup, as it only runs when the object has no kind. - `/security-review`: no findings. - `dotnet build -c Release` without warnings, `dotnet format --verify-no-changes`, `npm run build` in `docs/`, all tests of `KubeOps.Abstractions.Test` (338) and `KubeOps.Operator.Test` (358 per framework) pass on net10.0 and net11.0, including the integration tests against a local OrbStack cluster. One earlier full run hung in an integration test; the reruns per framework passed.
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
MakeOwnerReferenceset neithercontroller: truenorblockOwnerDeletion: true. Without a controller referencethere is no unique managing controller (the basis for owned watches, cf. controller-runtime
Owns()), and withoutblockOwnerDeletiona foreground deletion of the owner does not wait for its resources. The helpers followcontroller-runtime's
controllerutil:MakeControllerReference()(new): an owner reference withcontroller: trueandblockOwnerDeletion: true.WithControllerReference(owner)(new): adds the owner as controller. A resource has at most one controller;if another owner is already the controller, it throws an
InvalidOperationException(cf.AlreadyOwnedError).WithOwnerReference(owner)andWithControllerReference(owner):it again, e.g. with the new
uidof a recreated owner.controllerandblockOwnerDeletionof the existingreference stay, so ensuring an owner never drops the controller (fabric8 and kopf keep the existing reference
as well; controller-runtime's
SetOwnerReferencewould downgrade it),ArgumentExceptionfor an owner without name oruid, and for a namespaced owner of a resource thathas no or another namespace. The garbage collector treats such an owner as missing and deletes the resource.
MakeOwnerReference()andMakeObjectReference()take a missingapiVersionorkindfrom theKubernetesEntityAttributeof the type. Items of a list response carry neither, so two listed owners of differentkinds with the same name were indistinguishable, and the API server rejects an owner reference without
kind.GetController()of the Kubernetes client; no own getter.Docs: new section Owner References (including the RBAC note for
OwnerReferencesPermissionEnforcement, e.g. on OpenShift:blockOwnerDeletionneedsupdateon<plural>/finalizersof the owner) and a v15 entry inmigration.mdx.Breaking change
WithOwnerReference:controllerandblockOwnerDeletion.ArgumentExceptionfor a namespaced owner if the resource has no or another namespace, and for an ownerwithout name or
uid.Migration: set the namespace of a resource before adding the owner. Use
WithControllerReferencefor theresources a controller manages.
Open points
KubernetesClient.ListAsynccould fillapiVersion/kindof the items itself, which would fix allconsumers (events, logging) at the source.
migration.mdx: feat(operator): identify the operator by name in the user agent #1348 also appends to the v15 section; whichever is merged second needs a rebase.Checks
/code-review: one bug fixed (owners from list responses withoutapiVersion/kindwere merged by name);clearer error for a resource without namespace. Not changed: a namespaced owner object without its namespace set
is treated as cluster-scoped.
/simplify: ownGetControllerReferencereplaced byGetController()of the Kubernetes client;apiVersion/kindfallback shared withMakeObjectReferenceviaEntityMetadata.GroupWithVersion;ApiGroupsimplified./security-review: no findings.dotnet build -c Releasewithout warnings,dotnet format --verify-no-changes,KubeOps.Abstractions.Test(330) and the unit tests of
KubeOps.Operator.Test(610) pass. Not tested against a cluster.