Skip to content

Commit d7eb6e8

Browse files
committed
fix: ensure nixChart supports both values as a list of maps and map
1 parent c17f53f commit d7eb6e8

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

pkgs/nixchart/nixchart.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ func CleanupCharts(cleanup []string) {
6363
}
6464
}
6565

66+
func prepareChartValues(chart map[string]any) map[string]any {
67+
var v map[string]any
68+
vl, ok := chart["values"].([]map[string]any)
69+
if ok {
70+
mergedValues := map[string]any{}
71+
for _, m := range vl {
72+
mergedValues = utils.MergeMaps(mergedValues, m)
73+
}
74+
v = mergedValues
75+
} else {
76+
v, ok = chart["values"].(map[string]any)
77+
if !ok || v == nil {
78+
v = map[string]any{}
79+
}
80+
}
81+
return v
82+
}
83+
6684
var evalChart = func(chart map[string]any, hfbase string) (string, error) {
6785
nixChart, ok := chart["nixChart"].(string)
6886
if !ok {
@@ -95,10 +113,8 @@ var evalChart = func(chart map[string]any, hfbase string) (string, error) {
95113
log.Println("Failed to remove temporary file for values:", val.Name())
96114
}
97115
}()
98-
v := chart["values"].(map[string]any)
99-
if v == nil {
100-
v = map[string]any{}
101-
}
116+
117+
v := prepareChartValues(chart)
102118
if v["namespace"] != nil {
103119
log.Println("Warning: Do not set 'namespace' in values, use 'namespace' in the chart instead.")
104120
}
@@ -119,7 +135,7 @@ var evalChart = func(chart map[string]any, hfbase string) (string, error) {
119135

120136
err = val.Close()
121137
if err != nil {
122-
log.Fatalln("Failed to create temporary file for values:", err)
138+
log.Fatalln("Failed to close temporary file for values:", err)
123139
}
124140
expr := fmt.Sprintf(`(import %s).render "%s" "%s" "%s"`, f.Name(), fileName, base, val.Name())
125141
ne := nixeval.NewNixEval(expr)
@@ -136,7 +152,7 @@ var evalChart = func(chart map[string]any, hfbase string) (string, error) {
136152
if err != nil {
137153
log.Fatalln("Failed to create temporary directory for chart:", chart, " : ", err)
138154
}
139-
if err = os.WriteFile(chartDir+"/resources.yaml", yaml, 0644); err != nil {
155+
if err = os.WriteFile(chartDir+"/resources.yaml", yaml, 0o644); err != nil {
140156
log.Fatalln("Failed to write resources.yaml for chart:", chart, " : ", err)
141157
}
142158

pkgs/nixchart/nixchart_test.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestRenderCharts_Success(t *testing.T) {
1111
evalChart = func(chart map[string]any, base string) (string, error) {
1212
tmpDir := t.TempDir()
1313
resourcesPath := filepath.Join(tmpDir, "resources.yaml")
14-
if err := os.WriteFile(resourcesPath, []byte("mocked: true\n"), 0644); err != nil {
14+
if err := os.WriteFile(resourcesPath, []byte("mocked: true\n"), 0o644); err != nil {
1515
return "", err
1616
}
1717

@@ -50,3 +50,36 @@ func TestRenderCharts_Success(t *testing.T) {
5050

5151
CleanupCharts(cleanup)
5252
}
53+
54+
func TestPrepareChartValues(t *testing.T) {
55+
// Test with a map[string]any
56+
chartMap := map[string]any{
57+
"values": map[string]any{
58+
"a": 1,
59+
"b": "two",
60+
},
61+
}
62+
vals := prepareChartValues(chartMap)
63+
if vals["a"] != 1 || vals["b"] != "two" {
64+
t.Errorf("Expected map values, got: %#v", vals)
65+
}
66+
67+
// Test with a []map[string]any (should merge)
68+
chartList := map[string]any{
69+
"values": []map[string]any{
70+
{"a": 1, "b": "two"},
71+
{"b": "overwritten", "c": 3},
72+
},
73+
}
74+
vals = prepareChartValues(chartList)
75+
if vals["a"] != 1 || vals["b"] != "overwritten" || vals["c"] != 3 {
76+
t.Errorf("Expected merged values, got: %#v", vals)
77+
}
78+
79+
// Test with missing or nil values
80+
chartNil := map[string]any{}
81+
vals = prepareChartValues(chartNil)
82+
if len(vals) != 0 {
83+
t.Errorf("Expected empty map, got: %#v", vals)
84+
}
85+
}

testData/helm-nixchart/helmfile.nix

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@
2222
version = var.values.nginx_version;
2323
};
2424
}
25+
{
26+
name = "test-multival-nix";
27+
nixChart = "../nixChart/";
28+
createNamespace = true;
29+
namespace = "test-multi";
30+
values = [
31+
{
32+
replicas = 3;
33+
version = var.values.nginx_version;
34+
}
35+
{
36+
replicas = 4;
37+
}
38+
];
39+
}
2540
];
2641
}
2742
]

0 commit comments

Comments
 (0)