Skip to content

Patches

Outcome: both patch dialects applied — a strategic merge patch in production, a JSON 6902 patch in staging — each proven by the diff it makes, and a working sense of which dialect to reach for.

Transformer fields (chapter 5) cover the differences that come in standard shapes: names, labels, tags, counts. The rest — one workload's memory limit, a debug switch on one container — are patches: targeted edits to a single resource, declared in the overlay.

Strategic merge patches

A strategic merge patch is a fragment of YAML in the shape of the resource it edits, containing only what changes. Production carries one in its own file, replica-and-size-patch.yaml (transcript t04):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webhello
spec:
  replicas: 4
  template:
    spec:
      containers:
        - name: webhello
          resources:
            limits:
              cpu: 500m
              memory: 256Mi

and its kustomization applies it by pointing at the file:

patches:
  - path: replica-and-size-patch.yaml

The patch finds its target from its own apiVersion, kind and name — no separate address needed. "Merge" is the operative word: the fragment is laid over the base resource field by field. The build shows exactly the merge and nothing else (diff it, t05): replicas 2 → 4; limits 250m/128Mi → 500m/256Mi; requests, ports, environment, volumes — untouched, because the fragment never mentioned them. Lists merge by their key fields rather than by position, which is why the fragment can name the container webhello and adjust its resources without restating the container.

This is the dialect to prefer by default: it reads like the resource it edits, and a reviewer sees the intent at a glance.

JSON 6902 patches

A JSON 6902 patch is a list of operations with exact paths — add this at this address, replace that, remove the other. Staging uses one, inline in its kustomization (t03):

patches:
  - target:
      kind: Deployment
      name: webhello
    patch: |-
      - op: add
        path: /spec/template/spec/containers/0/env/-
        value:
          name: LOG_LEVEL
          value: debug

Here the target is stated separately, in the target: block, because the patch itself carries no resource identity — just operations. Read the one operation aloud: add, at the path of the first container's env list, appended at its end (the /-), this name-and-value pair. The staging build's environment list grows a third entry, LOG_LEVEL: debug, exactly there (t05's diff shows the two added lines and no others in that list).

Why keep the verbose dialect at all? Precision and reach. Paths can address a specific list position (containers/0), operations can remove a field outright or replace one that a merge would only overlay, and an add can create list entries — like the environment variable above — in cases where a merge fragment cannot express where the new entry goes. When a strategic merge patch cannot say it, JSON 6902 can; when both can, the merge patch is kinder to its readers.

Two syntaxes, one patches: list: an entry with path: names a patch file (whose contents may be either dialect — the merge patch above is a file), and an entry with target: plus patch: writes the JSON 6902 operations inline. Older kustomizations spell these patchesStrategicMerge: and patchesJson6902:; current kustomize unifies them under patches: and kustomize edit fix (chapter 9) converts the old spellings.

A patch that matches nothing

A patch is a promise that its target exists. Break the promise and the build refuses — the lab's broken-patch overlay mistypes the Deployment's name, and chapter 14 keeps the error verbatim:

Error: no resource matches strategic merge patch "Deployment.v1.apps/webhelo.[noNs]": ...

That strictness is a feature. A patch that silently skipped would let an overlay drift away from its base while every build stayed green; a patch that cannot find its target fails the build where you can see it.