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

move hydration code to CmdConfig and add variable merging

parent dba164c9d649
No related branches found
No related tags found
No related merge requests found
package runner
import (
"bytes"
"fmt"
"os"
"path/filepath"
......@@ -4,5 +6,6 @@
"os"
"path/filepath"
"text/template"
"github.com/go-cmd/cmd"
"github.com/rs/zerolog"
......@@ -6,6 +9,7 @@
"github.com/go-cmd/cmd"
"github.com/rs/zerolog"
"gopkg.in/yaml.v2"
)
func RunCMD(name string, args ...string) (err error, stdout, stderr []string) {
......@@ -71,3 +75,65 @@
Files []string
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
import (
"bytes"
"errors"
"fmt"
"os"
"text/template"
"github.com/spf13/viper"
......@@ -10,5 +4,4 @@
"github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"
)
// Variable ...
......@@ -70,13 +63,9 @@
return cfg, nil
}
func checkExists(path string) bool {
_, err := os.Stat(path)
return !errors.Is(err, os.ErrNotExist)
}
// hydrate expands templated variables in our config with concrete values
func (c *Config) hydrate() error {
if err := c.hydrateHelmCharts(); err != nil {
return err
// MergeVariables takes another config and will import those variables into
// the current config by replacing old ones and adding the new ones
func (c *Config) MergeVariables(other *Config) {
for _, variable := range other.Spec.Variables {
c.overlayVariable(variable)
}
......@@ -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 @@
}
func (c *Config) 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()
// overlayVariable takes a variable in and either replaces an existing variable
// of the same name or create a new variable in the config if no matching name
// is found
func (c *Config) overlayVariable(v Variable) {
// find same variable by name and replace is value
// if not found then create the variable
for index, originalVariable := range c.Spec.Variables {
if originalVariable.Name == v.Name {
c.Spec.Variables[index].Value = v.Value
return
}
}
......@@ -111,4 +84,4 @@
}
}
return nil
c.Spec.Variables = append(c.Spec.Variables, v)
}
......@@ -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 @@
// verify variables overwrite
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