Port PR 7114 to Helm 2 (#7128)

Signed-off-by: Martin Hickey <martin.hickey@ie.ibm.com>
pull/7319/head
Martin Hickey 5 years ago committed by GitHub
parent 0ca95341c1
commit c3060582bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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
}

@ -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{

Loading…
Cancel
Save