Allow multiple build values files

Fixes #198
This commit is contained in:
Ettore Di Giacinto
2021-04-12 12:04:04 +02:00
parent c022c75239
commit 44cae094e8
3 changed files with 62 additions and 35 deletions

View File

@@ -100,7 +100,7 @@ Build packages specifying multiple definition trees:
databaseType := viper.GetString("database") databaseType := viper.GetString("database")
compressionType := viper.GetString("compression") compressionType := viper.GetString("compression")
imageRepository := viper.GetString("image-repository") imageRepository := viper.GetString("image-repository")
values := viper.GetString("values") values := viper.GetStringSlice("values")
wait := viper.GetBool("wait") wait := viper.GetBool("wait")
push := viper.GetBool("push") push := viper.GetBool("push")
pull := viper.GetBool("pull") pull := viper.GetBool("pull")
@@ -311,7 +311,7 @@ func init() {
buildCmd.Flags().Bool("revdeps", false, "Build with revdeps") buildCmd.Flags().Bool("revdeps", false, "Build with revdeps")
buildCmd.Flags().Bool("all", false, "Build all specfiles in the tree") buildCmd.Flags().Bool("all", false, "Build all specfiles in the tree")
buildCmd.Flags().Bool("full", false, "Build all packages (optimized)") buildCmd.Flags().Bool("full", false, "Build all packages (optimized)")
buildCmd.Flags().String("values", "", "Build values file to interpolate with each package") buildCmd.Flags().StringSlice("values", []string{}, "Build values file to interpolate with each package")
buildCmd.Flags().StringSliceP("backend-args", "a", []string{}, "Backend args") buildCmd.Flags().StringSliceP("backend-args", "a", []string{}, "Backend args")
buildCmd.Flags().String("destination", filepath.Join(path, "build"), "Destination folder") buildCmd.Flags().String("destination", filepath.Join(path, "build"), "Destination folder")

View File

@@ -854,13 +854,7 @@ func (cs *LuetCompiler) compile(concurrency int, keepPermissions bool, p Compila
type templatedata map[string]interface{} type templatedata map[string]interface{}
// FromPackage returns a compilation spec from a package definition func (cs *LuetCompiler) templatePackage(pack pkg.Package) ([]byte, error) {
func (cs *LuetCompiler) FromPackage(p pkg.Package) (CompilationSpec, error) {
pack, err := cs.Database.FindPackageCandidate(p)
if err != nil {
return nil, err
}
var dataresult []byte var dataresult []byte
@@ -877,36 +871,49 @@ func (cs *LuetCompiler) FromPackage(p pkg.Package) (CompilationSpec, error) {
if err != nil { if err != nil {
return nil, errors.Wrap(err, "rendering file "+val) return nil, errors.Wrap(err, "rendering file "+val)
} }
packsRaw, err := pkg.GetRawPackages(data) packsRaw, err := pkg.GetRawPackages(data)
if err != nil {
return nil, errors.Wrap(err, "getting raw packages")
}
raw := packsRaw.Find(pack.GetName(), pack.GetCategory(), pack.GetVersion()) raw := packsRaw.Find(pack.GetName(), pack.GetCategory(), pack.GetVersion())
d := map[string]interface{}{} dst, err := helpers.UnMarshalValues(cs.Options.BuildValuesFile)
if len(cs.Options.BuildValuesFile) > 0 { if err != nil {
defBuild, err := ioutil.ReadFile(cs.Options.BuildValuesFile) return nil, errors.Wrap(err, "unmarshalling values")
if err != nil {
return nil, errors.Wrap(err, "rendering file "+val)
}
err = yaml.Unmarshal(defBuild, &d)
if err != nil {
return nil, errors.Wrap(err, "rendering file "+val)
}
} }
dat, err := helpers.RenderHelm(string(dataBuild), raw, d) dat, err := helpers.RenderHelm(string(dataBuild), raw, dst)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "rendering file "+pack.Rel(BuildFile)) return nil, errors.Wrap(err, "rendering file "+pack.Rel(BuildFile))
} }
dataresult = []byte(dat) dataresult = []byte(dat)
} else { } else {
out, err := helpers.RenderFiles(pack.Rel(BuildFile), val, cs.Options.BuildValuesFile) out, err := helpers.RenderFiles(pack.Rel(BuildFile), val, cs.Options.BuildValuesFile...)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "rendering file "+pack.Rel(BuildFile)) return nil, errors.Wrap(err, "rendering file "+pack.Rel(BuildFile))
} }
dataresult = []byte(out) dataresult = []byte(out)
} }
return dataresult, nil
return NewLuetCompilationSpec(dataresult, pack) }
// FromPackage returns a compilation spec from a package definition
func (cs *LuetCompiler) FromPackage(p pkg.Package) (CompilationSpec, error) {
pack, err := cs.Database.FindPackageCandidate(p)
if err != nil {
return nil, err
}
bytes, err := cs.templatePackage(pack)
if err != nil {
return nil, errors.Wrap(err, "while rendering package template")
}
return NewLuetCompilationSpec(bytes, pack)
} }
// GetBackend returns the current compilation backend // GetBackend returns the current compilation backend

View File

@@ -2,7 +2,9 @@ package helpers
import ( import (
"io/ioutil" "io/ioutil"
"sort"
"github.com/imdario/mergo"
"github.com/pkg/errors" "github.com/pkg/errors"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chart"
@@ -37,7 +39,31 @@ func RenderHelm(template string, values, d map[string]interface{}) (string, erro
type templatedata map[string]interface{} type templatedata map[string]interface{}
func RenderFiles(toTemplate, valuesFile string, defaultFile string) (string, error) { func UnMarshalValues(values []string) (templatedata, error) {
dst := templatedata{}
if len(values) > 0 {
allbv := values
sort.Sort(sort.Reverse(sort.StringSlice(allbv)))
for _, bv := range allbv {
current := map[string]interface{}{}
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 RenderFiles(toTemplate, valuesFile string, defaultFile ...string) (string, error) {
raw, err := ioutil.ReadFile(toTemplate) raw, err := ioutil.ReadFile(toTemplate)
if err != nil { if err != nil {
return "", errors.Wrap(err, "reading file "+toTemplate) return "", errors.Wrap(err, "reading file "+toTemplate)
@@ -52,20 +78,14 @@ func RenderFiles(toTemplate, valuesFile string, defaultFile string) (string, err
} }
var values templatedata var values templatedata
d := templatedata{}
if len(defaultFile) > 0 {
def, err := ioutil.ReadFile(defaultFile)
if err != nil {
return "", errors.Wrap(err, "reading file "+valuesFile)
}
if err = yaml.Unmarshal(def, &d); err != nil {
return "", errors.Wrap(err, "unmarshalling file "+toTemplate)
}
}
if err = yaml.Unmarshal(val, &values); err != nil { if err = yaml.Unmarshal(val, &values); err != nil {
return "", errors.Wrap(err, "unmarshalling file "+toTemplate) return "", errors.Wrap(err, "unmarshalling file "+toTemplate)
} }
return RenderHelm(string(raw), values, d) dst, err := UnMarshalValues(defaultFile)
if err != nil {
return "", errors.Wrap(err, "unmarshalling values")
}
return RenderHelm(string(raw), values, dst)
} }