Skip to content

feat(abstractions)!: add controller references and deduplicate owner references - #1355

Merged
kimpenhaus merged 2 commits into
mainfrom
feat/controller-owner-references
Nov 28, 2026
Merged

kimpenhaus merged 2 commits into
mainfrom
feat/controller-owner-references

Conversation

@kimpenhaus

@kimpenhaus kimpenhaus commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

MakeOwnerReference set neither controller: true nor blockOwnerDeletion: true. Without a controller reference
there is no unique managing controller (the basis for owned watches, cf. controller-runtime Owns()), and without
blockOwnerDeletion a foreground deletion of the owner does not wait for its resources. The helpers follow
controller-runtime's controllerutil:

  • MakeControllerReference() (new): an owner reference with controller: true and blockOwnerDeletion: 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) and WithControllerReference(owner):
    • replace a reference to the same owner (same API group, kind and name; the version is ignored) instead of adding
      it again, e.g. with the new uid of a recreated owner. controller and blockOwnerDeletion of the existing
      reference stay, so ensuring an owner never drops the controller (fabric8 and kopf keep the existing reference
      as well; controller-runtime's SetOwnerReference would downgrade it),
    • throw an ArgumentException for an owner without name or uid, and for a namespaced owner of a resource that
      has no or another namespace. The garbage collector treats such an owner as missing and deletes the resource.
  • MakeOwnerReference() and MakeObjectReference() take a missing apiVersion or kind from the
    KubernetesEntityAttribute of the type. Items of a list response carry neither, so two listed owners of different
    kinds with the same name were indistinguishable, and the API server rejects an owner reference without kind.
  • The controller reference is read with GetController() of the Kubernetes client; no own getter.

Docs: new section Owner References (including the RBAC note for
OwnerReferencesPermissionEnforcement, e.g. on OpenShift: blockOwnerDeletion needs update on
<plural>/finalizers of the owner) and a v15 entry in migration.mdx.

Breaking change

WithOwnerReference:

  • no longer adds a second reference to an owner that is already referenced; it replaces it and keeps its
    controller and blockOwnerDeletion.
  • throws an ArgumentException for a namespaced owner if the resource has no or another namespace, and for an owner
    without name or uid.

Migration: set the namespace of a resource before adding the owner. Use WithControllerReference for the
resources a controller manages.

Open points

Checks

  • /code-review: one bug fixed (owners from list responses without apiVersion/kind were 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: own GetControllerReference replaced by GetController() of the Kubernetes client; apiVersion/
    kind fallback shared with MakeObjectReference via EntityMetadata.GroupWithVersion; ApiGroup simplified.
  • /security-review: no findings.
  • dotnet build -c Release without 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.

…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.
@kimpenhaus
kimpenhaus marked this pull request as ready for review November 28, 2026 08:26
@kimpenhaus
kimpenhaus merged commit 4979abe into main Nov 28, 2026
7 of 9 checks passed
@kimpenhaus
kimpenhaus deleted the feat/controller-owner-references branch November 28, 2026 08:50
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 15.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.
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.

1 participant