Skip to content

Commit f614e8b

Browse files
authored
feat: Helmfile renders *.yaml.gotmpl in a K8s manifests/kustomization directory (roboll#1745)
Related to roboll#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 roboll#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.
1 parent 85accf7 commit f614e8b

File tree

6 files changed

+29
-6
lines changed

6 files changed

+29
-6
lines changed

embrawtest/myapp/foo.yaml.gotmpl

Whitespace-only changes.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ require (
2828
github.com/spf13/cobra v1.1.1
2929
github.com/tatsushid/go-prettytable v0.0.0-20141013043238-ed2d14c29939
3030
github.com/urfave/cli v1.22.5
31-
github.com/variantdev/chartify v0.6.0
31+
github.com/variantdev/chartify v0.7.0
3232
github.com/variantdev/dag v0.0.0-20191028002400-bb0b3c785363
3333
github.com/variantdev/vals v0.13.0
3434
go.uber.org/multierr v1.6.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,8 @@ github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
573573
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
574574
github.com/variantdev/chartify v0.6.0 h1:QQ00a8Vtuhk6F9jeTZJEXV2g0zRXhYG43xovWZrc3ac=
575575
github.com/variantdev/chartify v0.6.0/go.mod h1:qF4XzQlkfH/6k2jAi1hLas+lK4zSCa8kY+r5JdmLA68=
576+
github.com/variantdev/chartify v0.7.0 h1:LQU9bW734CPTIRRx9AG6rIolqW3GgbqaFZQTOCNOfn8=
577+
github.com/variantdev/chartify v0.7.0/go.mod h1:qF4XzQlkfH/6k2jAi1hLas+lK4zSCa8kY+r5JdmLA68=
576578
github.com/variantdev/dag v0.0.0-20191028002400-bb0b3c785363 h1:KrfQBEUn+wEOQ/6UIfoqRDvn+Q/wZridQ7t0G1vQqKE=
577579
github.com/variantdev/dag v0.0.0-20191028002400-bb0b3c785363/go.mod h1:pH1TQsNSLj2uxMo9NNl9zdGy01Wtn+/2MT96BrKmVyE=
578580
github.com/variantdev/vals v0.13.0 h1:zdtTBjoWKkUGdFauxETkDVjqWXdjUNwI+ggWcUmpxv8=

pkg/state/helmx.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package state
22

33
import (
44
"fmt"
5-
"github.com/roboll/helmfile/pkg/helmexec"
6-
"github.com/roboll/helmfile/pkg/remote"
7-
"github.com/variantdev/chartify"
85
"os"
96
"path/filepath"
107
"strings"
8+
9+
"github.com/roboll/helmfile/pkg/helmexec"
10+
"github.com/roboll/helmfile/pkg/remote"
11+
"github.com/variantdev/chartify"
1112
)
1213

1314
type Dependency struct {
@@ -195,6 +196,8 @@ func (st *HelmState) PrepareChartify(helm helmexec.Interface, release *ReleaseSp
195196
filesNeedCleaning = append(filesNeedCleaning, generatedFiles...)
196197

197198
chartify.Opts.ValuesFiles = generatedFiles
199+
chartify.Opts.TemplateData = st.newReleaseTemplateData(release)
200+
chartify.Opts.TemplateFuncs = st.newReleaseTemplateFuncMap(dir)
198201

199202
return chartify, clean, nil
200203
}

pkg/state/state.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -2428,10 +2428,22 @@ func (st *HelmState) flagsForLint(helm helmexec.Interface, release *ReleaseSpec,
24282428
return flags, files, nil
24292429
}
24302430

2431-
func (st *HelmState) RenderReleaseValuesFileToBytes(release *ReleaseSpec, path string) ([]byte, error) {
2431+
func (st *HelmState) newReleaseTemplateData(release *ReleaseSpec) releaseTemplateData {
24322432
vals := st.Values()
24332433
templateData := st.createReleaseTemplateData(release, vals)
24342434

2435+
return templateData
2436+
}
2437+
2438+
func (st *HelmState) newReleaseTemplateFuncMap(dir string) template.FuncMap {
2439+
r := tmpl.NewFileRenderer(st.readFile, dir, nil)
2440+
2441+
return r.Context.CreateFuncMap()
2442+
}
2443+
2444+
func (st *HelmState) RenderReleaseValuesFileToBytes(release *ReleaseSpec, path string) ([]byte, error) {
2445+
templateData := st.newReleaseTemplateData(release)
2446+
24352447
r := tmpl.NewFileRenderer(st.readFile, filepath.Dir(path), templateData)
24362448
rawBytes, err := r.RenderToBytes(path)
24372449
if err != nil {

pkg/tmpl/context_tmpl.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"text/template"
77
)
88

9-
func (c *Context) newTemplate() *template.Template {
9+
func (c *Context) CreateFuncMap() template.FuncMap {
1010
aliased := template.FuncMap{}
1111

1212
aliases := map[string]string{
@@ -27,6 +27,12 @@ func (c *Context) newTemplate() *template.Template {
2727
funcMap[name] = f
2828
}
2929

30+
return funcMap
31+
}
32+
33+
func (c *Context) newTemplate() *template.Template {
34+
funcMap := c.CreateFuncMap()
35+
3036
tmpl := template.New("stringTemplate").Funcs(funcMap)
3137
if c.preRender {
3238
tmpl = tmpl.Option("missingkey=zero")

0 commit comments

Comments
 (0)