feat(engine): add recursive template resolution

pull/613/head
Matt Butcher 9 years ago
parent bb8fca9b69
commit 6077968341

@ -66,10 +66,7 @@ func (e *Engine) Render(chrt *chart.Chart, vals *chart.Config) (map[string]strin
} }
// Render the charts // Render the charts
tmap := make(map[string]string, len(chrt.Templates)) tmap := allTemplates(chrt)
for _, tpl := range chrt.Templates {
tmap[tpl.Name] = string(tpl.Data)
}
return e.render(tmap, cvals) return e.render(tmap, cvals)
} }
@ -103,3 +100,17 @@ func (e *Engine) render(tpls map[string]string, v interface{}) (map[string]strin
return rendered, nil return rendered, nil
} }
// allTemplates returns all templates for a chart and its dependencies.
func allTemplates(c *chart.Chart) map[string]string {
templates := map[string]string{}
for _, child := range c.Dependencies {
for _, t := range child.Templates {
templates[t.Name] = string(t.Data)
}
}
for _, t := range c.Templates {
templates[t.Name] = string(t.Data)
}
return templates
}

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"sync" "sync"
"testing" "testing"
"github.com/deis/tiller/pkg/proto/hapi/chart"
) )
func TestEngine(t *testing.T) { func TestEngine(t *testing.T) {
@ -80,3 +82,55 @@ func TestParallelRenderInternals(t *testing.T) {
} }
wg.Wait() wg.Wait()
} }
func TestAllTemplates(t *testing.T) {
ch1 := &chart.Chart{
Templates: []*chart.Template{
{Name: "foo", Data: []byte("foo")},
{Name: "bar", Data: []byte("bar")},
},
Dependencies: []*chart.Chart{
{Templates: []*chart.Template{
{Name: "pinky", Data: []byte("pinky")},
{Name: "brain", Data: []byte("brain")},
}},
},
}
tpls := allTemplates(ch1)
if len(tpls) != 4 {
t.Errorf("Expected 4 charts, got %d", len(tpls))
}
}
func TestRenderDependency(t *testing.T) {
e := New()
deptpl := `{{define "myblock"}}World{{end}}`
toptpl := `Hello {{template "myblock"}}`
ch := &chart.Chart{
Templates: []*chart.Template{
{Name: "outer", Data: []byte(toptpl)},
},
Dependencies: []*chart.Chart{
{Templates: []*chart.Template{
{Name: "inner", Data: []byte(deptpl)},
}},
},
}
out, err := e.Render(ch, nil)
if err != nil {
t.Fatalf("failed to render chart: %s", err)
}
if len(out) != 2 {
t.Errorf("Expected 2, got %d", len(out))
}
expect := "Hello World"
if out["outer"] != expect {
t.Errorf("Expected %q, got %q", expect, out["outer"])
}
}

Loading…
Cancel
Save