Skip to content

Commit

Permalink
feat: Helmfile renders *.yaml.gotmpl in a K8s manifests/kustomization…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
mumoshu authored Apr 6, 2021
1 parent 85accf7 commit f614e8b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
Empty file.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
github.com/spf13/cobra v1.1.1
github.com/tatsushid/go-prettytable v0.0.0-20141013043238-ed2d14c29939
github.com/urfave/cli v1.22.5
github.com/variantdev/chartify v0.6.0
github.com/variantdev/chartify v0.7.0
github.com/variantdev/dag v0.0.0-20191028002400-bb0b3c785363
github.com/variantdev/vals v0.13.0
go.uber.org/multierr v1.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/variantdev/chartify v0.6.0 h1:QQ00a8Vtuhk6F9jeTZJEXV2g0zRXhYG43xovWZrc3ac=
github.com/variantdev/chartify v0.6.0/go.mod h1:qF4XzQlkfH/6k2jAi1hLas+lK4zSCa8kY+r5JdmLA68=
github.com/variantdev/chartify v0.7.0 h1:LQU9bW734CPTIRRx9AG6rIolqW3GgbqaFZQTOCNOfn8=
github.com/variantdev/chartify v0.7.0/go.mod h1:qF4XzQlkfH/6k2jAi1hLas+lK4zSCa8kY+r5JdmLA68=
github.com/variantdev/dag v0.0.0-20191028002400-bb0b3c785363 h1:KrfQBEUn+wEOQ/6UIfoqRDvn+Q/wZridQ7t0G1vQqKE=
github.com/variantdev/dag v0.0.0-20191028002400-bb0b3c785363/go.mod h1:pH1TQsNSLj2uxMo9NNl9zdGy01Wtn+/2MT96BrKmVyE=
github.com/variantdev/vals v0.13.0 h1:zdtTBjoWKkUGdFauxETkDVjqWXdjUNwI+ggWcUmpxv8=
Expand Down
9 changes: 6 additions & 3 deletions pkg/state/helmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package state

import (
"fmt"
"github.com/roboll/helmfile/pkg/helmexec"
"github.com/roboll/helmfile/pkg/remote"
"github.com/variantdev/chartify"
"os"
"path/filepath"
"strings"

"github.com/roboll/helmfile/pkg/helmexec"
"github.com/roboll/helmfile/pkg/remote"
"github.com/variantdev/chartify"
)

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

chartify.Opts.ValuesFiles = generatedFiles
chartify.Opts.TemplateData = st.newReleaseTemplateData(release)
chartify.Opts.TemplateFuncs = st.newReleaseTemplateFuncMap(dir)

return chartify, clean, nil
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2428,10 +2428,22 @@ func (st *HelmState) flagsForLint(helm helmexec.Interface, release *ReleaseSpec,
return flags, files, nil
}

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

return templateData
}

func (st *HelmState) newReleaseTemplateFuncMap(dir string) template.FuncMap {
r := tmpl.NewFileRenderer(st.readFile, dir, nil)

return r.Context.CreateFuncMap()
}

func (st *HelmState) RenderReleaseValuesFileToBytes(release *ReleaseSpec, path string) ([]byte, error) {
templateData := st.newReleaseTemplateData(release)

r := tmpl.NewFileRenderer(st.readFile, filepath.Dir(path), templateData)
rawBytes, err := r.RenderToBytes(path)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion pkg/tmpl/context_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"text/template"
)

func (c *Context) newTemplate() *template.Template {
func (c *Context) CreateFuncMap() template.FuncMap {
aliased := template.FuncMap{}

aliases := map[string]string{
Expand All @@ -27,6 +27,12 @@ func (c *Context) newTemplate() *template.Template {
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")
Expand Down

0 comments on commit f614e8b

Please sign in to comment.