add support for varref in maps values

This commit is contained in:
Nestor
2019-02-11 09:37:41 +01:00
parent 7574f07be3
commit 8c93f7ba74
4 changed files with 163 additions and 10 deletions

View File

@@ -147,5 +147,7 @@ varReference:
- path: spec/template/spec/initContainers/volumeMounts/mountPath
kind: Deployment
- path: metadata/labels
`
)

View File

@@ -39,6 +39,17 @@ func (rv *refvarTransformer) replaceVars(in interface{}) (interface{}, error) {
xs = append(xs, expansion.Expand(a.(string), rv.mappingFunc))
}
return xs, nil
case map[string]interface{}:
inMap := in.(map[string]interface{})
xs := make(map[string]interface{}, len(inMap))
for k, v := range inMap {
s, ok := v.(string)
if !ok {
return nil, fmt.Errorf("%#v is expected to be %T", v, s)
}
xs[k] = expansion.Expand(s, rv.mappingFunc)
}
return xs, nil
case interface{}:
s, ok := in.(string)
if !ok {

View File

@@ -0,0 +1,91 @@
package transformers
import (
"reflect"
"testing"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers/config"
)
func TestVarRef(t *testing.T) {
type given struct {
varMap map[string]string
fs []config.FieldSpec
res resmap.ResMap
}
type expected struct {
res resmap.ResMap
}
testCases := []struct {
description string
given given
expected expected
}{
{
description: "var replacement in map[string]",
given: given{
varMap: map[string]string{
"FOO": "BAR",
},
fs: []config.FieldSpec{
{Gvk: cmap, Path: "data"},
},
res: resmap.ResMap{
resid.NewResId(cmap, "cm1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "cm1",
},
"data": map[string]interface{}{
"item1": "$(FOO)",
"item2": "bla",
},
}),
},
},
expected: expected{
res: resmap.ResMap{
resid.NewResId(cmap, "cm1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "cm1",
},
"data": map[string]interface{}{
"item1": "BAR",
"item2": "bla",
},
}),
},
},
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
// arrange
tr := NewRefVarTransformer(tc.given.varMap, tc.given.fs)
// act
err := tr.Transform(tc.given.res)
// assert
if err != nil {
t.Errorf("unexpected error: %v", err)
}
a, e := tc.given.res, tc.expected.res
if !reflect.DeepEqual(a, e) {
err = e.ErrorIfNotEqual(a)
t.Fatalf("actual doesn't match expected: \nACTUAL:\n%v\nEXPECTED:\n%v\nERR: %v", a, e, err)
}
})
}
}