diff --git a/internal/chart/v3/util/dependencies.go b/internal/chart/v3/util/dependencies.go index 9c4d8e80f..063e4fab1 100644 --- a/internal/chart/v3/util/dependencies.go +++ b/internal/chart/v3/util/dependencies.go @@ -214,6 +214,24 @@ Loop: // recursively call self to process sub dependencies for _, t := range cd { + // When a dependency uses an alias, cvals[path][alias] may be missing keys + // that were coalesced into cvals[originalName] by ancestor CoalesceValues + // calls (which used the original chart name, not the alias). To correct + // this, we backfill any missing keys from the top-level entry into the + // nested path entry. The nested entry is authoritative: if a key exists + // in both, the nested (alias-keyed) value wins. + if path != "" { + if pt, err := cvals.Table(strings.TrimSuffix(path, ".")); err == nil { + if top, ok := cvals[t.Metadata.Name].(map[string]interface{}); ok { + if v, ok := pt[t.Metadata.Name]; ok && !istable(v) { + slog.Warn("skipping nested path update: value is not a table", "path", path+t.Metadata.Name) + } else { + nested, _ := v.(map[string]interface{}) + pt[t.Metadata.Name] = util.CoalesceTables(nested, top) + } + } + } + } subpath := path + t.Metadata.Name + "." if err := processDependencyEnabled(t, cvals, subpath); err != nil { return err diff --git a/internal/chart/v3/util/dependencies_test.go b/internal/chart/v3/util/dependencies_test.go index c8a176725..ae3a93b9d 100644 --- a/internal/chart/v3/util/dependencies_test.go +++ b/internal/chart/v3/util/dependencies_test.go @@ -552,6 +552,73 @@ func validateDependencyTree(t *testing.T, c *chart.Chart) { } } +func TestDependencyEnabledAliasNestedCondition(t *testing.T) { + // Chart structure: + // parentchart -> mid (alias: midchart) -> leaf (alias: leafchart) -> util (condition: util.enabled) + // + // leaf/values.yaml sets util.enabled: false. + // No user-provided values override this. + // Expected: util is NOT included in the output. + c := loadChart(t, "testdata/alias-condition-nested") + if err := processDependencyEnabled(c, c.Values, ""); err != nil { + t.Fatalf("error processing enabled dependencies: %v", err) + } + + names := extractChartNames(c) + expected := []string{"parentchart", "parentchart.midchart", "parentchart.midchart.leafchart"} + sort.Strings(expected) + + if len(names) != len(expected) { + t.Fatalf("slice lengths do not match: got %v, expected %v", names, expected) + } + for i := range names { + if names[i] != expected[i] { + t.Fatalf("slice values do not match: got %v, expected %v", names, expected) + } + } +} + +// TestDependencyEnabledAliasNestedConditionEnabled tests that overriding util.enabled=true +// through the parent chart's values correctly includes the dependency even when the leaf +// chart's default disables it. This is the complementary positive case to +// TestDependencyEnabledAliasNestedCondition. +func TestDependencyEnabledAliasNestedConditionEnabled(t *testing.T) { + // Chart structure: + // parentchart -> mid (alias: midchart) -> leaf (alias: leafchart) -> util (condition: util.enabled) + // + // leaf/values.yaml sets util.enabled: false by default. + // User-provided values override util.enabled=true via the full alias path. + // Expected: util IS included in the output. + c := loadChart(t, "testdata/alias-condition-nested") + vals := map[string]interface{}{ + "midchart": map[string]interface{}{ + "enabled": true, + "leafchart": map[string]interface{}{ + "enabled": true, + "util": map[string]interface{}{ + "enabled": true, + }, + }, + }, + } + if err := processDependencyEnabled(c, vals, ""); err != nil { + t.Fatalf("error processing enabled dependencies: %v", err) + } + + names := extractChartNames(c) + expected := []string{"parentchart", "parentchart.midchart", "parentchart.midchart.leafchart", "parentchart.midchart.leafchart.util"} + sort.Strings(expected) + + if len(names) != len(expected) { + t.Fatalf("slice lengths do not match: got %v, expected %v", names, expected) + } + for i := range names { + if names[i] != expected[i] { + t.Fatalf("slice values do not match: got %v, expected %v", names, expected) + } + } +} + func TestChartWithDependencyAliasedTwiceAndDoublyReferencedSubDependency(t *testing.T) { c := loadChart(t, "testdata/chart-with-dependency-aliased-twice") diff --git a/internal/chart/v3/util/testdata/alias-condition-nested/Chart.yaml b/internal/chart/v3/util/testdata/alias-condition-nested/Chart.yaml new file mode 100644 index 000000000..8cab888a3 --- /dev/null +++ b/internal/chart/v3/util/testdata/alias-condition-nested/Chart.yaml @@ -0,0 +1,9 @@ +apiVersion: v3 +name: parentchart +version: 0.1.0 +dependencies: + - name: mid + repository: http://localhost:10191 + version: 0.1.0 + alias: midchart + condition: midchart.enabled diff --git a/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/Chart.yaml b/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/Chart.yaml new file mode 100644 index 000000000..9009efc75 --- /dev/null +++ b/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/Chart.yaml @@ -0,0 +1,9 @@ +apiVersion: v3 +name: mid +version: 0.1.0 +dependencies: + - name: leaf + repository: http://localhost:10191 + version: 0.1.0 + alias: leafchart + condition: leafchart.enabled diff --git a/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/charts/leaf/Chart.yaml b/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/charts/leaf/Chart.yaml new file mode 100644 index 000000000..8477bef5f --- /dev/null +++ b/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/charts/leaf/Chart.yaml @@ -0,0 +1,8 @@ +apiVersion: v3 +name: leaf +version: 0.1.0 +dependencies: + - name: util + repository: http://localhost:10191 + version: 0.1.0 + condition: util.enabled diff --git a/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/charts/leaf/charts/util/Chart.yaml b/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/charts/leaf/charts/util/Chart.yaml new file mode 100644 index 000000000..b9ddc5115 --- /dev/null +++ b/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/charts/leaf/charts/util/Chart.yaml @@ -0,0 +1,3 @@ +apiVersion: v3 +name: util +version: 0.1.0 diff --git a/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/charts/leaf/charts/util/values.yaml b/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/charts/leaf/charts/util/values.yaml new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/charts/leaf/charts/util/values.yaml @@ -0,0 +1 @@ +{} diff --git a/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/charts/leaf/values.yaml b/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/charts/leaf/values.yaml new file mode 100644 index 000000000..54d7c9524 --- /dev/null +++ b/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/charts/leaf/values.yaml @@ -0,0 +1,3 @@ +# util is disabled by default in this chart's values +util: + enabled: false diff --git a/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/values.yaml b/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/values.yaml new file mode 100644 index 000000000..de12ce852 --- /dev/null +++ b/internal/chart/v3/util/testdata/alias-condition-nested/charts/mid/values.yaml @@ -0,0 +1,2 @@ +leafchart: + enabled: true diff --git a/internal/chart/v3/util/testdata/alias-condition-nested/values.yaml b/internal/chart/v3/util/testdata/alias-condition-nested/values.yaml new file mode 100644 index 000000000..4ef046569 --- /dev/null +++ b/internal/chart/v3/util/testdata/alias-condition-nested/values.yaml @@ -0,0 +1,2 @@ +midchart: + enabled: true diff --git a/pkg/chart/v2/util/dependencies.go b/pkg/chart/v2/util/dependencies.go index abd673f9d..5b7b110e1 100644 --- a/pkg/chart/v2/util/dependencies.go +++ b/pkg/chart/v2/util/dependencies.go @@ -214,6 +214,24 @@ Loop: // recursively call self to process sub dependencies for _, t := range cd { + // When a dependency uses an alias, cvals[path][alias] may be missing keys + // that were coalesced into cvals[originalName] by ancestor CoalesceValues + // calls (which used the original chart name, not the alias). To correct + // this, we backfill any missing keys from the top-level entry into the + // nested path entry. The nested entry is authoritative: if a key exists + // in both, the nested (alias-keyed) value wins. + if path != "" { + if pt, err := cvals.Table(strings.TrimSuffix(path, ".")); err == nil { + if top, ok := cvals[t.Metadata.Name].(map[string]interface{}); ok { + if v, ok := pt[t.Metadata.Name]; ok && !istable(v) { + slog.Warn("skipping nested path update: value is not a table", "path", path+t.Metadata.Name) + } else { + nested, _ := v.(map[string]interface{}) + pt[t.Metadata.Name] = util.CoalesceTables(nested, top) + } + } + } + } subpath := path + t.Metadata.Name + "." if err := processDependencyEnabled(t, cvals, subpath); err != nil { return err diff --git a/pkg/chart/v2/util/dependencies_test.go b/pkg/chart/v2/util/dependencies_test.go index 0e4df8528..f73981ed3 100644 --- a/pkg/chart/v2/util/dependencies_test.go +++ b/pkg/chart/v2/util/dependencies_test.go @@ -134,6 +134,76 @@ func TestDependencyEnabled(t *testing.T) { } } +// TestDependencyEnabledAliasNestedCondition tests that a condition defaulting to false in a +// leaf chart's values.yaml is respected when the leaf chart is accessed through multiple +// levels of aliased dependencies. +func TestDependencyEnabledAliasNestedCondition(t *testing.T) { + // Chart structure: + // parentchart -> mid (alias: midchart) -> leaf (alias: leafchart) -> util (condition: util.enabled) + // + // leaf/values.yaml sets util.enabled: false. + // No user-provided values override this. + // Expected: util is NOT included in the output. + c := loadChart(t, "testdata/alias-condition-nested") + if err := processDependencyEnabled(c, c.Values, ""); err != nil { + t.Fatalf("error processing enabled dependencies: %v", err) + } + + names := extractChartNames(c) + expected := []string{"parentchart", "parentchart.midchart", "parentchart.midchart.leafchart"} + sort.Strings(expected) + + if len(names) != len(expected) { + t.Fatalf("slice lengths do not match: got %v, expected %v", names, expected) + } + for i := range names { + if names[i] != expected[i] { + t.Fatalf("slice values do not match: got %v, expected %v", names, expected) + } + } +} + +// TestDependencyEnabledAliasNestedConditionEnabled tests that overriding util.enabled=true +// through the parent chart's values correctly includes the dependency even when the leaf +// chart's default disables it. This is the complementary positive case to +// TestDependencyEnabledAliasNestedCondition. +func TestDependencyEnabledAliasNestedConditionEnabled(t *testing.T) { + // Chart structure: + // parentchart -> mid (alias: midchart) -> leaf (alias: leafchart) -> util (condition: util.enabled) + // + // leaf/values.yaml sets util.enabled: false by default. + // User-provided values override util.enabled=true via the full alias path. + // Expected: util IS included in the output. + c := loadChart(t, "testdata/alias-condition-nested") + vals := map[string]interface{}{ + "midchart": map[string]interface{}{ + "enabled": true, + "leafchart": map[string]interface{}{ + "enabled": true, + "util": map[string]interface{}{ + "enabled": true, + }, + }, + }, + } + if err := processDependencyEnabled(c, vals, ""); err != nil { + t.Fatalf("error processing enabled dependencies: %v", err) + } + + names := extractChartNames(c) + expected := []string{"parentchart", "parentchart.midchart", "parentchart.midchart.leafchart", "parentchart.midchart.leafchart.util"} + sort.Strings(expected) + + if len(names) != len(expected) { + t.Fatalf("slice lengths do not match: got %v, expected %v", names, expected) + } + for i := range names { + if names[i] != expected[i] { + t.Fatalf("slice values do not match: got %v, expected %v", names, expected) + } + } +} + // extractChartNames recursively searches chart dependencies returning all charts found func extractChartNames(c *chart.Chart) []string { var out []string diff --git a/pkg/chart/v2/util/testdata/alias-condition-nested/Chart.yaml b/pkg/chart/v2/util/testdata/alias-condition-nested/Chart.yaml new file mode 100644 index 000000000..fc50e2468 --- /dev/null +++ b/pkg/chart/v2/util/testdata/alias-condition-nested/Chart.yaml @@ -0,0 +1,9 @@ +apiVersion: v2 +name: parentchart +version: 0.1.0 +dependencies: + - name: mid + repository: http://localhost:10191 + version: 0.1.0 + alias: midchart + condition: midchart.enabled diff --git a/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/Chart.yaml b/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/Chart.yaml new file mode 100644 index 000000000..9348b5fd2 --- /dev/null +++ b/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/Chart.yaml @@ -0,0 +1,9 @@ +apiVersion: v2 +name: mid +version: 0.1.0 +dependencies: + - name: leaf + repository: http://localhost:10191 + version: 0.1.0 + alias: leafchart + condition: leafchart.enabled diff --git a/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/charts/leaf/Chart.yaml b/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/charts/leaf/Chart.yaml new file mode 100644 index 000000000..28f1f26fe --- /dev/null +++ b/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/charts/leaf/Chart.yaml @@ -0,0 +1,8 @@ +apiVersion: v2 +name: leaf +version: 0.1.0 +dependencies: + - name: util + repository: http://localhost:10191 + version: 0.1.0 + condition: util.enabled diff --git a/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/charts/leaf/charts/util/Chart.yaml b/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/charts/leaf/charts/util/Chart.yaml new file mode 100644 index 000000000..a57a4988d --- /dev/null +++ b/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/charts/leaf/charts/util/Chart.yaml @@ -0,0 +1,3 @@ +apiVersion: v2 +name: util +version: 0.1.0 diff --git a/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/charts/leaf/charts/util/values.yaml b/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/charts/leaf/charts/util/values.yaml new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/charts/leaf/charts/util/values.yaml @@ -0,0 +1 @@ +{} diff --git a/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/charts/leaf/values.yaml b/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/charts/leaf/values.yaml new file mode 100644 index 000000000..54d7c9524 --- /dev/null +++ b/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/charts/leaf/values.yaml @@ -0,0 +1,3 @@ +# util is disabled by default in this chart's values +util: + enabled: false diff --git a/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/values.yaml b/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/values.yaml new file mode 100644 index 000000000..de12ce852 --- /dev/null +++ b/pkg/chart/v2/util/testdata/alias-condition-nested/charts/mid/values.yaml @@ -0,0 +1,2 @@ +leafchart: + enabled: true diff --git a/pkg/chart/v2/util/testdata/alias-condition-nested/values.yaml b/pkg/chart/v2/util/testdata/alias-condition-nested/values.yaml new file mode 100644 index 000000000..4ef046569 --- /dev/null +++ b/pkg/chart/v2/util/testdata/alias-condition-nested/values.yaml @@ -0,0 +1,2 @@ +midchart: + enabled: true