Patches in Kustomize: JSON 6902 vs Strategic Merge Patch

Patches in Kustomize: JSON 6902 vs Strategic Merge Patch

One of its key features in Kustomize is the ability to modify existing resource manifests through the use of patches. Kustomize supports two different types of patching mechanisms: JSON 6902 Patch and Strategic Merge Patch. These patching techniques allow you to tailor your Kubernetes resource definitions to suit your specific needs, enabling greater flexibility and reusability.

The JSON 6902 Patch is a standardized approach based on RFC 6902, which defines a JSON Path-based syntax for modifying JSON data. In the context of Kubernetes and Kustomize, it provides a versatile way to modify specific fields in your resource manifests using JSON Path expressions. This patch type offers a wide range of operations, including adding, removing, replacing, moving, copying, and testing fields, making it a potent tool for complex modifications.

On the other hand, the Strategic Merge Patch is a Custom Resource Definition (CRD) introduced by Kubernetes to handle updates to existing resources. It takes a more concise and intuitive approach by allowing you to specify only the fields you want to change, rather than defining the entire patch operation. This patch type is particularly well-suited for simple modifications, such as changing field values or adding/modifying nested fields, as it promotes better readability and maintainability.

When working with Kustomize, choosing between JSON 6902 Patch and Strategic Merge Patch largely depends on the complexity of your modifications and your personal preference. While JSON 6902 Patch offers more flexibility and power for advanced use cases, Strategic Merge Patch provides a more streamlined experience for simpler scenarios. Ultimately, the decision should be based on striking the right balance between flexibility and readability for your specific requirements.

Patching with Kustomize

Patching is a fundamental concept in Kustomize, and it allows you to modify existing Kubernetes resource configurations. Kustomize supports two types of patching:

JSON 6902 Patch

The JSON 6902 patch is a standard defined in RFC 6902, which describes a JSON Path-based syntax for modifying JSON data. In the context of Kubernetes and Kustomize, it allows you to modify specific fields in your resource manifests using JSON Path expressions.

Here's an example of how you might use a JSON 6902 patch in a Kustomize overlay:

# patch.yaml
apiVersion: kustomize.config.k8s.io/v1
kind: PatchJson6902
metadata:
  name: patch-deployment
target:
  group: apps
  version: v1
  kind: Deployment
  name: my-deployment
patch: |-
  - op: replace
    path: /spec/replicas
    value: 3        

In this example, the patch.yaml file defines a JSON 6902 patch that targets the Deployment resource named my-deployment. The patch operation replace is used to change the value of the /spec/replicas field to 3.

JSON 6902 patches are powerful and allow you to perform various operations like add, remove, replace, move, copy, and test. However, they can be verbose and harder to read and maintain, especially for complex modifications.

Strategic Merge Patch

The Strategic Merge Patch is a Custom Resource Definition (CRD) introduced by Kubernetes to handle updates to existing resources. It provides a more concise and intuitive way to modify resource configurations by specifying only the fields you want to change.

Here's an example of how you might use a Strategic Merge Patch in a Kustomize overlay:

 # patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3        

In this example, the patch.yaml file defines a Strategic Merge Patch that targets the Deployment resource named my-deployment. It specifies that the spec.replicas field should be set to 3.

Strategic Merge Patches are generally easier to read and maintain, especially for simple modifications. However, they have some limitations, such as not being able to remove fields or perform more complex operations like array element manipulation.

Choosing Between JSON 6902 and Strategic Merge Patch

When working with Kustomize, you should choose the patch type based on the complexity of your modifications and your personal preference. Here are some general guidelines:

Use Strategic Merge Patch for simple modifications like changing field values or adding/modifying nested fields. Strategic Merge Patches are easier to read and maintain for these scenarios.

Use JSON 6902 Patch for more complex modifications like removing fields, manipulating arrays, or performing conditional operations. JSON 6902 patches provide more flexibility and power for these advanced use cases.

It's worth noting that you can use both patch types together in a single Kustomize overlay if needed.

Conclusion

Kustomize's patching capabilities, through JSON 6902 Patch and Strategic Merge Patch, offer a powerful toolset for tailoring Kubernetes resource configurations to meet diverse requirements. The JSON 6902 Patch, with its JSON Path-based syntax and comprehensive set of operations, empowers users to perform intricate modifications on their resource manifests. Its versatility makes it an indispensable choice for complex scenarios, such as removing fields, manipulating arrays, or executing conditional operations. Conversely, the Strategic Merge Patch presents a more concise and intuitive approach, excelling in handling simple modifications like changing field values or adding/modifying nested fields. Its emphasis on readability and maintainability makes it a preferred option when dealing with straightforward updates.

Ultimately, the choice between these two patching mechanisms should be guided by the specific requirements of the project and the personal preferences of the user. Kustomize's flexibility allows for the seamless integration of both patch types within a single overlay, enabling users to leverage the strengths of each approach as needed. By mastering the nuances of these patching techniques, Kubernetes administrators and developers can unlock a world of possibilities, streamlining the management of their resource configurations and paving the way for more efficient and scalable deployments.

To view or add a comment, sign in

More articles by Christopher Adamson

Insights from the community

Others also viewed

Explore topics