Patch and replace semantics
Choose the smallest safe change
A Deployment named catalog has two containers: catalog and metrics. The release team wants to change only the catalog image, preserving the metrics container and resource settings. A patch sends a change description rather than replacing the entire resource. Which patch format you choose controls how arrays, maps and missing values behave. All sample images are fictional; no mutation commands are run in this package.
Strategic merge for supported built-in types
For the built-in Deployment Pod template, the containers list uses name as a merge key. A strategic merge patch matching catalog changes its image and retains metrics and unrelated catalog fields. Not all lists use this strategy; inspect the resource schema. Strategic merge patch is not supported for resources defined through CRDs. The following file is a partial patch rather than a complete manifest.
spec: template: spec: containers: - name: catalog image: registry.example.invalid/catalog:v2JSON merge patch replaces arrays
JSON merge patch recursively merges object members, but a supplied array replaces the whole existing array. Reusing the preceding patch with --type=merge would leave only a catalog entry and discard other fields in that original entry. A null value in a merge patch removes the corresponding member. Use a narrow object-member patch for an annotation, for instance, and do not send a partial containers list unless replacement is intended.
kubectl --kubeconfig ./approved-lab.kubeconfig --context bytemind-lab -n api-lab patch deployment catalog --type=strategic --patch-file strategic.yaml --dry-run=server -o yamlJSON Patch addresses exact paths
JSON Patch uses an ordered list of operations such as test, add, remove and replace. Array paths use numeric indexes. If a concurrent edit moves metrics into index zero, the test below fails before the replacement can be persisted. The guard checks identity, not the previous image value; add another test if changing a particular old image is a requirement. JSON Pointer escapes ~ as ~0 and / as ~1 inside path segments.
[ { "op": "test", "path": "/spec/template/spec/containers/0/name", "value": "catalog" }, { "op": "replace", "path": "/spec/template/spec/containers/0/image", "value": "registry.example.invalid/catalog:v2" }]Replace, edit and focused commands
A normal replace submits a full resource representation. Preserve required fields and use a freshly read resourceVersion for optimistic concurrency; stale versions can produce HTTP 409. Re-read, recompute the intended change and retry with a bound rather than blindly resending old content. replace --force deletes and recreates an object, which can cause downtime or a new UID; it is not ordinary replace. edit opens live configuration in an editor. set image, label and annotate are convenient focused operations, but still mutate live state unless previewed. For GitOps or other declarative ownership, update the source of truth as well. Avoid direct modifications that the controller will undo.
Compare the results offline
Read deployment-live.json, strategic.yaml and image-patch.json. Predict both container names and the image of each after a strategic merge versus a JSON merge. Run the provided Python validator for a small subset of patch semantics. It is not a Kubernetes schema validator. A successful patch can still trigger a rollout whose application fails. Next: verify acceptance and readiness separately.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Patch and replace semantics — knowledge check
Reason about the offline API change fixtures.
Which patch type can merge built-in Deployment containers by name?
What does JSON merge do with a supplied array?
Which JSON Patch operation can guard against the wrong container index?
What should a writer do after a stale resourceVersion conflict?
What is a risk of replace --force?