Updating a Key in a Dictionary
Using a JSON 6902 Patch
Consider the following Deployment configuration:"component: api" to "component: web" using a JSON 6902 patch, include this patch in your kustomization.yaml:
/spec/template/metadata/labels/component and replaces its value with “web”.
The JSON 6902 patch method provides precise control when updating complex configurations. Choose the patch type that best fits your needs.
Using a Strategic Merge Patch
Alternatively, you can update the label using a strategic merge patch stored in a separate file, for example,label-patch.yaml. Your main Deployment configuration remains unchanged, and your kustomization.yaml is updated as follows:
label-patch.yaml:
Adding a New Key to a Dictionary
Using a JSON 6902 Patch
Suppose you want to add a new label"org" with the value "KodeKloud" while keeping the original "component: api" label. Start with the following Deployment:
kustomization.yaml:
add operation to insert the new key "org" with the specified value into the labels dictionary.
Using a Strategic Merge Patch
For a strategic merge patch, create a separate file (e.g.,label-patch.yaml) with the following content:
kustomization.yaml:
"component: api" and "org: KodeKloud".
When adding new keys, always verify that the target dictionary exists to avoid runtime errors.
Removing a Key from a Dictionary
Using a JSON 6902 Patch
Assume you have a Deployment configuration that includes two labels:"org" label using a JSON 6902 patch, modify your kustomization.yaml as follows:
Using a Strategic Merge Patch
To remove the"org" label via a strategic merge patch, create a patch file (e.g., label-patch.yaml) with the following content:
kustomization.yaml:
null value as an instruction to remove the "org" label from the original configuration.
Ensure that you specify the correct path for removal operations to avoid inadvertently deleting other keys in the configuration.
With these examples, you now understand how to update, add, and remove keys in a Kubernetes Deployment configuration using both JSON 6902 patches and strategic merge patches. For more detailed information on Kubernetes configurations and patch strategies, visit the Kubernetes Documentation.