diff --git a/pkg/chart/common/util/coalesce.go b/pkg/chart/common/util/coalesce.go index 5bfa1c608..481f90f76 100644 --- a/pkg/chart/common/util/coalesce.go +++ b/pkg/chart/common/util/coalesce.go @@ -303,6 +303,9 @@ func coalesceTablesFullKey(printf printFn, dst, src map[string]interface{}, pref if dst == nil { return src } + if len(dst) == 0 && prefix != "" && !merge { + return dst + } for key, val := range dst { if val == nil { src[key] = nil diff --git a/pkg/chart/common/util/coalesce_test.go b/pkg/chart/common/util/coalesce_test.go index 871bfa8da..5109167cc 100644 --- a/pkg/chart/common/util/coalesce_test.go +++ b/pkg/chart/common/util/coalesce_test.go @@ -241,6 +241,41 @@ func TestCoalesceValues(t *testing.T) { 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) { var b bytes.Buffer tt := template.Must(template.New("t").Parse(tpl))