Skip to content
Snippets Groups Projects
Commit c4f0f586 authored by Florent Aide's avatar Florent Aide
Browse files

move hydration code to CmdConfig and add variable merging

parent dba164c9
No related branches found
No related tags found
No related merge requests found
package runner package runner
import ( import (
"bytes"
"fmt"
"os" "os"
"path/filepath" "path/filepath"
...@@ -4,5 +6,6 @@ ...@@ -4,5 +6,6 @@
"os" "os"
"path/filepath" "path/filepath"
"text/template"
"github.com/go-cmd/cmd" "github.com/go-cmd/cmd"
"github.com/rs/zerolog" "github.com/rs/zerolog"
...@@ -6,6 +9,7 @@ ...@@ -6,6 +9,7 @@
"github.com/go-cmd/cmd" "github.com/go-cmd/cmd"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"gopkg.in/yaml.v2"
) )
func RunCMD(name string, args ...string) (err error, stdout, stderr []string) { func RunCMD(name string, args ...string) (err error, stdout, stderr []string) {
...@@ -71,3 +75,65 @@ ...@@ -71,3 +75,65 @@
Files []string Files []string
Values []Value Values []Value
} }
// hydrate expands templated variables in our config with concrete values
func (c *CmdConfig) hydrate() error {
if err := c.hydrateHelmCharts(); err != nil {
return err
}
if err := c.hydrateYttCharts(); err != nil {
return err
}
return nil
}
func (c *CmdConfig) prepareVariables(v []Variable) map[string]string {
variables := make(map[string]string)
for _, variable := range v {
variables[variable.Name] = variable.Value
}
variables["namespace"] = c.Namespace
return variables
}
func (c *CmdConfig) hydrateYttCharts() error {
for entryFileName, entry := range c.Spec.Charts.Ytt {
for valIndex, val := range entry.Values {
valueTmpl, err := template.New("ytt entry value").Parse(val.Value)
if err != nil {
return fmt.Errorf("failed to parse ytt entry value as template: %q, %w", val.Value, err)
}
buf := new(bytes.Buffer)
if err := valueTmpl.Execute(buf, c.prepareVariables(c.Spec.Variables)); err != nil {
return fmt.Errorf("failed to hydrate ytt entry: %q, %w", val.Value, err)
}
// replace original content with hydrated version
c.Spec.Charts.Ytt[entryFileName].Values[valIndex].Value = buf.String()
}
}
return nil
}
func (c *CmdConfig) hydrateHelmCharts() error {
for name, chart := range c.Spec.Charts.Helm {
var newVals []string
for _, value := range chart.Values {
rawChartValue, err := yaml.Marshal(value)
if err != nil {
return fmt.Errorf("failed to get chart values as string: %w", err)
}
valueTmpl, err := template.New("chart").Parse(string(rawChartValue))
if err != nil {
return fmt.Errorf("failed to parse chart values as template: %q, %w", chart.Values, err)
}
buf := new(bytes.Buffer)
if err := valueTmpl.Execute(buf, c.prepareVariables(c.Spec.Variables)); err != nil {
return fmt.Errorf("failed to hydrate chart values entry: %q, %w", chart.Values, err)
}
newVals = append(newVals, buf.String())
}
chart.Values = newVals
c.Spec.Charts.Helm[name] = chart
}
return nil
}
package runner package runner
import ( import (
"bytes"
"errors"
"fmt"
"os"
"text/template"
"github.com/spf13/viper" "github.com/spf13/viper"
...@@ -10,5 +4,4 @@ ...@@ -10,5 +4,4 @@
"github.com/spf13/viper" "github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"
) )
// Variable ... // Variable ...
...@@ -70,13 +63,9 @@ ...@@ -70,13 +63,9 @@
return cfg, nil return cfg, nil
} }
func checkExists(path string) bool { // MergeVariables takes another config and will import those variables into
_, err := os.Stat(path) // the current config by replacing old ones and adding the new ones
return !errors.Is(err, os.ErrNotExist) func (c *Config) MergeVariables(other *Config) {
} for _, variable := range other.Spec.Variables {
c.overlayVariable(variable)
// hydrate expands templated variables in our config with concrete values
func (c *Config) hydrate() error {
if err := c.hydrateHelmCharts(); err != nil {
return err
} }
...@@ -82,16 +71,3 @@ ...@@ -82,16 +71,3 @@
} }
if err := c.hydrateYttCharts(); err != nil {
return err
}
return nil
}
func (c *Config) prepareVariables(v []Variable) map[string]string {
variables := make(map[string]string)
for _, variable := range v {
variables[variable.Name] = variable.Value
}
variables["namespace"] = c.Namespace
return variables
} }
...@@ -96,17 +72,14 @@ ...@@ -96,17 +72,14 @@
} }
func (c *Config) hydrateYttCharts() error { // overlayVariable takes a variable in and either replaces an existing variable
for entryFileName, entry := range c.Spec.Charts.Ytt { // of the same name or create a new variable in the config if no matching name
for valIndex, val := range entry.Values { // is found
valueTmpl, err := template.New("ytt entry value").Parse(val.Value) func (c *Config) overlayVariable(v Variable) {
if err != nil { // find same variable by name and replace is value
return fmt.Errorf("failed to parse ytt entry value as template: %q, %w", val.Value, err) // if not found then create the variable
} for index, originalVariable := range c.Spec.Variables {
buf := new(bytes.Buffer) if originalVariable.Name == v.Name {
if err := valueTmpl.Execute(buf, c.prepareVariables(c.Spec.Variables)); err != nil { c.Spec.Variables[index].Value = v.Value
return fmt.Errorf("failed to hydrate ytt entry: %q, %w", val.Value, err) return
}
// replace original content with hydrated version
c.Spec.Charts.Ytt[entryFileName].Values[valIndex].Value = buf.String()
} }
} }
...@@ -111,4 +84,4 @@ ...@@ -111,4 +84,4 @@
} }
} }
return nil c.Spec.Variables = append(c.Spec.Variables, v)
} }
...@@ -114,23 +87,1 @@ ...@@ -114,23 +87,1 @@
} }
func (c *Config) hydrateHelmCharts() error {
for name, chart := range c.Spec.Charts.Helm {
rawChartValues, err := yaml.Marshal(chart.Values)
if err != nil {
return fmt.Errorf("failed to get chart values as string: %w", err)
}
valueTmpl, err := template.New("chart").Parse(string(rawChartValues))
if err != nil {
return fmt.Errorf("failed to parse chart values as template: %q, %w", chart.Values, err)
}
buf := new(bytes.Buffer)
if err := valueTmpl.Execute(buf, c.prepareVariables(c.Spec.Variables)); err != nil {
return fmt.Errorf("failed to hydrate chart values entry: %q, %w", chart.Values, err)
}
// replace original content with hydrated version
hydratedChart := chart
hydratedChart.Values = buf.String()
c.Spec.Charts.Helm[name] = hydratedChart
}
return nil
}
...@@ -37,3 +37,4 @@ ...@@ -37,3 +37,4 @@
// verify variables overwrite // verify variables overwrite
assert.Equal(t, "admin", config.Spec.Charts.Ytt["odoo"].Values[2].Value) assert.Equal(t, "admin", config.Spec.Charts.Ytt["odoo"].Values[2].Value)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment