Kubernetes Patch Tester

Paste an object and a patch, pick the --type you would pass to kubectl patch, and see the object you would end up with. The three types disagree about lists: switch between them on the same patch and the difference is the answer.

Nothing is uploaded and no cluster is contacted: the three algorithms run in this tab.

Common mistakes

These are the ones that fail silently. The config is accepted, nothing raises an error, and the consequence arrives later.

  1. Using a strategic merge patch on a custom resource

    Strategic merge relies on Go struct tags that only exist for built-in types. On a CRD it silently falls back to a JSON merge patch, which replaces lists instead of merging them.

    Instead:Use a JSON patch on custom resources, where the semantics are explicit.

  2. Expecting a merge patch to add to a list

    A JSON merge patch REPLACES an array wholesale. Patching one container in a list of three deletes the other two.

    Instead:Use a strategic merge patch with the right merge key, or a JSON patch with an index.

  3. Setting a field to null to leave it alone

    In a merge patch, null means delete the key.

    Instead:Omit the field to leave it unchanged.

What it knows, and what it cannot know

Strategic merge is not a general algorithm. Everything below follows from where its rules are actually stored.

The merge keys are Go struct tags

Strategic merge knows containers merge on name and ports merge on containerPort because those strings are written into patchMergeKey tags on the API server's own types. They are not in the OpenAPI schema and they are not derivable from your object. Where a path is outside the table used here, this page says so rather than inventing a key.

A custom resource has no such tags

kubectl patch with the default type against a CustomResource comes back UnsupportedMediaType and changes nothing. kubectl apply and Kustomize fall back to JSON merge patch on the same object, so every list is replaced. Same field names, same YAML, opposite result, and nothing on the object hints at it.

Atomic lists that look mergeable

tolerations, command, args, envFrom, Ingress rules, Role rules and RoleBinding subjects carry no merge key, so even the strategic default replaces them wholesale. Patching one toleration onto a Deployment discards the ones already there.

finalizers go the other way

metadata.finalizers is patchStrategy merge on a list of plain strings, which means strategic merge unions the two lists and can only ever add. That is why patching finalizers to [] appears to succeed and leaves the namespace stuck, and why the answer is --type=merge.

JSON Pointer eats your annotation keys

RFC 6901 uses / as its separator, so kubernetes.io/ingress.class has to be written kubernetes.io~1ingress.class. Unescaped, the pointer splits into two segments and the operation fails as a missing path. When that happens here, the corrected pointer is printed.

Rejected means nothing applied

A JSON Patch is atomic. A remove on a path that is not there, an index past the end of a list, or a failing test aborts the whole request, including the operations before it that would have worked on their own. The verdict names the operation and the reason.

The limits, stated rather than implied

This applies the patch algorithm and nothing else. It does not run admission or mutating webhooks, does not apply API server defaulting, and does not validate the result against your cluster's schema, so a result shown here can still be rejected on submission. The merge keys come from a table of the paths people actually patch rather than from the full API surface: a list at a path outside it is reported as unmodelled, with the JSON merge behaviour shown. When you have cluster access, kubectl patch --dry-run=server is the authoritative answer, because it asks the API server that owns the struct tags.

kubectl patch has three algorithms and picks the dangerous one by default

kubectl patch --type takes strategic, merge or json. They agree on almost everything and disagree on lists, which is the only part anyone actually patches. The difference is not a preference: one of them will delete every container except the one you named.

JSON Merge Patch replaces lists, always

RFC 7386 is four rules long. Mappings merge recursively, a null value deletes a key, and anything else replaces what is there. A list is anything else. So a patch naming one container by name does not update that container: it replaces the containers array with a one-element array, and everything that was in the others is gone, along with every field of the named one that the patch did not restate.

original                        --type=merge -p
containers:                     containers:
  - name: app                     - name: app
    image: nginx:1.25               image: nginx:1.27
    env: [LOG_LEVEL=info]
  - name: metrics               result
    image: statsd:v0.26           containers:
                                    - name: app
                                        image: nginx:1.27

env gone. metrics gone. Exit code 0.

Strategic merge merges lists by a key you cannot see

The Kubernetes-aware type reads patchStrategy and patchMergeKey struct tags off the Go types compiled into the API server. containers, initContainers, volumes and imagePullSecrets merge on name; ports on containerPort; env on name; volumeMounts on mountPath; hostAliases on ip; ownerReferences on uid. The same patch that destroyed the list above updates one container and leaves the rest untouched. This is the default for kubectl patch, and it is what kubectl apply and Kustomize use.

Container.Ports  patchMergeKey:"containerPort"
ServiceSpec.Ports  patchMergeKey:"port"

Both are called "ports". Merging a Service port
list on containerPort matches nothing and appends
a duplicate entry instead.

Which lists do not merge, and it is not obvious

A field with no patchStrategy tag is atomic: strategic merge replaces it like a merge patch would. tolerations, command, args, envFrom, capabilities.add, Ingress rules and tls, Role rules, RoleBinding subjects and HPA metrics are all in this group. Meanwhile metadata.finalizers is tagged merge on a list of strings, which unions rather than replaces, so a strategic patch can add a finalizer and can never remove one.

kubectl patch ns stuck -p '{"metadata":{"finalizers":[]}}'
namespace/stuck patched            <- and unchanged

kubectl patch ns stuck --type=merge -p '{"metadata":{"finalizers":[]}}'
namespace/stuck patched            <- actually empty

JSON Patch is precise, positional and brittle

RFC 6902 is a list of operations: add, remove, replace, move, copy and test, each addressing a location by JSON Pointer. It is the only type that can remove one element from a list without restating the rest. The cost is that /spec/template/spec/containers/0 means whichever container is first today, so a patch stored in a repository breaks the moment a sidecar is injected ahead of it. add with an index inserts rather than overwrites, and the whole patch is rejected atomically if any operation misses.

