Skip to content

Commit

Permalink
feat(pkg/engine): expose subcharts scope in parent
Browse files Browse the repository at this point in the history
Expose the scope (.Values, .Charts, .Releases etc.) of subcharts to the parent scope.

Signed-off-by: Valentin Flaux <vflaux@oui.sncf>
  • Loading branch information
Valentin Flaux authored and vflaux committed Jul 20, 2021
1 parent 433b90c commit 3daaea0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
9 changes: 7 additions & 2 deletions pkg/engine/engine.go
Expand Up @@ -260,6 +260,7 @@ func (e Engine) renderWithReferences(tpls, referenceTpls map[string]renderable)
if err := t.ExecuteTemplate(&buf, filename, vals); err != nil {
return map[string]string{}, cleanupExecError(filename, err)
}
delete(vals, "Template")

// Work around the issue where Go will emit "<no value>" even if Options(missing=zero)
// is set. Since missing=error will never get here, we do not need to handle
Expand Down Expand Up @@ -344,13 +345,15 @@ func allTemplates(c *chart.Chart, vals chartutil.Values) map[string]renderable {
//
// As it recurses, it also sets the values to be appropriate for the template
// scope.
func recAllTpls(c *chart.Chart, templates map[string]renderable, vals chartutil.Values) {
func recAllTpls(c *chart.Chart, templates map[string]renderable, vals chartutil.Values) map[string]interface{} {
subCharts := make(map[string]interface{})
next := map[string]interface{}{
"Chart": c.Metadata,
"Files": newFiles(c.Files),
"Release": vals["Release"],
"Capabilities": vals["Capabilities"],
"Values": make(chartutil.Values),
"Subcharts": subCharts,
}

// If there is a {{.Values.ThisChart}} in the parent metadata,
Expand All @@ -362,7 +365,7 @@ func recAllTpls(c *chart.Chart, templates map[string]renderable, vals chartutil.
}

for _, child := range c.Dependencies() {
recAllTpls(child, templates, next)
subCharts[child.Name()] = recAllTpls(child, templates, next)
}

newParentID := c.ChartFullPath()
Expand All @@ -376,6 +379,8 @@ func recAllTpls(c *chart.Chart, templates map[string]renderable, vals chartutil.
basePath: path.Join(newParentID, "templates"),
}
}

return next
}

// isTemplateValid returns true if the template is valid for the chart type
Expand Down
20 changes: 16 additions & 4 deletions pkg/engine/engine_test.go
Expand Up @@ -384,35 +384,39 @@ func TestRenderNestedValues(t *testing.T) {
// Ensure namespacing rules are working.
deepestpath := "templates/inner.tpl"
checkrelease := "templates/release.tpl"
// Ensure subcharts scopes are working.
subchartspath := "templates/subcharts.tpl"

deepest := &chart.Chart{
Metadata: &chart.Metadata{Name: "deepest"},
Templates: []*chart.File{
{Name: deepestpath, Data: []byte(`And this same {{.Values.what}} that smiles {{.Values.global.when}}`)},
{Name: checkrelease, Data: []byte(`Tomorrow will be {{default "happy" .Release.Name }}`)},
},
Values: map[string]interface{}{"what": "milkshake"},
Values: map[string]interface{}{"what": "milkshake", "where": "here"},
}

inner := &chart.Chart{
Metadata: &chart.Metadata{Name: "herrick"},
Templates: []*chart.File{
{Name: innerpath, Data: []byte(`Old {{.Values.who}} is still a-flyin'`)},
},
Values: map[string]interface{}{"who": "Robert"},
Values: map[string]interface{}{"who": "Robert", "what": "glasses"},
}
inner.AddDependency(deepest)

outer := &chart.Chart{
Metadata: &chart.Metadata{Name: "top"},
Templates: []*chart.File{
{Name: outerpath, Data: []byte(`Gather ye {{.Values.what}} while ye may`)},
{Name: subchartspath, Data: []byte(`The glorious Lamp of {{.Subcharts.herrick.Subcharts.deepest.Values.where}}, the {{.Subcharts.herrick.Values.what}}`)},
},
Values: map[string]interface{}{
"what": "stinkweed",
"who": "me",
"herrick": map[string]interface{}{
"who": "time",
"who": "time",
"what": "Sun",
},
},
}
Expand All @@ -422,7 +426,8 @@ func TestRenderNestedValues(t *testing.T) {
"what": "rosebuds",
"herrick": map[string]interface{}{
"deepest": map[string]interface{}{
"what": "flower",
"what": "flower",
"where": "Heaven",
},
},
"global": map[string]interface{}{
Expand Down Expand Up @@ -469,6 +474,11 @@ func TestRenderNestedValues(t *testing.T) {
if out[fullcheckrelease] != "Tomorrow will be dyin" {
t.Errorf("Unexpected release: %q", out[fullcheckrelease])
}

fullchecksubcharts := "top/" + subchartspath
if out[fullchecksubcharts] != "The glorious Lamp of Heaven, the Sun" {
t.Errorf("Unexpected subcharts: %q", out[fullchecksubcharts])
}
}

func TestRenderBuiltinValues(t *testing.T) {
Expand All @@ -488,6 +498,7 @@ func TestRenderBuiltinValues(t *testing.T) {
Metadata: &chart.Metadata{Name: "Troy"},
Templates: []*chart.File{
{Name: "templates/Aeneas", Data: []byte(`{{.Template.Name}}{{.Chart.Name}}{{.Release.Name}}`)},
{Name: "templates/Amata", Data: []byte(`{{.Subcharts.Latium.Chart.Name}} {{.Subcharts.Latium.Files.author | printf "%s"}}`)},
},
}
outer.AddDependency(inner)
Expand All @@ -510,6 +521,7 @@ func TestRenderBuiltinValues(t *testing.T) {
expects := map[string]string{
"Troy/charts/Latium/templates/Lavinia": "Troy/charts/Latium/templates/LaviniaLatiumAeneid",
"Troy/templates/Aeneas": "Troy/templates/AeneasTroyAeneid",
"Troy/templates/Amata": "Latium Virgil",
"Troy/charts/Latium/templates/From": "Virgil Aeneid",
}
for file, expect := range expects {
Expand Down

0 comments on commit 3daaea0

Please sign in to comment.