Merge pull request #12879 from ryanhockstad/subchart-null

bugfix: Override subcharts with null values
pull/13283/head
Scott Rigby 8 months ago committed by GitHub
commit d53fb5058e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -237,6 +237,9 @@ func coalesceValues(printf printFn, c *chart.Chart, v map[string]interface{}, pr
printf("warning: skipped value for %s.%s: Not a table.", subPrefix, key) printf("warning: skipped value for %s.%s: Not a table.", subPrefix, key)
} }
} else { } else {
// If the key is a child chart, coalesce tables with Merge set to true
merge := childChartMergeTrue(c, key, merge)
// Because v has higher precedence than nv, dest values override src // Because v has higher precedence than nv, dest values override src
// values. // values.
coalesceTablesFullKey(printf, dest, src, concatPrefix(subPrefix, key), merge) coalesceTablesFullKey(printf, dest, src, concatPrefix(subPrefix, key), merge)
@ -249,6 +252,15 @@ func coalesceValues(printf printFn, c *chart.Chart, v map[string]interface{}, pr
} }
} }
func childChartMergeTrue(chrt *chart.Chart, key string, merge bool) bool {
for _, subchart := range chrt.Dependencies() {
if subchart.Name() == key {
return true
}
}
return merge
}
// CoalesceTables merges a source map into a destination map. // CoalesceTables merges a source map into a destination map.
// //
// dest is considered authoritative. // dest is considered authoritative.

@ -44,18 +44,21 @@ global:
boat: true boat: true
pequod: pequod:
boat: null
global: global:
name: Stinky name: Stinky
harpooner: Tashtego harpooner: Tashtego
nested: nested:
boat: false boat: false
sail: true sail: true
foo2: null
ahab: ahab:
scope: whale scope: whale
boat: null boat: null
nested: nested:
foo: true foo: true
bar: null boat: null
object: null
`) `)
func withDeps(c *chart.Chart, deps ...*chart.Chart) *chart.Chart { func withDeps(c *chart.Chart, deps ...*chart.Chart) *chart.Chart {
@ -82,6 +85,13 @@ func TestCoalesceValues(t *testing.T) {
"global": map[string]interface{}{ "global": map[string]interface{}{
"nested2": map[string]interface{}{"l0": "moby"}, "nested2": map[string]interface{}{"l0": "moby"},
}, },
"pequod": map[string]interface{}{
"boat": "maybe",
"ahab": map[string]interface{}{
"boat": "maybe",
"nested": map[string]interface{}{"boat": "maybe"},
},
},
}, },
}, },
withDeps(&chart.Chart{ withDeps(&chart.Chart{
@ -92,19 +102,25 @@ func TestCoalesceValues(t *testing.T) {
"global": map[string]interface{}{ "global": map[string]interface{}{
"nested2": map[string]interface{}{"l1": "pequod"}, "nested2": map[string]interface{}{"l1": "pequod"},
}, },
"boat": false,
"ahab": map[string]interface{}{
"boat": false,
"nested": map[string]interface{}{"boat": false},
},
}, },
}, },
&chart.Chart{ &chart.Chart{
Metadata: &chart.Metadata{Name: "ahab"}, Metadata: &chart.Metadata{Name: "ahab"},
Values: map[string]interface{}{ Values: map[string]interface{}{
"global": map[string]interface{}{ "global": map[string]interface{}{
"nested": map[string]interface{}{"foo": "bar"}, "nested": map[string]interface{}{"foo": "bar", "foo2": "bar2"},
"nested2": map[string]interface{}{"l2": "ahab"}, "nested2": map[string]interface{}{"l2": "ahab"},
}, },
"scope": "ahab", "scope": "ahab",
"name": "ahab", "name": "ahab",
"boat": true, "boat": true,
"nested": map[string]interface{}{"foo": false, "bar": true}, "nested": map[string]interface{}{"foo": false, "boat": true},
"object": map[string]interface{}{"foo": "bar"},
}, },
}, },
), ),
@ -155,6 +171,7 @@ func TestCoalesceValues(t *testing.T) {
{"{{.pequod.ahab.nested.foo}}", "true"}, {"{{.pequod.ahab.nested.foo}}", "true"},
{"{{.pequod.ahab.global.name}}", "Ishmael"}, {"{{.pequod.ahab.global.name}}", "Ishmael"},
{"{{.pequod.ahab.global.nested.foo}}", "bar"}, {"{{.pequod.ahab.global.nested.foo}}", "bar"},
{"{{.pequod.ahab.global.nested.foo2}}", "<no value>"},
{"{{.pequod.ahab.global.subject}}", "Queequeg"}, {"{{.pequod.ahab.global.subject}}", "Queequeg"},
{"{{.pequod.ahab.global.harpooner}}", "Tashtego"}, {"{{.pequod.ahab.global.harpooner}}", "Tashtego"},
{"{{.pequod.global.name}}", "Ishmael"}, {"{{.pequod.global.name}}", "Ishmael"},
@ -200,13 +217,22 @@ func TestCoalesceValues(t *testing.T) {
t.Error("Expected nested boat key to be removed, still present") t.Error("Expected nested boat key to be removed, still present")
} }
subchart := v["pequod"].(map[string]interface{})["ahab"].(map[string]interface{}) subchart := v["pequod"].(map[string]interface{})
if _, ok := subchart["boat"]; ok { if _, ok := subchart["boat"]; ok {
t.Error("Expected subchart boat key to be removed, still present") t.Error("Expected subchart boat key to be removed, still present")
} }
if _, ok := subchart["nested"].(map[string]interface{})["bar"]; ok { subsubchart := subchart["ahab"].(map[string]interface{})
t.Error("Expected subchart nested bar key to be removed, still present") if _, ok := subsubchart["boat"]; ok {
t.Error("Expected sub-subchart ahab boat key to be removed, still present")
}
if _, ok := subsubchart["nested"].(map[string]interface{})["boat"]; ok {
t.Error("Expected sub-subchart nested boat key to be removed, still present")
}
if _, ok := subsubchart["object"]; ok {
t.Error("Expected sub-subchart object map to be removed, still present")
} }
// CoalesceValues should not mutate the passed arguments // CoalesceValues should not mutate the passed arguments

Loading…
Cancel
Save