luet/pkg/helpers/helm.go

98 lines
2.5 KiB
Go
Raw Normal View History

2020-10-04 17:16:01 +00:00
package helpers
import (
2020-11-08 20:14:19 +00:00
"io/ioutil"
"github.com/imdario/mergo"
2020-10-30 18:15:04 +00:00
"github.com/pkg/errors"
2020-11-08 20:14:19 +00:00
"gopkg.in/yaml.v2"
2020-10-04 17:16:01 +00:00
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/engine"
)
// RenderHelm renders the template string with helm
func RenderHelm(template string, values, d map[string]interface{}) (string, error) {
2020-10-04 17:16:01 +00:00
c := &chart.Chart{
Metadata: &chart.Metadata{
Name: "",
Version: "",
},
Templates: []*chart.File{
{Name: "templates", Data: []byte(template)},
},
2020-10-30 18:15:04 +00:00
Values: map[string]interface{}{"Values": values},
2020-10-04 17:16:01 +00:00
}
v, err := chartutil.CoalesceValues(c, map[string]interface{}{"Values": d})
2020-10-04 17:16:01 +00:00
if err != nil {
2020-10-30 18:15:04 +00:00
return "", errors.Wrap(err, "while rendering template")
2020-10-04 17:16:01 +00:00
}
out, err := engine.Render(c, v)
if err != nil {
2020-10-30 18:15:04 +00:00
return "", errors.Wrap(err, "while rendering template")
2020-10-04 17:16:01 +00:00
}
2020-10-30 18:15:04 +00:00
return out["templates"], nil
2020-10-04 17:16:01 +00:00
}
2020-11-08 20:14:19 +00:00
type templatedata map[string]interface{}
// UnMarshalValues unmarshal values files and joins them into a unique templatedata
// the join happens from right to left, so any rightmost value file overwrites the content of the ones before it.
func UnMarshalValues(values []string) (templatedata, error) {
dst := templatedata{}
if len(values) > 0 {
for _, bv := range reverse(values) {
current := templatedata{}
defBuild, err := ioutil.ReadFile(bv)
if err != nil {
return nil, errors.Wrap(err, "rendering file "+bv)
}
err = yaml.Unmarshal(defBuild, &current)
if err != nil {
return nil, errors.Wrap(err, "rendering file "+bv)
}
if err := mergo.Merge(&dst, current); err != nil {
return nil, errors.Wrap(err, "merging values file "+bv)
}
}
}
return dst, nil
}
func reverse(s []string) []string {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
func RenderFiles(toTemplate, valuesFile string, defaultFile ...string) (string, error) {
2020-11-08 20:14:19 +00:00
raw, err := ioutil.ReadFile(toTemplate)
if err != nil {
return "", errors.Wrap(err, "reading file "+toTemplate)
}
if !Exists(valuesFile) {
return "", errors.Wrap(err, "file not existing "+valuesFile)
}
val, err := ioutil.ReadFile(valuesFile)
2020-11-08 20:14:19 +00:00
if err != nil {
return "", errors.Wrap(err, "reading file "+valuesFile)
}
var values templatedata
if err = yaml.Unmarshal(val, &values); err != nil {
2020-11-08 20:14:19 +00:00
return "", errors.Wrap(err, "unmarshalling file "+toTemplate)
}
dst, err := UnMarshalValues(defaultFile)
if err != nil {
return "", errors.Wrap(err, "unmarshalling values")
}
return RenderHelm(string(raw), values, dst)
2020-11-08 20:14:19 +00:00
}