Documentation
¶
Overview ¶
Package patch provides declarative patching of Kubernetes resources using a simple, structured syntax without templates or overlays.
This package enables tools to modify Kubernetes manifests through patches that target specific fields and list items using dot-notation paths with smart selectors.
Quick Start ¶
Load resources and apply patches:
// Load base Kubernetes resources
resources, err := patch.LoadResourcesFromMultiYAML(resourceFile)
if err != nil {
return err
}
// Load patch specifications
patches, err := patch.LoadPatchFile(patchFile)
if err != nil {
return err
}
// Create patchable set and apply
set, err := patch.NewPatchableAppSet(resources, patches)
if err != nil {
return err
}
resolved, err := set.Resolve()
if err != nil {
return err
}
for _, r := range resolved {
if err := r.Apply(); err != nil {
return err
}
}
Patch Syntax ¶
Both YAML and TOML patch formats are supported with automatic detection:
# YAML format
spec.replicas: 3
spec.containers[name=main].image: nginx:latest
spec.ports[+name=https]: {name: https, port: 443}
# TOML format
[deployment.app]
spec.replicas: 3
[deployment.app.containers.name=main]
image: nginx:latest
resources.requests.cpu: 100m
Core Types ¶
PatchableAppSet - Manages resources and their patches PatchOp - Individual patch operation PathPart - Structured path component TOMLHeader - Parsed TOML section header
Detailed Documentation ¶
For comprehensive information, see the markdown documentation:
- DESIGN.md - Complete syntax reference and examples
- PATCH_ENGINE_DESIGN.md - Architecture and implementation details
- PATH_RESOLUTION.md - Advanced path resolution and type inference
- ERROR_HANDLING.md - Error handling patterns and debugging
Debugging ¶
Enable detailed logging:
export KURE_DEBUG=1
Index ¶
- Variables
- func ApplyPatch(basePath, patchPath string) ([]*unstructured.Unstructured, error)
- func GenerateOutputFilename(originalPath, patchPath, outputDir string) string
- func InferPatchOp(path string) string
- func IsTOMLFormat(content string) bool
- func LoadResourcesFromMultiYAML(r io.Reader) ([]*unstructured.Unstructured, error)
- func SubstituteVariables(value string, ctx *VariableContext) (interface{}, error)
- type PatchOp
- type PatchSpec
- func LoadPatchFile(r io.Reader) ([]PatchSpec, error)
- func LoadPatchFileWithVariables(r io.Reader, varCtx *VariableContext) ([]PatchSpec, error)
- func LoadTOMLPatchFile(r io.Reader, varCtx *VariableContext) ([]PatchSpec, error)
- func LoadYAMLPatchFile(r io.Reader, varCtx *VariableContext) ([]PatchSpec, error)
- type PatchableAppSet
- func LoadPatchableAppSet(resourceReaders []io.Reader, patchReader io.Reader) (*PatchableAppSet, error)
- func NewPatchableAppSet(resources []*unstructured.Unstructured, patches []PatchSpec) (*PatchableAppSet, error)
- func NewPatchableAppSetWithStructure(documentSet *YAMLDocumentSet, patches []PatchSpec) (*PatchableAppSet, error)
- type PathPart
- type RawPatchMap
- type ResourceWithPatches
- type Selector
- type TOMLHeader
- type TargetedPatch
- type VariableContext
- type YAMLDocument
- type YAMLDocumentSet
- func (set *YAMLDocumentSet) Copy() (*YAMLDocumentSet, error)
- func (set *YAMLDocumentSet) FindDocumentByKindAndName(kind, name string) *YAMLDocument
- func (set *YAMLDocumentSet) FindDocumentByName(name string) *YAMLDocument
- func (set *YAMLDocumentSet) GetResources() []*unstructured.Unstructured
- func (set *YAMLDocumentSet) WriteToFile(filename string) error
Constants ¶
This section is empty.
Variables ¶
var Debug = os.Getenv("KURE_DEBUG") == "1"
Functions ¶
func ApplyPatch ¶
func ApplyPatch(basePath, patchPath string) ([]*unstructured.Unstructured, error)
ApplyPatch loads resources and patch instructions from the provided file paths and returns the patched resources.
func GenerateOutputFilename ¶
GenerateOutputFilename creates the output filename based on the pattern <outputDir>/<originalname>-patch-<patchname>.yaml
func InferPatchOp ¶
InferPatchOp infers a patch operation based on the path syntax.
func IsTOMLFormat ¶
IsTOMLFormat detects if the content appears to be TOML-style patch format
func LoadResourcesFromMultiYAML ¶
func LoadResourcesFromMultiYAML(r io.Reader) ([]*unstructured.Unstructured, error)
func SubstituteVariables ¶
func SubstituteVariables(value string, ctx *VariableContext) (interface{}, error)
SubstituteVariables replaces ${values.key} and ${features.flag} patterns with actual values
Types ¶
type PatchOp ¶
type PatchOp struct {
Op string `json:"op"`
Path string `json:"path"`
ParsedPath []PathPart `json:"patsedpath,omitempty"`
Selector string `json:"selector,omitempty"`
Value interface{} `json:"value"`
}
PatchOp represents a single patch operation to apply to an object.
func ParsePatchLine ¶
ParsePatchLine converts a YAML patch line of form "path[selector]" into a PatchOp.
func (*PatchOp) NormalizePath ¶
NormalizePath parses the Path field and stores the result in ParsedPath.
func (*PatchOp) ValidateAgainst ¶
func (p *PatchOp) ValidateAgainst(obj *unstructured.Unstructured) error
ValidateAgainst checks that the patch operation is valid for the given object.
type PatchSpec ¶
PatchSpec ties a parsed PatchOp to an optional explicit target.
func LoadPatchFileWithVariables ¶
func LoadPatchFileWithVariables(r io.Reader, varCtx *VariableContext) ([]PatchSpec, error)
func LoadTOMLPatchFile ¶
func LoadTOMLPatchFile(r io.Reader, varCtx *VariableContext) ([]PatchSpec, error)
func LoadYAMLPatchFile ¶
func LoadYAMLPatchFile(r io.Reader, varCtx *VariableContext) ([]PatchSpec, error)
type PatchableAppSet ¶
type PatchableAppSet struct {
Resources []*unstructured.Unstructured
DocumentSet *YAMLDocumentSet // Preserves original YAML structure
Patches []struct {
Target string
Patch PatchOp
}
}
PatchableAppSet represents a collection of resources together with the patches that should be applied to them.
func LoadPatchableAppSet ¶
func NewPatchableAppSet ¶
func NewPatchableAppSet(resources []*unstructured.Unstructured, patches []PatchSpec) (*PatchableAppSet, error)
NewPatchableAppSet constructs a PatchableAppSet from already loaded resources and parsed patch specifications.
func NewPatchableAppSetWithStructure ¶
func NewPatchableAppSetWithStructure(documentSet *YAMLDocumentSet, patches []PatchSpec) (*PatchableAppSet, error)
NewPatchableAppSetWithStructure constructs a PatchableAppSet with YAML structure preservation
func (*PatchableAppSet) Resolve ¶
func (s *PatchableAppSet) Resolve() ([]*ResourceWithPatches, error)
Resolve groups patches by their target resource and returns them as ResourceWithPatches objects.
func (*PatchableAppSet) WritePatchedFiles ¶
func (s *PatchableAppSet) WritePatchedFiles(originalPath string, patchFiles []string, outputDir string) error
WritePatchedFiles writes separate files for each patch set applied
func (*PatchableAppSet) WriteToFile ¶
func (s *PatchableAppSet) WriteToFile(filename string) error
WriteToFile writes the patched resources to a file while preserving structure
type PathPart ¶
PathPart represents one segment of a parsed patch path.
func ParsePatchPath ¶
ParsePatchPath parses a patch path with selectors into structured parts.
type RawPatchMap ¶
type RawPatchMap map[string]interface{}
type ResourceWithPatches ¶
type ResourceWithPatches struct {
Name string
Base *unstructured.Unstructured
Patches []PatchOp
}
ResourceWithPatches ties a base object with the patches that should be applied to it.
func (*ResourceWithPatches) Apply ¶
func (r *ResourceWithPatches) Apply() error
Apply executes all patches on the base object.
type Selector ¶
type Selector struct {
Type string // "index", "key-value", "bracketed"
Index *int
Key string
Value string
Bracketed string
}
Selector represents different types of selectors in TOML headers
type TOMLHeader ¶
TOMLHeader represents a parsed TOML-style header like [kind.name.section.selector]
func ParseTOMLHeader ¶
func ParseTOMLHeader(header string) (*TOMLHeader, error)
ParseTOMLHeader parses a TOML-style header into structured components Examples:
[deployment.app] → Kind: deployment, Name: app
[deployment.app.containers.name=main] → Kind: deployment, Name: app, Sections: [containers], Selector: {Key: name, Value: main}
[deployment.app.ports.0] → Kind: deployment, Name: app, Sections: [ports], Selector: {Index: 0}
[deployment.app.containers[image.name=main]] → Kind: deployment, Name: app, Sections: [containers], Selector: {Bracketed: image.name=main}
func (*TOMLHeader) ResolveTOMLPath ¶
func (h *TOMLHeader) ResolveTOMLPath() (resourceTarget, fieldPath string, err error)
ResolveTOMLPath converts a TOML header to resource target and field path
func (*TOMLHeader) String ¶
func (h *TOMLHeader) String() string
String returns a string representation of the TOML header
type TargetedPatch ¶
type VariableContext ¶
VariableContext holds variables for substitution
type YAMLDocument ¶
type YAMLDocument struct {
Node *yaml.Node
Resource *unstructured.Unstructured
Original string // Original YAML content with comments
Order int // Original position in file
}
YAMLDocument represents a single YAML document with preserved structure
func (*YAMLDocument) ApplyPatchesToDocument ¶
func (doc *YAMLDocument) ApplyPatchesToDocument(patches []PatchOp) error
ApplyPatchesToDocument applies patches to a YAML document while preserving structure
func (*YAMLDocument) UpdateDocumentFromResource ¶
func (doc *YAMLDocument) UpdateDocumentFromResource() error
UpdateDocumentFromResource updates a document's YAML node from its resource
type YAMLDocumentSet ¶
type YAMLDocumentSet struct {
Documents []*YAMLDocument
Separator string // Document separator (usually "---")
}
YAMLDocumentSet holds multiple YAML documents with preserved order and comments
func LoadResourcesWithStructure ¶
func LoadResourcesWithStructure(r io.Reader) (*YAMLDocumentSet, error)
LoadResourcesWithStructure loads YAML resources while preserving comments and order
func (*YAMLDocumentSet) Copy ¶
func (set *YAMLDocumentSet) Copy() (*YAMLDocumentSet, error)
Copy creates a deep copy of the YAMLDocumentSet
func (*YAMLDocumentSet) FindDocumentByKindAndName ¶
func (set *YAMLDocumentSet) FindDocumentByKindAndName(kind, name string) *YAMLDocument
FindDocumentByKindAndName finds a document by resource kind and name
func (*YAMLDocumentSet) FindDocumentByName ¶
func (set *YAMLDocumentSet) FindDocumentByName(name string) *YAMLDocument
FindDocumentByName finds a document by resource name
func (*YAMLDocumentSet) GetResources ¶
func (set *YAMLDocumentSet) GetResources() []*unstructured.Unstructured
GetResources returns the unstructured resources in order
func (*YAMLDocumentSet) WriteToFile ¶
func (set *YAMLDocumentSet) WriteToFile(filename string) error
WriteToFile writes the document set to a file with preserved structure