CoursesKustomizeIntermediate

Patches: strategic merge & JSON6902

Change exactly what differs.

Intermediate14 min · lesson 6 of 12

Patches are how an overlay changes specific fields of a base resource. Kustomize supports two styles. A strategic-merge patch is a partial manifest — you write just the fields you want to change, with enough identity (kind + name) to match, and Kustomize merges it into the base. A JSON6902 patch is a precise op list (add/replace/remove at a JSON path), ideal for surgical edits and for changing list elements where merge semantics are ambiguous.

CHOOSING A PATCH STYLE
Strategic-merge (default choice)
Partial manifest
write only the fields that change
Match by kind + name
enough identity to locate the resource
Merges maps naturally
reads like the manifest itself
JSON6902 (surgical)
Op list
add / replace / remove at a JSON path
Target list by index
e.g. containers/0/env/-
Remove a field entirely
when merge semantics are ambiguous
Strategic-merge for most edits; JSON6902 for list-by-index, removals, or ambiguous merge cases.
strategic-merge patch
# patches/resources.yaml — only the fields that change
apiVersion: apps/v1
kind: Deployment
metadata: { name: payments-api }
spec:
template:
spec:
containers:
- name: app
resources:
limits: { cpu: "2", memory: 1Gi }
# kustomization.yaml: patches: [{ path: patches/resources.yaml }]
JSON6902 patch
patches:
- target: { kind: Deployment, name: payments-api }
patch: |-
- op: replace
path: /spec/replicas
value: 4
- op: add
path: /spec/template/spec/containers/0/env/-
value: { name: TIER, value: prod }

Choosing a patch style

Reach for strategic-merge for most changes — it reads like the manifest and merges maps naturally. Use JSON6902 when you need to target a list element by index, remove a field entirely, or when merge behavior on a list is not what you want (strategic merge can append or replace lists depending on patch strategy). Both are declarative and both are visible in kustomization.yaml, so the overlay documents exactly what it changes.

A patch that does not match is often silent
If a patch’s target identity (kind/name, or a JSON path) does not match anything in the base — a renamed resource, a wrong index — Kustomize may skip it or error depending on the case, and a skipped strategic-merge patch means your intended change silently did not happen. Always build and inspect the output to confirm the patch actually took effect; do not assume it applied because the file exists.