Files
helmfile/pkg/tmpl/context_tmpl.go
T
Yusuke Kuoka f614e8b1bf feat: Helmfile renders *.yaml.gotmpl in a K8s manifests/kustomization directory (#1745)
Related to #494

This feature is mostly a built-in alternative to the `incubator/raw` chart without external dependency and has
access to helmfile's own template functions and template data.

The expected use-case of this feature is to add arbitrary K8s resources to your deployment.

Unlike the original issue raised in #494 this doesn't enable you to add arbitary resources to a release. That's another story. But this would be a good foundation for that, too.
2021-04-06 14:22:34 +09:00

64 lines
1.1 KiB
Go

package tmpl
import (
"bytes"
"github.com/Masterminds/sprig/v3"
"text/template"
)
func (c *Context) CreateFuncMap() template.FuncMap {
aliased := template.FuncMap{}
aliases := map[string]string{
"get": "sprigGet",
}
funcMap := sprig.TxtFuncMap()
for orig, alias := range aliases {
aliased[alias] = funcMap[orig]
}
for name, f := range c.createFuncMap() {
funcMap[name] = f
}
for name, f := range aliased {
funcMap[name] = f
}
return funcMap
}
func (c *Context) newTemplate() *template.Template {
funcMap := c.CreateFuncMap()
tmpl := template.New("stringTemplate").Funcs(funcMap)
if c.preRender {
tmpl = tmpl.Option("missingkey=zero")
} else {
tmpl = tmpl.Option("missingkey=error")
}
return tmpl
}
func (c *Context) RenderTemplateToBuffer(s string, data ...interface{}) (*bytes.Buffer, error) {
var t, parseErr = c.newTemplate().Parse(s)
if parseErr != nil {
return nil, parseErr
}
var tplString bytes.Buffer
var d interface{}
if len(data) > 0 {
d = data[0]
}
var execErr = t.Execute(&tplString, d)
if execErr != nil {
return &tplString, execErr
}
return &tplString, nil
}