use helm engine for helm templating

This commit is contained in:
Diego Fernando Carrión 2023-10-21 15:06:55 +02:00
parent 24f6aa711a
commit 4dac2ea565
Signed by: CRThaze
GPG Key ID: 0F647F8A6A3AF588
3 changed files with 124 additions and 31 deletions

117
helm/helm.go Normal file
View File

@ -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,
)
}

View File

@ -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()
}

13
main.go
View File

@ -17,7 +17,7 @@ import (
sprig "github.com/Masterminds/sprig/v3" sprig "github.com/Masterminds/sprig/v3"
yaml "gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
helm "gitlab.com/CRThaze/spry/helmCompat" "gitlab.com/CRThaze/spry/helm"
) )
type StrSet map[string]struct{} type StrSet map[string]struct{}
@ -256,7 +256,7 @@ func mergeValues() (res map[string]interface{}) {
func main() { func main() {
var err error var err error
var hc *helm.HelmCompatLayer var ht *helm.Templater
tpl := template.New("base").Funcs(sprig.FuncMap()) tpl := template.New("base").Funcs(sprig.FuncMap())
values := mergeValues() values := mergeValues()
@ -282,7 +282,7 @@ func main() {
checkErr(err) checkErr(err)
if flags.helmCompat { if flags.helmCompat {
hc = helm.NewHelmCompatLayerWithStr(string(data)) ht = helm.NewTemplaterWithBytes(data, nil)
fmt.Fprintf(os.Stderr, "WARN: Helm Compatibility only partially implemented.\n") fmt.Fprintf(os.Stderr, "WARN: Helm Compatibility only partially implemented.\n")
tpl, err = tpl.Parse(string(data)) tpl, err = tpl.Parse(string(data))
} else { } else {
@ -296,7 +296,7 @@ func main() {
checkErr(err) checkErr(err)
if flags.helmCompat { if flags.helmCompat {
hc = helm.NewHelmCompatLayerWithStr(string(data)) ht = helm.NewTemplaterWithBytes(data, nil)
fmt.Fprintf(os.Stderr, "WARN: Helm Compatibility only partially implemented.\n") fmt.Fprintf(os.Stderr, "WARN: Helm Compatibility only partially implemented.\n")
tpl, err = tpl.Parse(string(data)) tpl, err = tpl.Parse(string(data))
} else { } else {
@ -308,7 +308,8 @@ func main() {
tplFiles := expandFiles(flag.Args()) tplFiles := expandFiles(flag.Args())
if flags.helmCompat { if flags.helmCompat {
fmt.Fprintf(os.Stderr, "WARN: Helm Compatibility only partially implemented.\n") 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...) tpl, err = tpl.ParseFiles(tplFiles...)
checkErr(err) checkErr(err)
for i, file := range tplFiles { for i, file := range tplFiles {
@ -329,7 +330,7 @@ func main() {
} }
return return
} }
if hc != nil { if ht != nil {
checkErr(tpl.Execute(os.Stdout, values)) checkErr(tpl.Execute(os.Stdout, values))
} else { } else {
checkErr(tpl.Execute(os.Stdout, values)) checkErr(tpl.Execute(os.Stdout, values))