Kustomize
Kubernetes
configuration management
DevOps
YAML

Kustomize patch to replace an item in a list

Codemia Knowledge Hub ·

To replace an item in a list with Kustomize, use a strategic merge patch that repeats the item's merge key (usually name) with the new values. For lists that have no merge key, or when you want to target a position, use a JSON 6902 patch with an array index such as /spec/template/spec/containers/0.

YAML lists have no keys of their own, which is why list items (containers, volumes, env vars) are harder to patch than map fields.

Strategic merge patch: match by name

Strategic merge patches match list items by their merge key. For containers, the merge key is name:

yaml
1# base/deployment.yaml
2apiVersion: apps/v1
3kind: Deployment
4metadata:
5  name: my-app
6spec:
7  template:
8    spec:
9      containers:
10        - name: app
11          image: myapp:1.0
12          resources:
13            limits:
14              memory: "128Mi"
15              cpu: "250m"
yaml
1# overlays/production/patch.yaml
2apiVersion: apps/v1
3kind: Deployment
4metadata:
5  name: my-app
6spec:
7  template:
8    spec:
9      containers:
10        - name: app  # Matches by name
11          image: myapp:2.0  # Replaced
12          resources:
13            limits:
14              memory: "512Mi"  # Replaced
15              cpu: "1000m"  # Replaced
yaml
1# overlays/production/kustomization.yaml
2apiVersion: kustomize.config.k8s.io/v1beta1
3kind: Kustomization
4resources:
5  - ../../base
6patchesStrategicMerge:
7  - patch.yaml
bash
kustomize build overlays/production
# Result: container "app" has image myapp:2.0 and updated resources

JSON 6902 patch: match by index

For precise control, or when items have no merge key, use JSON 6902 patches:

yaml
1# overlays/production/kustomization.yaml
2apiVersion: kustomize.config.k8s.io/v1beta1
3kind: Kustomization
4resources:
5  - ../../base
6patches:
7  - target:
8      kind: Deployment
9      name: my-app
10    path: patch.yaml
yaml
1# overlays/production/patch.yaml
2# Replace the entire first container
3- op: replace
4  path: /spec/template/spec/containers/0
5  value:
6    name: app
7    image: myapp:2.0
8    resources:
9      limits:
10        memory: "512Mi"
11        cpu: "1000m"
12
13# Replace just the image of the first container
14- op: replace
15  path: /spec/template/spec/containers/0/image
16  value: myapp:2.0

When the container has an env list, an index addresses one entry. The path must exist, or the build fails with doc is missing path:

yaml
1# Replace a specific env var (third item in env list)
2- op: replace
3  path: /spec/template/spec/containers/0/env/2/value
4  value: "new-value"

Adding items to a list

yaml
1# JSON 6902 — add a new container to the list
2- op: add
3  path: /spec/template/spec/containers/-
4  value:
5    name: sidecar
6    image: sidecar:latest
7    ports:
8      - containerPort: 9090
9
10# Add a new env var to the first container
11- op: add
12  path: /spec/template/spec/containers/0/env/-
13  value:
14    name: NEW_VAR
15    value: "hello"

The - at the end of the path means "append to the array."

Removing items from a list

yaml
1# Remove the second container (index 1)
2- op: remove
3  path: /spec/template/spec/containers/1
4
5# Remove a specific env var by index
6- op: remove
7  path: /spec/template/spec/containers/0/env/0

Replacing environment variables

Env vars also use name as the merge key, so a strategic merge patch can replace individual values:

yaml
1# base/deployment.yaml
2spec:
3  template:
4    spec:
5      containers:
6        - name: app
7          image: myapp:1.0
8          env:
9            - name: DATABASE_URL
10              value: "postgres://localhost:5432/dev"
11            - name: LOG_LEVEL
12              value: "debug"
13            - name: API_KEY
14              valueFrom:
15                secretKeyRef:
16                  name: app-secrets
17                  key: api-key
yaml
1# Strategic merge patch — matches env items by name
2apiVersion: apps/v1
3kind: Deployment
4metadata:
5  name: my-app
6spec:
7  template:
8    spec:
9      containers:
10        - name: app
11          env:
12            - name: DATABASE_URL
13              value: "postgres://prod-db:5432/production"
14            - name: LOG_LEVEL
15              value: "warn"

Inline patches

For small changes, define patches inline in kustomization.yaml:

yaml
1apiVersion: kustomize.config.k8s.io/v1beta1
2kind: Kustomization
3resources:
4  - ../../base
5patches:
6  - target:
7      kind: Deployment
8      name: my-app
9    patch: |-
10      - op: replace
11        path: /spec/template/spec/containers/0/image
12        value: myapp:2.0
13      - op: replace
14        path: /spec/replicas
15        value: 3

Copying values with replacements (Kustomize 4.5+)

The replacements field copies values between resources declaratively:

yaml
1apiVersion: kustomize.config.k8s.io/v1beta1
2kind: Kustomization
3resources:
4  - ../../base
5replacements:
6  - source:
7      kind: ConfigMap
8      name: app-config
9      fieldPath: data.image_tag
10    targets:
11      - select:
12          kind: Deployment
13          name: my-app
14        fieldPaths:
15          - spec.template.spec.containers.[name=app].image

Mistakes that break list patches

  • Expecting a strategic merge patch to delete items: if you include a list in a strategic merge patch but leave out existing items, those items stay, because the merge matches by name. Only a plain (non-strategic) merge patch replaces the entire list.
  • Wrong array index in JSON 6902: indices are zero-based. Targeting /containers/1 when there is only one container fails with path not found. Run kustomize build on the base to check the list structure before patching by index.
  • Merge key mismatch: containers and volumes merge by name, ports by containerPort. If the key in your patch matches nothing in the base, a new item is added instead of replacing the existing one.
  • Lists with no merge key: lists like args and command have none, so a strategic merge patch replaces the whole list. Use JSON 6902 for item-level control.
  • Patch order: Kustomize applies patches in the order listed in kustomization.yaml, and later patches see the result of earlier ones. If two patches touch the same list item, the second works on the already-modified version.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design