-
Operation Type
This specifies the action you want to perform. The three most common operations are:- add: Introduces a new element. For instance, adding a container to an existing list.
- remove: Eliminates an existing element, such as taking away a container or label.
- replace: Substitutes an existing value with a new one. For example, changing the replica count from 5 to 10.
-
Target
The target defines the match criteria for determining which Kubernetes object(s) the patch should apply to. This may include properties such as kind, version, name, namespace, label selectors, or annotation selectors. You can combine multiple criteria to precisely identify the desired resource(s). -
Value
This represents the new data to add or use for replacement. Note that if the operation isremove, no value is required.
Consider the following sample deployment configuration in
deployment.yaml, where the deployment’s name is currently set to “api-deployment”. The goal is to update it to “web-deployment”.
kustomization.yaml is shown below:
- The
|-introduces the patch details. opspecifies the operation (in this case, replace).pathpoints to the YAML property you want to change – here,/metadata/name.valueprovides the new value (“web-deployment”).
Patches are especially useful when you need to make precise, minimal modifications without affecting the entire configuration. This helps in managing changes in large-scale deployments.
Example: Updating the Replica Count
Let’s explore another scenario. Suppose you want to update the replica count from 1 to 5 in your deployment. The originaldeployment.yaml looks like this:
kustomization.yaml as follows:
/spec/replicas pinpoints the replica count field under the spec, which is then updated with the new value.
Two Methods for Defining Patches
Kustomize supports two primary methods for defining patches:1. JSON 6902 Patch
This method explicitly specifies both the target and patch details using JSON patch semantics. For example:2. Strategic Merge Patch
This approach resembles a standard Kubernetes configuration file. You provide a snippet that mirrors the original resource configuration and includes only the fields to be updated:Both JSON 6902 and strategic merge patches are valid options. Choose JSON 6902 for detailed control when you need explicit patch operations, or opt for strategic merge patches for a more readable and Kubernetes-native configuration style.
Summary
Patches in Kustomize allow for precise modifications to Kubernetes objects by specifying:- Operation Type: Determines whether to add, remove, or replace a value.
- Target: Defines the resource(s) the patch applies to using specific matching criteria.
- Value: Contains the updated data or new configuration value.