A Kustomize JSON6902 patch fails because a parent field is missing
Your error
add operation does not apply: doc is missing path: "/metadata/annotations/example": missing value2 more lines it can showFix it fast
Most common cause, 1 of 3A parent object on the path does not exist in the resource
Create the parent map in one add operation, with the new key inside its value.
patches:
- target:
kind: Deployment
name: my-app
patch: |-
- op: add
path: /metadata/annotations
value:
example: "true"Short answer
Kustomize prints doc is missing path when a JSON6902 patch has to pass through a parent object the resource lacks, such as annotations or nodeSelector. Create the whole parent map with an add op, write a slash inside a key as ~1, and narrow the target to the resource it was written for. Applies to kustomize 5.8 and kubectl kustomize.
Which cause do you have?
Read the operation name and the path in the error, then render the base on its own and look for every segment of that path in the resource the patch targets.
kubectl kustomize overlays/dev
kubectl kustomize baseThen find the line you see in this list and open its fix:
- 1A parent object on the path does not exist in the resource
A segment before the last one, such as annotations or nodeSelector, is absent from the rendered base - 2A key with a slash in it was not escaped
The path holds a label or annotation key such as app.kubernetes.io/version written with a plain slash, not ~1 - 3The target selector also matches a resource without that field
The path is right for one resource, but the target only gives kind, a name pattern or a label selector that several resources share
What does the error look like?
add operation does not apply: doc is missing path: "/metadata/annotations/example": missing valuereplace operation does not apply: doc is missing path: /spec/template/spec/nodeSelector/disktype: missing valueremove operation does not apply: doc is missing path: "/metadata/labels/app.kubernetes.io/version": missing value
Cause 1 of 3
A parent object on the path does not exist in the resource
You seeA segment before the last one, such as annotations or nodeSelector, is absent from the rendered base
Check
Kustomize applies JSON6902 patches with the json-patch library, which walks every segment of the path except the last and gives up with doc is missing path when one is absent, whatever the op. RFC 6902 lets add create only the final key; the object meant to hold it has to exist already, and replace or remove need their whole target.
kubectl kustomize base | grep -n -A3 'annotations:'Fix
Create the parent map in one add operation, with the new key inside its value. That sets the whole map, so list every key the overlay needs there. A replace aimed inside a nodeSelector the base never set is fixed the same way, with an add on /spec/template/spec/nodeSelector.
patches:
- target:
kind: Deployment
name: my-app
patch: |-
- op: add
path: /metadata/annotations
value:
example: "true"Verify
The overlay renders without the error and the annotation appears on the Deployment.
kubectl kustomize overlays/dev | grep -n -A2 'annotations:'Confirmed by rfc-editor.org
Cause 2 of 3
A key with a slash in it was not escaped
You seeThe path holds a label or annotation key such as app.kubernetes.io/version written with a plain slash, not ~1
Check
JSON Pointer treats each slash as a step into a child object. Without escaping, app.kubernetes.io/version is read as a key named app.kubernetes.io followed by a child named version, which does not exist.
grep -rn 'path: /metadata' overlays/Fix
Write the slash inside the key as ~1, as the Kustomize patches reference shows for label names. A tilde inside a key becomes ~0.
- op: add
path: /metadata/labels/app.kubernetes.io~1version
value: 1.21.0Verify
The label appears with its full name in the rendered output.
kubectl kustomize overlays/dev | grep -n 'app.kubernetes.io/version'Confirmed by kubectl.docs.kubernetes.io
Cause 3 of 3
The target selector also matches a resource without that field
You seeThe path is right for one resource, but the target only gives kind, a name pattern or a label selector that several resources share
Check
A Kustomize patch is applied to every resource that matches all the fields given in target, so one JSON patch can land on a second Deployment or a CronJob whose structure is different. The patch then fails on the resource it was never written for.
kubectl kustomize base | grep -n -E '^kind:|^ name:'Fix
Narrow the target with group, version, kind and an exact name so only the intended resource matches. Name values are anchored patterns, so my-app matches only my-app.
target:
group: apps
version: v1
kind: Deployment
name: my-appVerify
The build passes and only the named resource carries the change.
kubectl kustomize overlays/devConfirmed by kubectl.docs.kubernetes.io
How do I stop it coming back?
- Use add rather than replace for a field the base may leave unset, since add also overwrites the value when the field is already there.
- Give every JSON6902 target a kind and an exact name, not only a label selector.
- Render each overlay with kubectl kustomize in CI so a base change that removes a patched field breaks the pull request, not the deploy.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Dispatch Pro.