Skip to content

Commit

Permalink
Port PR 7114 to Helm 2 (#7128)
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Hickey <martin.hickey@ie.ibm.com>
(cherry picked from commit c306058)
  • Loading branch information
hickeyma authored and mattfarina committed Mar 23, 2020
1 parent 1ee0254 commit 18ab2b9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{
Expand Down

0 comments on commit 18ab2b9

Please sign in to comment.