Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

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 show

Fix it fast

Most common cause, 1 of 3

A 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.

Fix
patches:
  - target:
      kind: Deployment
      name: my-app
    patch: |-
      - op: add
        path: /metadata/annotations
        value:
          example: "true"
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to kustomize 5.8.3 (json-patch v4.13.0), kubectl 1.37 (kubectl kustomize), JSON Patch per RFC 6902
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
kubectl kustomize overlays/dev
kubectl kustomize base

Then find the line you see in this list and open its fix:

  1. 1A parent object on the path does not exist in the resourceA segment before the last one, such as annotations or nodeSelector, is absent from the rendered base
  2. 2A key with a slash in it was not escapedThe path holds a label or annotation key such as app.kubernetes.io/version written with a plain slash, not ~1
  3. 3The target selector also matches a resource without that fieldThe 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 value
  • replace operation does not apply: doc is missing path: /spec/template/spec/nodeSelector/disktype: missing value
  • remove 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.

Check
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.

Fix
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.

Verify
kubectl kustomize overlays/dev | grep -n -A2 'annotations:'

Confirmed by rfc-editor.org

Did this fix it?

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.

Check
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.

Fix
- op: add
  path: /metadata/labels/app.kubernetes.io~1version
  value: 1.21.0

Verify

The label appears with its full name in the rendered output.

Verify
kubectl kustomize overlays/dev | grep -n 'app.kubernetes.io/version'

Confirmed by kubectl.docs.kubernetes.io

Did this fix it?

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.

Check
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.

Fix
target:
  group: apps
  version: v1
  kind: Deployment
  name: my-app

Verify

The build passes and only the named resource carries the change.

Verify
kubectl kustomize overlays/dev

Confirmed by kubectl.docs.kubernetes.io

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.