Kustomize apply one patch to multiple resource types (to add ownerReferences)

2/15/2021

I'm trying to use kustomize to apply a patch that adds ownerReferences to all resources in the base

I want to do something like this:

# kustomization.yaml
resources:
  - deployment.yaml
  - svc.yaml
secretGenerator:
- name: mysecret
  files:
    - mysecret.txt
patches:
  - owner_references.yaml

# owner_references.yaml
metadata:
  ownerReferences:
    - apiVersion: my.crdapi/v1
      blockOwnerDeletion: true
      controller: true
      kind: MyCrd
      name: all-the-things
      uid: <todo>

But i get the error:

error unmarshaling JSON: while decoding JSON: Object 'Kind' is missing in

So it seems kustomize requires a "Kind: " for patches? But I want to patch all resources..

Can anyone suggest a good way to do this? Thanks :)

-- Bob
kubectl
kubernetes
kustomize

1 Answer

2/15/2021

Merge patches don't support this kind of thing because they need the GKV and name/namespace to know which object it's a patch for. You could do this with a JSON patch though. (just more ugly, sorry)

-- coderanger
Source: StackOverflow