Add fields to list with Kustomize

How to add things to a list using kustomize. This is useful for example when you need to patch additional environment variables into a pod.

A simple example, adding the MY_SETTING environment variable to pods in a deployment called external-deployment while keeping all pre-existing environment variables in the list.

kind: Kustomization

  - base/deployment.yaml

  - target:
      group: apps
      version: v1
      kind: Deployment
      name: external-deployment
    path: patches/external-deployment.yaml
- op: add
  path: "/spec/template/spec/containers/0/env/0"
    name: MY_SETTING
    value: "my value"

See also