kosiew 15 hours ago committed by GitHub
commit 699ebdc8ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -303,6 +303,14 @@ func coalesceTablesFullKey(printf printFn, dst, src map[string]interface{}, pref
if dst == nil { if dst == nil {
return src return src
} }
// If dst is empty and we're operating on a sub-key (prefix != ""),
// bail out when not merging so that child charts don't inherit defaults
// for an explicitly empty table in the parent. We only do this for
// non-top-level coalescing (prefix != "") because top-level coalescing
// still needs to pull chart defaults across when dst is empty.
if len(dst) == 0 && prefix != "" && !merge {
return dst
}
for key, val := range dst { for key, val := range dst {
if val == nil { if val == nil {
src[key] = nil src[key] = nil

@ -241,6 +241,41 @@ func TestCoalesceValues(t *testing.T) {
is.Equal(valsCopy, vals) is.Equal(valsCopy, vals)
} }
func TestCoalesceValuesEmptyMapOverride(t *testing.T) {
c := &chart.Chart{
Metadata: &chart.Metadata{Name: "emptymap"},
Values: map[string]interface{}{
"config": map[string]interface{}{"foo": "bar"},
"toDelete": map[string]interface{}{"baz": "qux"},
},
}
overrides := map[string]interface{}{
"config": map[string]interface{}{},
"toDelete": nil,
}
result, err := CoalesceValues(c, overrides)
if err != nil {
t.Fatal(err)
}
config, ok := result["config"].(map[string]interface{})
if !ok {
t.Fatalf("expected config to remain a map, got %T", result["config"])
}
if len(config) != 0 {
t.Fatalf("expected config map to be empty, got %v", config)
}
if _, exists := config["foo"]; exists {
t.Fatalf("expected config override to drop default key, still found foo in %v", config)
}
if _, exists := result["toDelete"]; exists {
t.Fatalf("expected toDelete key to be removed when set to nil override")
}
}
func ttpl(tpl string, v map[string]interface{}) (string, error) { func ttpl(tpl string, v map[string]interface{}) (string, error) {
var b bytes.Buffer var b bytes.Buffer
tt := template.Must(template.New("t").Parse(tpl)) tt := template.Must(template.New("t").Parse(tpl))
@ -401,6 +436,36 @@ func TestMergeValues(t *testing.T) {
} }
func TestCoalesceTables(t *testing.T) { func TestCoalesceTables(t *testing.T) {
t.Run("empty destination table overrides defaults", func(t *testing.T) {
t.Helper()
dst := map[string]interface{}{
"config": map[string]interface{}{},
}
src := map[string]interface{}{
"config": map[string]interface{}{
"enabled": true,
"port": 8080,
},
}
CoalesceTables(dst, src)
config, ok := dst["config"].(map[string]interface{})
if !ok {
t.Fatalf("config should remain a map, got %T", dst["config"])
}
if len(config) != 0 {
t.Fatalf("expected empty config map, got %v", config)
}
if _, exists := config["enabled"]; exists {
t.Fatal("expected default \"enabled\" key to be absent")
}
if _, exists := config["port"]; exists {
t.Fatal("expected default \"port\" key to be absent")
}
})
dst := map[string]interface{}{ dst := map[string]interface{}{
"name": "Ishmael", "name": "Ishmael",
"address": map[string]interface{}{ "address": map[string]interface{}{

Loading…
Cancel
Save