When you change labels on a Pod template, consider whether
spec.selector.matchLabels also needs to be updated so the Deployment continues to select the updated Pods. Failing to update the selector may cause unexpected Deployment behavior.Summary: When to use JSON 6902 vs Strategic Merge Patch
Replace a key in a dictionary using JSON 6902
Original Deployment (api-deployment.yaml):component label under the Pod template:
component label becomes web while the Deployment’s spec.selector.matchLabels still contains the original api value (update the selector if you intend to match the new label).
Replace a key in a dictionary using a Strategic Merge Patch
Use an external patch file and reference it fromkustomization.yaml.
kustomization.yaml:
label-patch.yaml into the original Deployment, resulting in the Pod template label component: web (again, update spec.selector.matchLabels if you need the selector to match the updated Pods).
Resulting fragment:
Add a new key in a dictionary using JSON 6902
Original Deployment (api-deployment.yaml):org: KodeKloud under the Pod template:
component: api and org: KodeKloud.
Add a new key in a dictionary using a Strategic Merge Patch
Using an external patch file referenced bykustomization.yaml:
kustomization.yaml:
Remove a key from a dictionary using JSON 6902
Original Deployment (api-deployment.yaml) contains two labels:org label:
org label is removed from the Pod template; only component: api remains.
Remove a key from a dictionary using a Strategic Merge Patch
With Strategic Merge Patch you indicate deletion by setting a key tonull in the patch file.
kustomization.yaml:
org label):
org: null as “remove this key” and merges accordingly, yielding:
Best practices and tips
- Use JSON 6902 for precise, targeted operations when you need to guarantee the exact change (add/replace/remove a single field).
- Use Strategic Merge Patch for declarative, easy-to-read patches that merge cleanly with existing objects.
- Always re-evaluate
spec.selector.matchLabelswhen changing Pod template labels to ensure Deployment selection remains correct. - Keep patch files small and focused — include only the identifying fields and the fields you intend to change.