diff options
author | Vincent Ambo <tazjin@gmail.com> | 2017-02-14T18·00+0100 |
---|---|---|
committer | Vincent Ambo <tazjin@gmail.com> | 2017-02-14T18·12+0100 |
commit | 0147c3e13e741f1d2bac7f082e509222fb86ab38 (patch) | |
tree | 2dccf2eb214b2c20ab4527e386e4459c7735022c /context | |
parent | f81fe551bcf1baa1167ed237c7120df69cf2ddab (diff) |
feat ctx: Let sub-resource-sets inherit vars from parent
Users of kontemplate may expect variables defined on the parent resource to be inherited by children. This implements that functionality. Values defined twice are overwritten by the child's definition. Fixes #20
Diffstat (limited to 'context')
-rw-r--r-- | context/context.go | 2 | ||||
-rw-r--r-- | context/context_test.go | 59 | ||||
-rw-r--r-- | context/testdata/parent-variable-override.yaml | 10 | ||||
-rw-r--r-- | context/testdata/parent-variables.yaml | 10 |
4 files changed, 81 insertions, 0 deletions
diff --git a/context/context.go b/context/context.go index 108bda28a90d..520070d88f67 100644 --- a/context/context.go +++ b/context/context.go @@ -9,6 +9,7 @@ import ( "github.com/ghodss/yaml" "github.com/polydawn/meep" + "github.com/tazjin/kontemplate/util" ) type ResourceSet struct { @@ -82,6 +83,7 @@ func flattenResourceSetCollections(rs *[]ResourceSet) *[]ResourceSet { for _, subResourceSet := range r.Include { subResourceSet.Parent = r.Name subResourceSet.Name = path.Join(r.Name, subResourceSet.Name) + subResourceSet.Values = *util.Merge(&r.Values, &subResourceSet.Values) flattened = append(flattened, subResourceSet) } } diff --git a/context/context_test.go b/context/context_test.go index b34222ed4650..66b857ffe822 100644 --- a/context/context_test.go +++ b/context/context_test.go @@ -81,3 +81,62 @@ func TestLoadContextWithResourceSetCollections(t *testing.T) { } } + +func TestSubresourceVariableInheritance(t *testing.T) { + ctx, err := LoadContextFromFile("testdata/parent-variables.yaml") + + if err != nil { + t.Error(err) + t.Fail() + } + + expected := Context{ + Name: "k8s.prod.mydomain.com", + ResourceSets: []ResourceSet{ + { + Name: "parent/child", + Values: map[string]interface{}{ + "foo": "bar", + "bar": "baz", + }, + Include: nil, + Parent: "parent", + }, + }, + BaseDir: "testdata", + } + + if !reflect.DeepEqual(*ctx, expected) { + t.Error("Loaded and expected context did not match") + t.Fail() + } +} + +func TestSubresourceVariableInheritanceOverride(t *testing.T) { + ctx, err := LoadContextFromFile("testdata/parent-variable-override.yaml") + + if err != nil { + t.Error(err) + t.Fail() + } + + expected := Context{ + Name: "k8s.prod.mydomain.com", + ResourceSets: []ResourceSet{ + { + Name: "parent/child", + Values: map[string]interface{}{ + "foo": "newvalue", + }, + Include: nil, + Parent: "parent", + }, + }, + BaseDir: "testdata", + } + + if !reflect.DeepEqual(*ctx, expected) { + t.Error("Loaded and expected context did not match") + t.Fail() + } +} diff --git a/context/testdata/parent-variable-override.yaml b/context/testdata/parent-variable-override.yaml new file mode 100644 index 000000000000..42676c3028fe --- /dev/null +++ b/context/testdata/parent-variable-override.yaml @@ -0,0 +1,10 @@ +--- +context: k8s.prod.mydomain.com +include: + - name: parent + values: + foo: bar + include: + - name: child + values: + foo: newvalue diff --git a/context/testdata/parent-variables.yaml b/context/testdata/parent-variables.yaml new file mode 100644 index 000000000000..8459fd30405b --- /dev/null +++ b/context/testdata/parent-variables.yaml @@ -0,0 +1,10 @@ +--- +context: k8s.prod.mydomain.com +include: + - name: parent + values: + foo: bar + include: + - name: child + values: + bar: baz |