Kustomize
patches
patchesJson6902
Kubernetes
configuration-management

What is the difference between patches vs patchesJson6902 in Kustomize

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In the world of Kubernetes configuration management, tools like Kustomize are essential for managing and customizing Kubernetes objects. Kustomize offers several patching strategies, including patches and patchesJson6902. Both serve the purpose of modifying Kubernetes resources, but they operate in different ways and each has unique use cases and constraints. Understanding the difference between these patching methods is crucial for effectively utilizing Kustomize in your DevOps workflows.

Patches in Kustomize

The patches directive in Kustomize is used for strategic merge patching, which is a Kubernetes-specific patching format. This method allows you to specify changes to be applied to a Kubernetes resource without having to redefine the entire resource configuration.

Key Features

  • Strategic Merge: patches uses a strategic merge strategy specific to Kubernetes resources, making it more suited for Kubernetes objects.
  • Selective Patching: You can specify which parts of a resource to patch, based on the resource's existing structure.
  • Preserve Intent: It's designed to maintain the intention described in the patch when applied over the base resources.

Example Usage

Suppose you have a base Deployment configuration and you want to append additional labels:

Base Deployment Configuration

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: my-deployment
5  labels:
6    app: my-app
7spec:
8  replicas: 3
9  template:
10    metadata:
11      labels:
12        app: my-app
13    spec:
14      containers:
15      - name: my-container
16        image: nginx

Strategic Merge Patch

yaml
1patches:
2- target:
3    kind: Deployment
4    name: my-deployment
5  patch: |-
6    metadata:
7      labels:
8        version: v1
9    spec:
10      template:
11        metadata:
12          labels:
13            version: v1

Considerations

  • Exchange Precision for Conciseness: While it simplifies patching, there might be limitations when it comes to specifying certain exceptional structures due to its schema-driven nature.

PatchesJson6902 in Kustomize

The patchesJson6902 feature of Kustomize utilizes the JSON 6902 patch format, which is a JSON-based patching standard. This format is more flexible than strategic merge patches.

Key Features

  • Generic JSON Formatting: It's independent of Kubernetes-specific nuances, which makes it helpful for non-standard object modifications.
  • Full Object Specification: Unlike strategic patches, patchesJson6902 can express changes in any JSON-compatible format.
  • Patch Opacity: You have full control over the exact patch operations, like add, remove, and replace, as specified in JSON 6902.

Example Usage

With JSON 6902, appending a label works in a different way:

yaml
1patchesJson6902:
2- target:
3    group: apps
4    version: v1
5    kind: Deployment
6    name: my-deployment
7  path: labels-patch.json

Labels-Patch.json

json
1[
2  { "op": "add", "path": "/metadata/labels/version", "value": "v1" },
3  { "op": "add", "path": "/spec/template/metadata/labels/version", "value": "v1" }
4]

Considerations

  • Stringent Format: Requires adhering strictly to JSON 6902 syntax, possibly leading to verbosity.
  • No Schema Awareness: The operation is manually specified, thus potentially more error-prone in complex scenarios.

Comparison Table

FeaturePatchesPatchesJson6902
Type of PatchStrategic Merge PatchJSON Patch 6902
Schema AwarenessYesNo
Complexity ManagementEasier for simple, schema-based modificationsFlexible for complex changes
Patch Operations SupportedMergingAdd, Remove, Replace, etc.
Usefulness for Non-standard ObjectsLimitedHigh
Error PronenessLower (auto-schema handling)Higher (manual operation)
VerbosityConcisePotentially verbose

Additional Considerations

When to Use Patches vs PatchesJson6902

  • Use patches when:
    • You need to make simple adjustments fitting within Kubernetes' strategic merge patch semantics.
    • You want schema alignment and automatic merging of nested objects.
  • Use patchesJson6902 when:
    • You require fine-grained control over patch operations.
    • You're dealing with non-Kubernetes-specific resources or when the schema is unknown or unstructured.
    • You need to perform complex operations not expressible by strategic merge (like array manipulation).

Conclusion

Both patches and patchesJson6902 have particular strengths making them suitable for different patching needs in Kubernetes object management. An understanding of these differences and when to apply each method can significantly enhance the efficiency and precision of your Kubernetes configurations using Kustomize.


Course illustration
Course illustration

All Rights Reserved.