From 2c338db1bd9b8624dd908eb6ac137c913d9ec39d Mon Sep 17 00:00:00 2001 From: Lukas Eichler Date: Tue, 24 Oct 2017 19:39:05 +0200 Subject: [PATCH 1/3] fix(helm): Set template context inside tpl function to outer function. docs(helm): Added documentation about tpl function --- pkg/engine/engine.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index 595ec2c19..3019ef4c8 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -166,19 +166,32 @@ func (e *Engine) alterFuncMap(t *template.Template) template.FuncMap { // Add the 'tpl' function here 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", tpl, err.Error()) + } + r := renderable{ - tpl: tpl, - vals: vals, + tpl: tpl, + vals: vals, + basePath: basePath.(string), } + println(vals.Table) + 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", tpl, err.Error()) + } + + templates[templateName.(string)] = r result, err := e.render(templates) if err != nil { 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 From 1cebc760a036f41caf8ce2207237775b0d13055b Mon Sep 17 00:00:00 2001 From: Lukas Eichler Date: Wed, 25 Oct 2017 09:30:26 +0200 Subject: [PATCH 2/3] Fixed warning for missing formating parameter inside error message. --- pkg/engine/engine.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index 3019ef4c8..dfbc257ce 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -168,7 +168,7 @@ func (e *Engine) alterFuncMap(t *template.Template) template.FuncMap { 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", tpl, err.Error()) + return "", fmt.Errorf("Cannot retrieve Template.Basepath from values inside tpl function: %s (%s)", tpl, err.Error()) } r := renderable{ @@ -182,7 +182,7 @@ func (e *Engine) alterFuncMap(t *template.Template) template.FuncMap { templates := map[string]renderable{} templateName, err := vals.PathValue("Template.Name") if err != nil { - return "", fmt.Errorf("Cannot retrieve Template.Name from values inside tpl function", tpl, err.Error()) + return "", fmt.Errorf("Cannot retrieve Template.Name from values inside tpl function: %s (%s)", tpl, err.Error()) } templates[templateName.(string)] = r From 8bc7dede1864b94ced115a566c115e522b4af11b Mon Sep 17 00:00:00 2001 From: Lukas Eichler Date: Sun, 3 Dec 2017 17:39:34 +0100 Subject: [PATCH 3/3] fix(helm): Apply PR comments for tpl template name fix Modified existing unit test to verify the changed behavior. Removed debug print. --- pkg/engine/engine.go | 2 -- pkg/engine/engine_test.go | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index dfbc257ce..46e0a59cf 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -177,8 +177,6 @@ func (e *Engine) alterFuncMap(t *template.Template) template.FuncMap { basePath: basePath.(string), } - println(vals.Table) - templates := map[string]renderable{} templateName, err := vals.PathValue("Template.Name") if err != nil { diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index 7da4a9103..8ffb3d87c 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -538,7 +538,7 @@ func TestAlterFuncMap(t *testing.T) { Metadata: &chart.Metadata{Name: "TplFunction"}, Templates: []*chart.Template{ {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: ``}, Dependencies: []*chart.Chart{}, @@ -558,7 +558,7 @@ func TestAlterFuncMap(t *testing.T) { t.Fatal(err) } - expectedTplStrWithInclude := "\"TestRelease\"" + expectedTplStrWithInclude := "\"TplFunction/templates/base\"" if gotStrTplWithInclude := outTplWithInclude["TplFunction/templates/base"]; gotStrTplWithInclude != expectedTplStrWithInclude { t.Errorf("Expected %q, got %q (%v)", expectedTplStrWithInclude, gotStrTplWithInclude, outTplWithInclude) }