mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
Function to set labels.
This commit is contained in:
@@ -4,15 +4,18 @@
|
||||
package annotations
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/filters/fsslice"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
type annoMap map[string]string
|
||||
|
||||
type Filter struct {
|
||||
// Annotations is the set of annotations to apply to the inputs
|
||||
Annotations map[string]string `yaml:"annotations,omitempty"`
|
||||
Annotations annoMap `yaml:"annotations,omitempty"`
|
||||
|
||||
// FsSlice contains the FieldSpecs to locate the namespace field
|
||||
FsSlice types.FsSlice
|
||||
@@ -21,24 +24,19 @@ type Filter struct {
|
||||
var _ kio.Filter = Filter{}
|
||||
|
||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
for i := range nodes {
|
||||
if err := f.run(nodes[i]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
// run applies the filter to a single node.
|
||||
func (f Filter) run(node *yaml.RNode) error {
|
||||
for key, value := range f.Annotations {
|
||||
if err := node.PipeE(fsslice.Filter{
|
||||
FsSlice: f.FsSlice,
|
||||
SetValue: fsslice.SetEntry(key, value),
|
||||
CreateKind: yaml.MappingNode, // Annotations are MappingNodes.
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
keys := filtersutil.SortedMapKeys(f.Annotations)
|
||||
_, err := kio.FilterAll(yaml.FilterFunc(
|
||||
func(node *yaml.RNode) (*yaml.RNode, error) {
|
||||
for _, k := range keys {
|
||||
if err := node.PipeE(fsslice.Filter{
|
||||
FsSlice: f.FsSlice,
|
||||
SetValue: fsslice.SetEntry(k, f.Annotations[k]),
|
||||
CreateKind: yaml.MappingNode, // Annotations are MappingNodes.
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return node, nil
|
||||
})).Filter(nodes)
|
||||
return nodes, err
|
||||
}
|
||||
|
||||
@@ -4,17 +4,17 @@
|
||||
package annotations
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
||||
filtertest_test "sigs.k8s.io/kustomize/api/testutils/filtertest"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
var annosFs = builtinconfig.MakeDefaultConfig().CommonAnnotations
|
||||
|
||||
func TestAnnotations_Filter(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
input string
|
||||
@@ -28,11 +28,9 @@ apiVersion: example.com/v1
|
||||
kind: Foo
|
||||
metadata:
|
||||
name: instance
|
||||
---
|
||||
apiVersion: example.com/v1
|
||||
kind: Bar
|
||||
metadata:
|
||||
name: instance
|
||||
annotations:
|
||||
hero: batman
|
||||
fiend: riddler
|
||||
`,
|
||||
expectedOutput: `
|
||||
apiVersion: example.com/v1
|
||||
@@ -40,17 +38,18 @@ kind: Foo
|
||||
metadata:
|
||||
name: instance
|
||||
annotations:
|
||||
sleater: kinney
|
||||
---
|
||||
apiVersion: example.com/v1
|
||||
kind: Bar
|
||||
metadata:
|
||||
name: instance
|
||||
annotations:
|
||||
sleater: kinney
|
||||
hero: batman
|
||||
fiend: riddler
|
||||
auto: ford
|
||||
bean: cannellini
|
||||
clown: emmett kelley
|
||||
dragon: smaug
|
||||
`,
|
||||
filter: Filter{Annotations: map[string]string{
|
||||
"sleater": "kinney",
|
||||
filter: Filter{Annotations: annoMap{
|
||||
"clown": "emmett kelley",
|
||||
"auto": "ford",
|
||||
"dragon": "smaug",
|
||||
"bean": "cannellini",
|
||||
}},
|
||||
},
|
||||
"update": {
|
||||
@@ -60,7 +59,8 @@ kind: Foo
|
||||
metadata:
|
||||
name: instance
|
||||
annotations:
|
||||
foo: foo
|
||||
hero: batman
|
||||
fiend: riddler
|
||||
`,
|
||||
expectedOutput: `
|
||||
apiVersion: example.com/v1
|
||||
@@ -68,10 +68,16 @@ kind: Foo
|
||||
metadata:
|
||||
name: instance
|
||||
annotations:
|
||||
foo: bar
|
||||
hero: superman
|
||||
fiend: luthor
|
||||
bean: cannellini
|
||||
clown: emmett kelley
|
||||
`,
|
||||
filter: Filter{Annotations: map[string]string{
|
||||
"foo": "bar",
|
||||
filter: Filter{Annotations: annoMap{
|
||||
"clown": "emmett kelley",
|
||||
"hero": "superman",
|
||||
"fiend": "luthor",
|
||||
"bean": "cannellini",
|
||||
}},
|
||||
},
|
||||
"data-fieldspecs": {
|
||||
@@ -107,7 +113,7 @@ a:
|
||||
b:
|
||||
sleater: kinney
|
||||
`,
|
||||
filter: Filter{Annotations: map[string]string{
|
||||
filter: Filter{Annotations: annoMap{
|
||||
"sleater": "kinney",
|
||||
}},
|
||||
fsslice: []types.FieldSpec{
|
||||
@@ -121,126 +127,13 @@ a:
|
||||
|
||||
for tn, tc := range testCases {
|
||||
t.Run(tn, func(t *testing.T) {
|
||||
config := builtinconfig.MakeDefaultConfig()
|
||||
|
||||
filter := tc.filter
|
||||
filter.FsSlice = append(config.CommonAnnotations, tc.fsslice...)
|
||||
|
||||
var out bytes.Buffer
|
||||
rw := kio.ByteReadWriter{
|
||||
Reader: bytes.NewBufferString(tc.input),
|
||||
Writer: &out,
|
||||
}
|
||||
|
||||
err := kio.Pipeline{
|
||||
Inputs: []kio.Reader{&rw},
|
||||
Filters: []kio.Filter{filter},
|
||||
Outputs: []kio.Writer{&rw},
|
||||
}.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
filter.FsSlice = append(annosFs, tc.fsslice...)
|
||||
if !assert.Equal(t,
|
||||
strings.TrimSpace(tc.expectedOutput),
|
||||
strings.TrimSpace(out.String())) {
|
||||
strings.TrimSpace(filtertest_test.RunFilter(t, tc.input, filter))) {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnnotations_Filter_Multiple(t *testing.T) {
|
||||
input := `
|
||||
apiVersion: example.com/v1
|
||||
kind: Foo
|
||||
metadata:
|
||||
name: instance
|
||||
---
|
||||
apiVersion: example.com/v1
|
||||
kind: Bar
|
||||
metadata:
|
||||
name: instance
|
||||
`
|
||||
annos := map[string]string{
|
||||
"sleater": "kinney",
|
||||
"sonic": "youth",
|
||||
}
|
||||
config := builtinconfig.MakeDefaultConfig()
|
||||
filter := Filter{Annotations: annos}
|
||||
filter.FsSlice = config.CommonAnnotations
|
||||
|
||||
var out bytes.Buffer
|
||||
rw := kio.ByteReadWriter{
|
||||
Reader: bytes.NewBufferString(input),
|
||||
Writer: &out,
|
||||
}
|
||||
|
||||
err := kio.Pipeline{
|
||||
Inputs: []kio.Reader{&rw},
|
||||
Filters: []kio.Filter{filter},
|
||||
Outputs: []kio.Writer{&rw},
|
||||
}.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
assertHasAnnotation(t, out.String(), annos)
|
||||
}
|
||||
|
||||
func assertHasAnnotation(t *testing.T, y string, exp map[string]string) bool {
|
||||
var out bytes.Buffer
|
||||
rw := kio.ByteReadWriter{
|
||||
Reader: bytes.NewBufferString(y),
|
||||
Writer: &out,
|
||||
}
|
||||
filter := &captureAnnotationFilter{
|
||||
annotations: make(map[string]annotations),
|
||||
}
|
||||
err := kio.Pipeline{
|
||||
Inputs: []kio.Reader{&rw},
|
||||
Filters: []kio.Filter{filter},
|
||||
Outputs: []kio.Writer{&rw},
|
||||
}.Execute()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return false
|
||||
}
|
||||
|
||||
for name, annos := range filter.annotations {
|
||||
for key, val := range exp {
|
||||
v, found := annos[key]
|
||||
if !found {
|
||||
t.Errorf("expected annotation with key %s in object %s, but didn't find it",
|
||||
key, name)
|
||||
return false
|
||||
}
|
||||
if want, got := val, v; got != want {
|
||||
t.Errorf("exected annotation %s in object %s to have value %s, but found %s",
|
||||
key, name, want, got)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type annotations map[string]string
|
||||
|
||||
type captureAnnotationFilter struct {
|
||||
annotations map[string]annotations
|
||||
}
|
||||
|
||||
func (c captureAnnotationFilter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode,
|
||||
error) {
|
||||
for _, n := range nodes {
|
||||
meta, err := n.GetMeta()
|
||||
if err != nil {
|
||||
return nodes, err
|
||||
}
|
||||
name := meta.Name
|
||||
annos := meta.Annotations
|
||||
c.annotations[name] = annos
|
||||
}
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user