Unverified Commit 78d6b930 authored by Matthew Fisher's avatar Matthew Fisher Committed by GitHub

Merge pull request #3061 from eicnix/feature/tpl-fix

fix(helm): Set template context inside tpl function to outer function.
parents 6c585d47 8bc7dede
...@@ -166,19 +166,30 @@ func (e *Engine) alterFuncMap(t *template.Template) template.FuncMap { ...@@ -166,19 +166,30 @@ func (e *Engine) alterFuncMap(t *template.Template) template.FuncMap {
// Add the 'tpl' function here // Add the 'tpl' function here
funcMap["tpl"] = func(tpl string, vals chartutil.Values) (string, error) { funcMap["tpl"] = func(tpl string, vals chartutil.Values) (string, error) {
basePath, err := vals.PathValue("Template.BasePath")
if err != nil {
return "", fmt.Errorf("Cannot retrieve Template.Basepath from values inside tpl function: %s (%s)", tpl, err.Error())
}
r := renderable{ r := renderable{
tpl: tpl, tpl: tpl,
vals: vals, vals: vals,
basePath: basePath.(string),
} }
templates := map[string]renderable{} templates := map[string]renderable{}
templates["aaa_template"] = r templateName, err := vals.PathValue("Template.Name")
if err != nil {
return "", fmt.Errorf("Cannot retrieve Template.Name from values inside tpl function: %s (%s)", tpl, err.Error())
}
templates[templateName.(string)] = r
result, err := e.render(templates) result, err := e.render(templates)
if err != nil { if err != nil {
return "", fmt.Errorf("Error during tpl function execution for %q: %s", tpl, err.Error()) return "", fmt.Errorf("Error during tpl function execution for %q: %s", tpl, err.Error())
} }
return result["aaa_template"], nil return result[templateName.(string)], nil
} }
return funcMap return funcMap
......
...@@ -538,7 +538,7 @@ func TestAlterFuncMap(t *testing.T) { ...@@ -538,7 +538,7 @@ func TestAlterFuncMap(t *testing.T) {
Metadata: &chart.Metadata{Name: "TplFunction"}, Metadata: &chart.Metadata{Name: "TplFunction"},
Templates: []*chart.Template{ Templates: []*chart.Template{
{Name: "templates/base", Data: []byte(`{{ tpl "{{include ` + "`" + `TplFunction/templates/_partial` + "`" + ` . | quote }}" .}}`)}, {Name: "templates/base", Data: []byte(`{{ tpl "{{include ` + "`" + `TplFunction/templates/_partial` + "`" + ` . | quote }}" .}}`)},
{Name: "templates/_partial", Data: []byte(`{{.Release.Name}}`)}, {Name: "templates/_partial", Data: []byte(`{{.Template.Name}}`)},
}, },
Values: &chart.Config{Raw: ``}, Values: &chart.Config{Raw: ``},
Dependencies: []*chart.Chart{}, Dependencies: []*chart.Chart{},
...@@ -558,7 +558,7 @@ func TestAlterFuncMap(t *testing.T) { ...@@ -558,7 +558,7 @@ func TestAlterFuncMap(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
expectedTplStrWithInclude := "\"TestRelease\"" expectedTplStrWithInclude := "\"TplFunction/templates/base\""
if gotStrTplWithInclude := outTplWithInclude["TplFunction/templates/base"]; gotStrTplWithInclude != expectedTplStrWithInclude { if gotStrTplWithInclude := outTplWithInclude["TplFunction/templates/base"]; gotStrTplWithInclude != expectedTplStrWithInclude {
t.Errorf("Expected %q, got %q (%v)", expectedTplStrWithInclude, gotStrTplWithInclude, outTplWithInclude) t.Errorf("Expected %q, got %q (%v)", expectedTplStrWithInclude, gotStrTplWithInclude, outTplWithInclude)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment