k8s - multiple ownerrefs in CRDs

12/3/2021

Say I have the following CRD:

...
kind: SomeCRD
metadata:
  ownerReferences:
  - uid: aaa
    name: name-a
...

When I run kubectl apply -f some.yaml where some.yaml is the following

...
kind: SomeCRD
metadata:
  ownerReferences:
  - uid: bbb
    name: name-b
...

I'd like to have the ownerrefs merged as the following:

...
kind: SomeCRD
metadata:
  ownerReferences:
  - uid: aaa
    name: name-a
  - uid: bbb
    name: name-b
...

Is that possible? Currently kubectl apply would simply replace the existing ownerReferences aaa with bbb, while it would merge the ownerReferences aaa and bbb for native types such as Pod.

If I understand it correctly, strategic merge isn't supported for CRDs by default, but starting from 1.16/1.17 we can define merge strategy in the schema of the CRDs (k8s doc). Ideally we could define the merge strategy for metadata.ownerReferences in the schema of someCRD and that'd solve the problem here, but metadata belongs to apiserver and can't be redefined in CRDs. It feels like metadata is stuck in some unfortunate state where it's a k8s native type, but treated as an unstructured field when it's in CRDs.

Any way to make this work or am I missing something?

Thanks in advance! :)

-- Nancy Hsu
kubernetes
kubernetes-custom-resources
merge-strategy

0 Answers