From 4dac2ea5656846c6809af175742e9896d3b4d55c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Fernando=20Carri=C3=B3n?= Date: Sat, 21 Oct 2023 15:06:55 +0200 Subject: [PATCH] use helm engine for helm templating --- helm/helm.go | 117 +++++++++++++++++++++++++++++++++++++++++++++ helmCompat/helm.go | 25 ---------- main.go | 13 ++--- 3 files changed, 124 insertions(+), 31 deletions(-) create mode 100644 helm/helm.go delete mode 100644 helmCompat/helm.go diff --git a/helm/helm.go b/helm/helm.go new file mode 100644 index 0000000..972708a --- /dev/null +++ b/helm/helm.go @@ -0,0 +1,117 @@ +package helm + +import ( + "os" + "path/filepath" + + "helm.sh/helm/v3/pkg/chart" + "helm.sh/helm/v3/pkg/chartutil" + "helm.sh/helm/v3/pkg/engine" +) + +type LangOpts struct { + Strict bool + LintMode bool + EnableDNS bool +} + +var defaultLangOpts = LangOpts{ + Strict: true, + LintMode: false, + EnableDNS: true, +} + +type Templater struct { + engine engine.Engine + chart chart.Chart +} + +func (t *Templater) Render(values map[string]interface{}) (map[string]string, error) { + t.chart.Values = values + return t.engine.Render(&t.chart, chartutil.Values(values)) +} + +func NewTemplaterWithLangOpts( + templates map[string][]byte, + langOpts *LangOpts, +) *Templater { + if langOpts == nil { + langOpts = &defaultLangOpts + } + + if templates == nil { + templates = map[string][]byte{ + "": []byte{}, + } + } + + ht := &Templater{ + engine: engine.Engine{ + Strict: langOpts.Strict, + LintMode: langOpts.LintMode, + EnableDNS: langOpts.EnableDNS, + }, + chart: chart.Chart{ + Raw: make([]*chart.File, 0, 0), + Metadata: &chart.Metadata{}, + Lock: &chart.Lock{}, + Templates: make([]*chart.File, len(templates), len(templates)), + Values: nil, + Schema: make([]byte, 0, 0), + Files: make([]*chart.File, 0, 0), + }, + } + + var i int + for name, data := range templates { + ht.chart.Templates[i] = &chart.File{ + Name: name, + Data: data, + } + i++ + } + return ht +} + +func NewTemplater(templates map[string][]byte) *Templater { + return NewTemplaterWithLangOpts(templates, nil) +} + +func NewTemplaterWithFiles( + tplFiles []string, + langOpts *LangOpts, +) (*Templater, error) { + tpls := map[string][]byte{} + var err error + for _, tf := range tplFiles { + tpls[filepath.Base(tf)], err = os.ReadFile(tf) + if err != nil { + return nil, err + } + } + return NewTemplaterWithLangOpts(tpls, langOpts), nil +} + +func NewTemplaterWithStr( + tplStr string, + langOpts *LangOpts, +) *Templater { + return NewTemplaterWithLangOpts( + map[string][]byte{ + "": []byte(tplStr), + }, + langOpts, + ) +} + +func NewTemplaterWithBytes( + tplBytes []byte, + langOpts *LangOpts, +) *Templater { + return NewTemplaterWithLangOpts( + map[string][]byte{ + "": tplBytes, + }, + langOpts, + ) +} diff --git a/helmCompat/helm.go b/helmCompat/helm.go deleted file mode 100644 index c7ba77f..0000000 --- a/helmCompat/helm.go +++ /dev/null @@ -1,25 +0,0 @@ -package helmCompat - -import ( - "helm.sh/helm/v3/pkg/chart" - "helm.sh/helm/v3/pkg/chartutil" - "helm.sh/helm/v3/pkg/engine" -) - -type HelmCompatLayer struct { - engine engine.Engine - chart chart.Chart - values chartutil.Values -} - -func NewHelmCompatLayer() *HelmCompatLayer { - return &HelmCompatLayer{} -} - -func NewHelmCompatLayerWithFiles(tplFiles []string) *HelmCompatLayer { - return NewHelmCompatLayer() -} - -func NewHelmCompatLayerWithStr(tplStr string) *HelmCompatLayer { - return NewHelmCompatLayer() -} diff --git a/main.go b/main.go index f8da464..7d74542 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ import ( sprig "github.com/Masterminds/sprig/v3" yaml "gopkg.in/yaml.v3" - helm "gitlab.com/CRThaze/spry/helmCompat" + "gitlab.com/CRThaze/spry/helm" ) type StrSet map[string]struct{} @@ -256,7 +256,7 @@ func mergeValues() (res map[string]interface{}) { func main() { var err error - var hc *helm.HelmCompatLayer + var ht *helm.Templater tpl := template.New("base").Funcs(sprig.FuncMap()) values := mergeValues() @@ -282,7 +282,7 @@ func main() { checkErr(err) if flags.helmCompat { - hc = helm.NewHelmCompatLayerWithStr(string(data)) + ht = helm.NewTemplaterWithBytes(data, nil) fmt.Fprintf(os.Stderr, "WARN: Helm Compatibility only partially implemented.\n") tpl, err = tpl.Parse(string(data)) } else { @@ -296,7 +296,7 @@ func main() { checkErr(err) if flags.helmCompat { - hc = helm.NewHelmCompatLayerWithStr(string(data)) + ht = helm.NewTemplaterWithBytes(data, nil) fmt.Fprintf(os.Stderr, "WARN: Helm Compatibility only partially implemented.\n") tpl, err = tpl.Parse(string(data)) } else { @@ -308,7 +308,8 @@ func main() { tplFiles := expandFiles(flag.Args()) if flags.helmCompat { fmt.Fprintf(os.Stderr, "WARN: Helm Compatibility only partially implemented.\n") - hc = helm.NewHelmCompatLayerWithFiles(tplFiles) + ht, err = helm.NewTemplaterWithFiles(tplFiles, nil) + checkErr(err) tpl, err = tpl.ParseFiles(tplFiles...) checkErr(err) for i, file := range tplFiles { @@ -329,7 +330,7 @@ func main() { } return } - if hc != nil { + if ht != nil { checkErr(tpl.Execute(os.Stdout, values)) } else { checkErr(tpl.Execute(os.Stdout, values))