Function to set labels.

This commit is contained in:
Jeffrey Regan
2020-03-26 17:19:30 -07:00
committed by jregan
parent 85e9127071
commit 2be48ca96a
11 changed files with 371 additions and 189 deletions

View File

@@ -5,11 +5,25 @@ package filtersutil
import (
"encoding/json"
"sort"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
// SortedMapKeys returns a sorted slice of keys to the given map.
// Writing this function never gets old.
func SortedMapKeys(m map[string]string) []string {
keys := make([]string, len(m))
i := 0
for k := range m {
keys[i] = k
i++
}
sort.Strings(keys)
return keys
}
// ApplyToJSON applies the filter to the json objects.
func ApplyToJSON(filter kio.Filter, objs ...marshalerUnmarshaler) error {
var nodes []*yaml.RNode

View File

@@ -11,6 +11,32 @@ import (
"sigs.k8s.io/kustomize/kyaml/yaml"
)
func TestSortedKeys(t *testing.T) {
testCases := map[string]struct {
input map[string]string
expected []string
}{
"empty": {
input: map[string]string{},
expected: []string{}},
"one": {
input: map[string]string{"a": "aaa"},
expected: []string{"a"}},
"three": {
input: map[string]string{"c": "ccc", "b": "bbb", "a": "aaa"},
expected: []string{"a", "b", "c"}},
}
for tn, tc := range testCases {
t.Run(tn, func(t *testing.T) {
if !assert.Equal(t,
filtersutil.SortedMapKeys(tc.input),
tc.expected) {
t.FailNow()
}
})
}
}
func TestApplyToJSON(t *testing.T) {
instance1 := bytes.NewBufferString(`{"kind": "Foo"}`)
instance2 := bytes.NewBufferString(`{"kind": "Bar"}`)