Patch Engine
Patch - Declarative Resource Patching
The patch package provides a JSONPath-based system for declaratively modifying Kubernetes resources. It supports both TOML and YAML patch file formats with structure-preserving modifications and variable substitution.
Overview
Patches allow you to modify Kubernetes manifests without rewriting them. The system uses JSONPath expressions to target specific fields and applies changes while preserving the original YAML structure (comments, ordering, formatting).
Patch File Formats
TOML Format (.kpatch)
YAML Format
Quick Start
Key Features
Variable Substitution
Patches support variable references that resolve against a parameter context:
List Selectors
Target specific items in lists using selectors:
Structure Preservation
When using NewPatchableAppSetWithStructure, the original YAML document structure is preserved through patching, maintaining comments, key ordering, and formatting.
Strategic Merge Patch
For broad document-level changes, strategic merge patch (SMP) deep-merges a partial YAML document into the target resource. Known Kubernetes kinds merge lists by key (e.g. containers by name); unknown kinds fall back to JSON merge patch (RFC 7386).
SMP patches are applied before field-level patches. See DESIGN.md for full specification.
API Reference
Loading Patches
| Function | Description |
|---|---|
LoadPatchFile(r) | Load with automatic format detection |
LoadPatchFileWithVariables(r, ctx) | Load with variable substitution |
LoadTOMLPatchFile(r, ctx) | Load TOML-format patches |
LoadYAMLPatchFile(r, ctx) | Load YAML-format patches |
Applying Patches
| Function | Description |
|---|---|
NewPatchableAppSet(resources, patches) | Create patchable set |
NewPatchableAppSetWithStructure(docSet, patches) | Create with structure preservation |
ParsePatchLine(path, value) | Parse a single patch operation |
Strategic Merge Patch
| Function | Description |
|---|---|
ApplyStrategicMergePatch(resource, patch, lookup) | Apply SMP to a single resource |
DefaultKindLookup() | Create a KindLookup from the built-in scheme |
DetectSMPConflicts(patches, lookup, gvk) | Check pairwise conflicts among patches |
ResolveWithConflictCheck() | Resolve patches with conflict detection |
Related Packages
- pkg/oam - Uses patches in the kurel package/build pipeline
- kure pkg/io - YAML parsing for patch targets