Sub topic 7
Changing fields with strategic merge patches
This chapter assumes you can read Kubernetes YAML, know how a Deployment nests containers, and have a base plus a production overlay as set up in the earlier chapters. Transformers such as replicas and images cover common changes. For anything else, such as resource limits or probes, you need a patch.
What a strategic merge patch is
A strategic merge patch is a partial Kubernetes object. It names the resource it changes (apiVersion, kind, metadata.name) and contains only the fields you want to add or change. Kustomize merges it into the matching base resource.
The merge uses knowledge of the Kubernetes schema. Lists such as containers are matched by a key, usually name, instead of being replaced as a whole. A plain JSON merge would overwrite the whole list. A strategic merge keeps the other containers and fields.
A worked example
The base Deployment has one container named web and no resource settings. This patch file, overlays/production/resources-patch.yaml, adds them:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
template:
spec:
containers:
- name: web
resources:
requests:
cpu: 500m
memory: 256Mi
limits:
memory: 512MiThe overlay's kustomization.yaml points to the file:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
patches:
- path: resources-patch.yaml
target:
kind: Deployment
name: webRender it with kubectl kustomize overlays/production. The output keeps the image, labels and port from the base and adds the resources block to the web container.
The target field is optional here. Without it, Kustomize uses the kind and name inside the patch file. Setting it explicitly makes the intent clear, and it matters if you use a name prefix, as the next section explains.
How matching works
Kustomize applies patches before it applies transformers that rename resources. So the patch should use the base name web, not prod-web, even if the overlay sets namePrefix: prod-.
Merge rules to remember:
- A field you set in the patch replaces the base value.
- A field you leave out stays as it is in the base.
- Maps are merged key by key.
- Lists with a merge key, such as
containers(keyname) andenv(keyname), are merged by that key. - Lists without a merge key, such as
argsorcommand, are replaced whole. - To delete a field, set it to
nullin the patch.
Common errors
- Patch does nothing. The
nameorkindin the patch does not match any base resource. Kustomize may report that no matching resource was found. Check the base name, not the prefixed name. - A container is added instead of changed. The container
namein the patch differs from the base, so it is treated as a new container. Use the exact name. - Whole list lost. You patched a list that has no merge key, for example
args, and it replaced the base list. Repeat all the values you want to keep. - Wrong indentation. A patch nested one level too shallow merges into the wrong place. Mirror the full path from
specdown to the field.
Inline patches
You can also write a patch inline with the patch field instead of a file. This suits very short changes:
patches:
- target:
kind: Deployment
name: web
patch: |-
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
template:
spec:
containers:
- name: web
imagePullPolicy: AlwaysKustomize decides whether the content is a strategic merge patch or a JSON 6902 patch by its shape. A list of op entries is treated as JSON 6902. The next chapter covers that format.
Exercise
The base Deployment sets args: ["--port=8080", "--verbose"] on container web. Your patch sets args: ["--port=9090"] on the container named web. What are the rendered args, and why?
Show answer
The rendered args are ["--port=9090"]. The containers list is merged by container name, but args is a list of plain strings with no merge key, so the patch value replaces the base list. To keep --verbose, include it in the patch.
Exercise
An overlay sets namePrefix: prod-. Should the patch file use name: web or name: prod-web to target the base Deployment?
Show answer
Use name: web. Patches are matched against the base names, before the prefix is added.