Set common labels and annotations as strings

This commit is contained in:
Phillip Wittrock
2020-05-13 16:52:44 -07:00
parent 0a8d367633
commit 0866e2a54b
5 changed files with 73 additions and 4 deletions

View File

@@ -30,7 +30,7 @@ func (f Filter) Filter(nodes []*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]),
SetValue: fsslice.SetEntry(k, f.Annotations[k], yaml.StringTag),
CreateKind: yaml.MappingNode, // Annotations are MappingNodes.
}); err != nil {
return nil, err

View File

@@ -123,6 +123,33 @@ a:
},
},
},
"number": {
input: `
apiVersion: example.com/v1
kind: Foo
metadata:
name: instance
annotations:
hero: batman
fiend: riddler
`,
expectedOutput: `
apiVersion: example.com/v1
kind: Foo
metadata:
name: instance
annotations:
hero: batman
fiend: riddler
2: ford
clown: "1"
`,
filter: Filter{Annotations: annoMap{
"clown": "1",
"2": "ford",
}},
},
}
for tn, tc := range testCases {

View File

@@ -19,11 +19,18 @@ func SetScalar(value string) SetFn {
}
// SetEntry returns a SetFn to set an entry in a map
func SetEntry(key, value string) SetFn {
func SetEntry(key, value, tag string) SetFn {
return func(node *yaml.RNode) error {
return node.PipeE(yaml.FieldSetter{
Name: key, StringValue: value})
Name: key,
Value: yaml.NewRNode(&yaml.Node{
Kind: yaml.ScalarNode,
Value: value,
Tag: tag,
}),
})
}
}
var _ yaml.Filter = Filter{}

View File

@@ -31,7 +31,7 @@ func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
for _, k := range keys {
if err := node.PipeE(fsslice.Filter{
FsSlice: f.FsSlice,
SetValue: fsslice.SetEntry(k, f.Labels[k]),
SetValue: fsslice.SetEntry(k, f.Labels[k], yaml.StringTag),
CreateKind: yaml.MappingNode, // Labels are MappingNodes.
}); err != nil {
return nil, err

View File

@@ -291,6 +291,41 @@ metadata:
},
},
},
"number": {
input: `
apiVersion: example.com/v1
kind: Foo
metadata:
name: instance
labels:
hero: batman
fiend: riddler
`,
expectedOutput: `
apiVersion: example.com/v1
kind: Foo
metadata:
name: instance
labels:
hero: batman
fiend: riddler
1: emmett kelley
auto: "2"
`,
filter: Filter{
Labels: labelMap{
"1": "emmett kelley",
"auto": "2",
},
FsSlice: []types.FieldSpec{
{
Path: "metadata/labels",
CreateIfNotPresent: true,
},
},
},
},
}
for tn, tc := range testCases {