Adding items to a list with kubectl kustomize

I have base/foo.yaml that I want to apply to all my environments and it partially looks like this

Kubernetes:
  deploymentPatches:
    - patch: |-
      - {"op": "add", "path": "/spec/template/spec/volumes/-", "value": {"name": "volume", "secret": {"secretName": "my-secret"}}}
      - {"op": "add", "path": "/spec/template/spec/containers/0/volumeMounts/-", "value": {"mountPath": "/connections", "name": "volume"}}

Now I want to add environment-specific patches as well so my end goal after running e.g. kubectl kustomize accept should either be

Kubernetes:
  deploymentPatches:
    - patch: |-
      - {"op": "add", "path": "/spec/template/spec/volumes/-", "value": {"name": "volume", "secret": {"secretName": "my-secret"}}}
      - {"op": "add", "path": "/spec/template/spec/containers/0/volumeMounts/-", "value": {"mountPath": "/connections", "name": "volume"}}
      - {"op": "add", "path": "/spec/template/spec/containers/0/env/-", "value": {"name": "MY_ENVIRONMENT", "value": "accept"}}

or

Kubernetes:
  deploymentPatches:
    - patch: |-
      - {"op": "add", "path": "/spec/template/spec/volumes/-", "value": {"name": "volume", "secret": {"secretName": "my-secret"}}}
      - {"op": "add", "path": "/spec/template/spec/containers/0/volumeMounts/-", "value": {"mountPath": "/connections", "name": "volume"}}
  - patch: |-
      - {"op": "add", "path": "/spec/template/spec/containers/0/env/-", "value": {"name": "MY_ENVIRONMENT", "value": "accept"}}

How would I go about setting up my kustomize config to accomplish this? I’ve tried the following thus far:

kustomize.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
  - ../base
patchesStrategicMerge:
  - foo.yaml

accept/foo.yaml

Kubernetes:
  deploymentPatches:
    - patch: |-
      - {"op": "add", "path": "/spec/template/spec/containers/0/env/-", "value": {"name": "MY_ENVIRONMENT", "value": "accept"}}

Read more here: Source link