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:
patchesuses 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
Strategic Merge Patch
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,
patchesJson6902can 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:
Labels-Patch.json
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
| Feature | Patches | PatchesJson6902 |
| Type of Patch | Strategic Merge Patch | JSON Patch 6902 |
| Schema Awareness | Yes | No |
| Complexity Management | Easier for simple, schema-based modifications | Flexible for complex changes |
| Patch Operations Supported | Merging | Add, Remove, Replace, etc. |
| Usefulness for Non-standard Objects | Limited | High |
| Error Proneness | Lower (auto-schema handling) | Higher (manual operation) |
| Verbosity | Concise | Potentially verbose |
Additional Considerations
When to Use Patches vs PatchesJson6902
- Use
patcheswhen:- You need to make simple adjustments fitting within Kubernetes' strategic merge patch semantics.
- You want schema alignment and automatic merging of nested objects.
- Use
patchesJson6902when:- 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.

