Skip to content

Commit 7538246

Browse files
dastrobuchrko
authored andcommitted
copy dependency metadata on aliasing to avoid sharing imported values
imported values are stored in dependency objects, which breaks if a chart dependency is shared among multiple aliases. By copying the dependency objects in the metadata values can be imported correctly. Supersedes helm#10174 Signed-off-by: Daniel Strobusch <1847260+dastrobu@users.noreply.github.com>
1 parent 3d8990f commit 7538246

8 files changed

Lines changed: 102 additions & 3 deletions

File tree

pkg/chartutil/dependencies.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,30 @@ func getAliasDependency(charts []*chart.Chart, dep *chart.Dependency) *chart.Cha
117117
}
118118

119119
out := *c
120-
md := *c.Metadata
121-
out.Metadata = &md
120+
out.Metadata = copyMetadata(c.Metadata)
122121

123122
if dep.Alias != "" {
124-
md.Name = dep.Alias
123+
out.Metadata.Name = dep.Alias
125124
}
126125
return &out
127126
}
128127
return nil
129128
}
130129

130+
func copyMetadata(metadata *chart.Metadata) *chart.Metadata {
131+
md := *metadata
132+
133+
if md.Dependencies != nil {
134+
dependencies := make([]*chart.Dependency, len(md.Dependencies))
135+
for i := range md.Dependencies {
136+
dependency := *md.Dependencies[i]
137+
dependencies[i] = &dependency
138+
}
139+
md.Dependencies = dependencies
140+
}
141+
return &md
142+
}
143+
131144
// processDependencyEnabled removes disabled charts from dependencies
132145
func processDependencyEnabled(c *chart.Chart, v map[string]interface{}, path string) error {
133146
if c.Metadata.Dependencies == nil {

pkg/chartutil/dependencies_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,38 @@ func TestProcessDependencyImportValues(t *testing.T) {
271271
}
272272
}
273273

274+
func TestProcessDependencyImportValuesFromSharedDependencyToAliases(t *testing.T) {
275+
c := loadChart(t, "testdata/chart-with-import-from-aliased-dependencies")
276+
277+
if err := processDependencyEnabled(c, c.Values, ""); err != nil {
278+
t.Fatalf("expected no errors but got %q", err)
279+
}
280+
if err := processDependencyImportValues(c, true); err != nil {
281+
t.Fatalf("processing import values dependencies %v", err)
282+
}
283+
e := make(map[string]string)
284+
285+
e["foo-defaults.defaultValue"] = "42"
286+
e["bar-defaults.defaultValue"] = "42"
287+
288+
e["foo.defaults.defaultValue"] = "42"
289+
e["bar.defaults.defaultValue"] = "42"
290+
291+
e["foo.grandchild.defaults.defaultValue"] = "42"
292+
e["bar.grandchild.defaults.defaultValue"] = "42"
293+
294+
cValues := Values(c.Values)
295+
for kk, vv := range e {
296+
pv, err := cValues.PathValue(kk)
297+
if err != nil {
298+
t.Fatalf("retrieving import values table %v %v", kk, err)
299+
}
300+
if pv != vv {
301+
t.Errorf("failed to match imported value %v with expected %v", pv, vv)
302+
}
303+
}
304+
}
305+
274306
func TestProcessDependencyImportValuesMultiLevelPrecedence(t *testing.T) {
275307
c := loadChart(t, "testdata/three-level-dependent-chart/umbrella")
276308

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: v2
2+
appVersion: 1.0.0
3+
name: chart-with-dependency-aliased-twice
4+
type: application
5+
version: 1.0.0
6+
7+
dependencies:
8+
- name: child
9+
alias: foo
10+
version: 1.0.0
11+
import-values:
12+
- parent: foo-defaults
13+
child: defaults
14+
- name: child
15+
alias: bar
16+
version: 1.0.0
17+
import-values:
18+
- parent: bar-defaults
19+
child: defaults
20+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v2
2+
appVersion: 1.0.0
3+
name: child
4+
type: application
5+
version: 1.0.0
6+
7+
dependencies:
8+
- name: grandchild
9+
version: 1.0.0
10+
import-values:
11+
- parent: defaults
12+
child: defaults
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: v2
2+
appVersion: 1.0.0
3+
name: grandchild
4+
type: application
5+
version: 1.0.0
6+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
defaults:
2+
defaultValue: "42"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: {{ .Chart.Name }}
5+
data:
6+
{{ .Values.defaults | toYaml }}
7+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: {{ .Chart.Name }}
5+
data:
6+
{{ toYaml .Values.defaults | indent 2 }}
7+

0 commit comments

Comments
 (0)