- op: test           guard against a concurrent change
  path: /spec/replicas
  value: 2
- op: replace
  path: /spec/template/spec/containers/0/image
  value: nginx:1.27

test fails  ->  nothing is applied, exit 1

JSON Pointer escaping, which is where Kubernetes patches actually fail

In RFC 6901 a pointer segment escapes / as ~1 and ~ as ~0, and nothing else. Almost every annotation and label key in Kubernetes contains a slash, so almost every pointer at one needs escaping. Unescaped, the pointer splits into two segments and the operation reports the field as missing, which reads like the annotation is not there rather than like a syntax error. Expand ~1 before ~0 when decoding, or ~01 comes back as a slash instead of as the literal ~1 it means.

wrong  /metadata/annotations/kubernetes.io/ingress.class
right  /metadata/annotations/kubernetes.io~1ingress.class

wrong  /metadata/labels/app.kubernetes.io/name
right  /metadata/labels/app.kubernetes.io~1name

And none of it applies to a custom resource

Strategic merge exists only for kinds whose Go types are compiled into the API server. A CustomResource has no such type, so the API server refuses a strategic patch with UnsupportedMediaType and kubectl patch fails outright. Client-side kubectl apply and Kustomize fall back to JSON merge patch for the same reason, which means a Kustomize patch against an Argo CD Application or a Prometheus behaves like the destructive column above while the identical patch against a Deployment behaves like the safe one. Server-side apply is the exception: it reads listType and listMapKey markers out of the CRD's own schema, so it does merge lists on a custom resource.

kubectl patch prometheus main -p '{"spec":{"replicas":3}}'
Error from server (UnsupportedMediaType): the body of the
request was in an unknown format

kubectl patch prometheus main --type=merge -p '...'   works
kubectl patch prometheus main --type=json -p '...'    works
kubectl apply --server-side -f ...                    merges lists

More kubernetes tools

Kubernetes YAML Generator Answer a few questions, get a manifest Kubernetes YAML Validator Is this YAML valid, and where is it wrong? Kubernetes Rollout & Probe Timing Restart loops, capacity dips and stuck drains Kubernetes Service Checker Will this Service have any endpoints? Kubernetes Network Policy Viewer What can reach what, and what just broke Kubernetes Manifest Diff What actually changed Kubernetes Resource Calculator What these manifests actually reserve Kubernetes RBAC Viewer Who can actually do what Kubernetes Deployment Generator See the capacity dip before you deploy Kubernetes Service Generator port, targetPort, and which one you meant Kubernetes StatefulSet Generator Stable names, and volumes that outlive you Kubernetes QoS Class Calculator And where you sit in the eviction order Kubernetes Node Capacity Planner Allocatable is not capacity Kubernetes Max Pods per Node Calculator The ENI limit, not the 110 default Kubernetes Label Selector Tester Which key failed, not just that one did Kubernetes Taint & Toleration Tester Tolerations permit, they do not attract Kubernetes Resource Name Validator RFC 1123 subdomain, label, or 1035 Kubernetes Label & Annotation Validator Why your image tag is not a legal label Kubernetes CrashLoopBackOff Guide The status is a timer, not a cause Kubernetes TLS Certificate Decoder When it expires, and what it covers Kubernetes ServiceAccount Token Decoder Bound or legacy, and when it dies Kubernetes imagePullSecret Generator The registry URL nobody gets right Kubernetes cert-manager Certificates Issuer scope, and the rate limit that costs a week Kubernetes Flux HelmRelease Generator With the source it cannot work without Kubernetes Helm Chart Generator Chart.yaml, values, and working templates Kubernetes Argo CD Application Generator The defaults that are off, turned on Kubernetes Ingress Generator pathType, and the TLS secret that fails quietly Kubernetes HPA Generator Why it says unknown and never scales Kubernetes SecurityContext Generator Pod level, container level, and which wins Kubernetes Probe Generator How long it gets to start before liveness kills it Kubernetes PVC Generator Access modes your storage can actually do Kubernetes ResourceQuota Generator And the LimitRange that stops it breaking deploys Kubernetes CronJob Generator The fields that decide whether it runs Kubernetes NetworkPolicy Generator With the DNS rule already in it Kubernetes RBAC Generator And what it actually grants Kubernetes PodDisruptionBudget Generator Can this budget ever be satisfied? Kubernetes API Deprecation Checker What breaks on the next upgrade Kubernetes Kubeconfig Viewer Read it without handing it to anyone Kubernetes Quantity Parser What 512Mi, 100m and 1e3 actually mean Kubernetes Multi-Document YAML Splitter One file per object, in apply order Kubernetes .env to ConfigMap & Secret Split the credentials out on the way Kubernetes Secret Decoder See what is actually in there Kubernetes Ingress to Gateway API Which annotations silently stop working Kubernetes Manifest Visualizer What points at what, and what points at nothing Kubernetes kubectl JSONPath Tester kubectl's dialect, not generic JSONPath Kubernetes Node Affinity Tester Required excludes, preferred only scores Kubernetes Helm Values Diff A missing key is a third value Kubernetes Pod and Service CIDR Fixed at cluster creation Kubernetes Secret Encoder base64 is not encryption Kubernetes API Version Checker Will this apply on that cluster Kubernetes Go Template Tester Go truthiness is not JavaScript's Kubernetes Cluster Cost Estimator At your rates, not a price table Kubernetes Kustomize Build Transformers run in kustomize's order Kubernetes Helm Chart Validator version is SemVer, appVersion is not

Elsewhere on the site