This lesson explains how patches enable targeted modifications of Kubernetes configurations, allowing precise updates to specific objects rather than applying global changes.
In this lesson, we explain how patches offer a surgical approach for modifying Kubernetes configurations. Unlike common transformers that are efficient for applying global configuration changes—such as adding labels or setting namespaces—patches allow you to target one or a few specific objects. For example, if you need to update the replica count in a particular deployment, a customized patch permits you to precisely match and change the targeted object.
To create a patch, you need to provide three essential parameters:
Operation Type: Determines the action performed on a resource. The most commonly used operations are:
add: Inserts a new element. For instance, when adding a container to a list in a deployment.
remove: Deletes an element, such as removing a container or a label.
replace: Swaps an existing value with a new one. For example, changing the replica count from 5 to 10.
Target: Specifies the criteria to identify the exact Kubernetes object(s) to patch. You can filter objects based on properties like:
kind
version
name
namespace
label selector
annotation selector
These criteria can be combined to narrow down your selection.
Value: Represents the new value to be applied or used for replacement. Note that for removal operations, no value is provided since the intention is to delete the target property.
When updating configurations, ensure that your patch accurately targets the intended resource to prevent unintended changes.
Let’s review another scenario. Suppose the original deployment has one replica, and you want to increase it to five. The initial configuration remains:
This approach resembles standard Kubernetes configuration files. You provide only the parts of the configuration you intend to modify, and Kustomize merges these changes with the existing configuration. For example:
Both methods effectively update Kubernetes resources. The choice between JSON 6902 and strategic merge patches is based on personal preference, with many opting for the readability of strategic merge patches.
This lesson provided an overview of using patches with Kustomize to perform precise modifications on your Kubernetes configurations. By selecting the appropriate patch type, you can update labels, names, replica counts, and other fields with confidence and ease.For additional details about patching and Kustomize, check out the Kustomize documentation and explore more in the Kubernetes Documentation.