Replace contents of an item in a list using Kustomize
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Kustomize modifies Kubernetes YAML without templates by applying patches to base configurations. Replacing items in a list (like containers, volumes, or env vars) is one of the trickiest operations because YAML lists do not have keys — Kustomize uses Strategic Merge Patches that match list items by a merge key (usually name). For lists without merge keys, you need JSON 6902 patches that target items by array index.
Strategic Merge Patch (Match by Name)
Kubernetes strategic merge patches match list items by their merge key. For containers, the merge key is name:
JSON 6902 Patch (Match by Index)
For precise control or when items have no merge key, use JSON 6902 patches:
Adding Items to a List
The - at the end of the path means "append to the array."
Removing Items from a List
Replacing Environment Variables
Environment variables are a common list replacement scenario:
Inline Patches
For small changes, define patches inline in kustomization.yaml:
Using replacements (Kustomize 4.5+)
The replacements field provides a declarative way to copy values between resources:
Common Pitfalls
- Strategic merge patch replacing the entire list: If you include a list in a strategic merge patch but omit existing items, those items are not deleted — they remain because the merge matches by the
namekey. However, if you use a plain merge patch (not strategic), the entire list is replaced. - Wrong array index in JSON 6902: Array indices are zero-based. Targeting
/containers/1when there is only one container results inpath not found. Verify the base manifest's list length before patching by index. - Merge key mismatch: Strategic merge patches match containers by
name, volumes byname, ports bycontainerPort. If the name in your patch does not match any item in the base, a new item is added instead of replacing the existing one. - Patching items with no merge key: Some lists (like
args,command) have no merge key. Strategic merge patches replace the entire list in these cases. Use JSON 6902 patches for precise item-level control. - Order of patch application: Kustomize applies patches in the order listed in
kustomization.yaml. Later patches see the result of earlier ones. If two patches modify the same list item, the second patch operates on the already-modified version.
Summary
- Use strategic merge patches to replace list items by their merge key (usually
name) - Use JSON 6902 patches (
op: replace,op: add,op: remove) for index-based or precise list modifications - The
-path suffix appends to an array; numeric indices target specific positions - Environment variables and containers use
nameas the merge key for strategic merge patches - Verify base manifest structure with
kustomize buildbefore writing patches to ensure correct paths
Related reading
- Replication Controller VS Deployment in Kubernetes
- Requeue a kubernetes event in a non-blocking reconcile loop
- Required value must specify a volume type when statically provisioning PV
- Restart container within pod
- Replication Modes Definitions?
- Repository is not signed in docker build
- Restart pods when configmap updates in Kubernetes?
- Restart VMs in scale set in AKS Node Pool

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.