Sub topic 8
JSON 6902 patches and selecting targets
This chapter assumes you can read a kustomization.yaml, know the base and overlay layout, and have used a strategic merge patch. If not, review those chapters first.
When to use a JSON 6902 patch
A strategic merge patch looks like the resource it changes. It works well for adding or changing fields. It is awkward when you need to remove a field, change one item in a list by position, or replace a single value without repeating its parents.
A JSON 6902 patch (named after RFC 6902) is a list of explicit operations. Each operation has an op, a path, and usually a value. The path is a JSON Pointer, so /spec/replicas means the replicas field inside spec.
Here is an inline patch in an overlay's kustomization.yaml:
patches:
- target:
kind: Deployment
name: web
patch: |-
- op: replace
path: /spec/replicas
value: 3The target chooses which resources receive the patch. The patch field holds the operations. Kustomize detects the JSON 6902 format because the patch is a list. A strategic merge patch is a map with apiVersion and kind.
The operations
| Operation | What it does | Fails when |
|---|---|---|
| `add` | Adds a field, or inserts into a list | The parent path does not exist |
| `replace` | Overwrites an existing value | The path does not exist |
| `remove` | Deletes a field or list item | The path does not exist |
| `copy`, `move` | Duplicates or relocates a value using `from` | The `from` path does not exist |
| `test` | Checks a value and aborts the patch if it differs | The value does not match |
List items are addressed by zero-based index. /spec/template/spec/containers/0/image is the first container. Use - as the index with add to append to the end of a list.
Example: remove and append
This patch file removes a field and adds an environment variable. Save it as overlays/production/env-patch.yaml:
- op: remove
path: /spec/template/spec/containers/0/ports
- op: add
path: /spec/template/spec/containers/0/env
value:
- name: REGION
value: eu-west-1Reference it with path:
patches:
- path: env-patch.yaml
target:
kind: Deployment
name: webStrategic merge patches identify their resource from the patch content. A JSON 6902 patch has no such content, so the target is required.
Selecting targets
The target field accepts several selectors. All the fields you give must match.
group,versionandkindmatch the API type.namematches the resource name. It is a regular expression.namespacematches the namespace.labelSelectorandannotationSelectormatch metadata.
Omit name to patch every resource of a kind. This adds a label to every Deployment:
patches:
- target:
kind: Deployment
patch: |-
- op: add
path: /metadata/labels/tier
value: backendThe add here fails if the resource has no labels map, because the parent path must exist. Check the rendered output for each target.
Checking the result
Render the overlay before applying it:
kubectl kustomize overlays/productionIf a path is wrong, the build stops with an error that names the failing operation. A patch whose target matches nothing is also an error in current versions, which catches typos in name.
Exercise
A base Deployment has two containers, web and sidecar. You write a JSON 6902 patch with op: replace and path: /spec/template/spec/containers/1/image. Later a teammate reorders the containers. What happens, and which kind of patch avoids the problem?
Show answer
The patch now replaces the image of web instead of sidecar, because index 1 refers to list position. A strategic merge patch avoids this because it matches containers by their name field.