operator-framework / java-operator-sdk

Java SDK for building Kubernetes Operators
https://javaoperatorsdk.io/
Apache License 2.0
802 stars 215 forks source link

Add "OwnerType" Annotation for Non-GC'ed DependentResources #2585

Open coltmcnealy-lh opened 4 hours ago

coltmcnealy-lh commented 4 hours ago

I have a reconciler that extends KubernetesCRUDNoGCDependentResource. The created dependent has the following annotations:

metadata:
  annotations:
    io.javaoperatorsdk/primary-name: my-thing
    io.javaoperatorsdk/primary-namespace: my-namespace

In my operator, I have several different CustomResourceDefinitions and reconcilers. Therefore, I can and do have multiple custom resources in the my-namespace namespace called my-thing.

In my own code, I need to determine the owner of these resources. I can solve that by putting labels/annotations on them myself (this is quite a bit of work). However, I imagine that this is likely a vulnerability to bugs.

Proposed Solution

Add another annotation to NoGC resources and use that for determining ownership:

metadata:
  annotations:
    io.javaoperatorsdk/primary-type: MyCustomResource
metacosm commented 4 hours ago

I need to double check but isn't the type of the primary known when the annotations are used?

metacosm commented 3 hours ago

Indeed, the owner type is not known. However, this will require deeper changes than simply adding the annotation because the mappers only return ResourceID instances, which, currently, only also record name and namespaces.

csviri commented 3 hours ago

@metacosm I'm pretty sure we don't need to extend resourceID, note that from v5 we do type checking for owner references, adding type annotation should do the trick, and have the secondary filter on the.

Another option is just use labels/ label selectors, which is also more efficient.

Or both.