package fieldspec

import "sigs.k8s.io/kustomize/api/filters/fieldspec"

Package fieldspec contains a yaml.Filter to modify a resource that matches the FieldSpec.

Index

Examples

Types

type Filter

type Filter struct {
	// FieldSpec contains the path to the value to set.
	FieldSpec types.FieldSpec `yaml:"fieldSpec"`

	// Set the field using this function
	SetValue filtersutil.SetFn

	// CreateKind defines the type of node to create if the field is not found
	CreateKind yaml.Kind

	CreateTag string
	// contains filtered or unexported fields
}

Filter possibly mutates its object argument using a FieldSpec. If the object matches the FieldSpec, and the node found by following the fieldSpec's path is non-null, this filter calls the setValue function on the node at the end of the path. If any part of the path doesn't exist, the filter returns without doing anything and without error, unless it was set to create the path. If set to create, it creates a tree of maps along the path, and the leaf node gets the setValue called on it. Error on GVK mismatch, empty or poorly formed path. Filter expect kustomize style paths, not JSON paths. Filter stores internal state and should not be reused

Example

Code:

{
	in := &kio.ByteReader{
		Reader: bytes.NewBufferString(`
apiVersion: example.com/v1
kind: Foo
metadata:
  name: instance
---
apiVersion: example.com/v1
kind: Bar
metadata:
  name: instance
`),
	}
	fltr := Filter{
		CreateKind: yaml.ScalarNode,
		SetValue:   filtersutil.SetScalar("green"),
		FieldSpec:  types.FieldSpec{Path: "a/b", CreateIfNotPresent: true},
	}

	err := kio.Pipeline{
		Inputs:  []kio.Reader{in},
		Filters: []kio.Filter{kio.FilterAll(fltr)},
		Outputs: []kio.Writer{kio.ByteWriter{Writer: os.Stdout}},
	}.Execute()
	if err != nil {
		log.Fatal(err)
	}

	// Output:
	// apiVersion: example.com/v1
	// kind: Foo
	// metadata:
	//   name: instance
	// a:
	//   b: green
	// ---
	// apiVersion: example.com/v1
	// kind: Bar
	// metadata:
	//   name: instance
	// a:
	//   b: green
}

Output:

apiVersion: example.com/v1
kind: Foo
metadata:
  name: instance
a:
  b: green
---
apiVersion: example.com/v1
kind: Bar
metadata:
  name: instance
a:
  b: green

func (Filter) Filter

func (fltr Filter) Filter(obj *yaml.RNode) (*yaml.RNode, error)

Source Files

doc.go fieldspec.go

Version
v0.20.0 (latest)
Published
Jun 28, 2025
Platform
linux/amd64
Imports
8 packages
Last checked
2 days ago

Tools for package owners.