fieldspec

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2022 License: Apache-2.0 Imports: 8 Imported by: 7

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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
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

func (Filter) Filter

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL