diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index b4b6475c9..6063fdff0 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -26,6 +26,7 @@ import ( "text/template" "github.com/Masterminds/sprig" + "github.com/pkg/errors" "k8s.io/helm/pkg/chartutil" "k8s.io/helm/pkg/proto/hapi/chart" @@ -144,12 +145,21 @@ func (e *Engine) alterFuncMap(t *template.Template, referenceTpls map[string]ren funcMap[k] = v } + includedNames := make([]string, 0) + // Add the 'include' function here so we can close over t. funcMap["include"] = func(name string, data interface{}) (string, error) { buf := bytes.NewBuffer(nil) + for _, n := range includedNames { + if n == name { + return "", errors.Wrapf(fmt.Errorf("unable to excute template"), "rendering template has a nested reference name: %s", name) + } + } + includedNames = append(includedNames, name) if err := t.ExecuteTemplate(buf, name, data); err != nil { return "", err } + includedNames = includedNames[:len(includedNames)-1] return buf.String(), nil } diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index 712b3b3df..c130540aa 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -423,6 +423,15 @@ func TestAlterFuncMap(t *testing.T) { Dependencies: []*chart.Chart{}, } + // Check nested reference in include FuncMap + d := &chart.Chart{ + Metadata: &chart.Metadata{Name: "nested"}, + Templates: []*chart.Template{ + {Name: "templates/quote", Data: []byte(`{{include "nested/templates/quote" . | indent 2}} dead.`)}, + {Name: "templates/_partial", Data: []byte(`{{.Release.Name}} - he`)}, + }, + } + v := chartutil.Values{ "Values": &chart.Config{Raw: ""}, "Chart": c.Metadata, @@ -441,6 +450,12 @@ func TestAlterFuncMap(t *testing.T) { t.Errorf("Expected %q, got %q (%v)", expect, got, out) } + _, err = New().Render(d, v) + expectErrName := "nested/templates/quote" + if err == nil { + t.Errorf("Expected err of nested reference name: %v", expectErrName) + } + reqChart := &chart.Chart{ Metadata: &chart.Metadata{Name: "conan"}, Templates: []*chart.Template{