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

runner: cleanup the mess and make all tests pass again :p

parent a2f65e81b262
No related branches found
No related tags found
No related merge requests found
...@@ -27,9 +27,10 @@ ...@@ -27,9 +27,10 @@
func NewCmdConfig(logger zerolog.Logger, configDir string, namespace string) (*CmdConfig, error) { func NewCmdConfig(logger zerolog.Logger, configDir string, namespace string) (*CmdConfig, error) {
cmdConfig := &CmdConfig{} cmdConfig := &CmdConfig{}
cmdConfig.RootDir = configDir
cmdConfig.Spec.Charts.Helm = make(map[string]CmdChart) cmdConfig.Spec.Charts.Helm = make(map[string]CmdChart)
cmdConfig.Spec.Charts.Ytt = make(map[string]CmdChart) cmdConfig.Spec.Charts.Ytt = make(map[string]CmdChart)
cmdConfig.Namespace = namespace cmdConfig.Namespace = namespace
cmdConfig.Logger = logger cmdConfig.Logger = logger
baseCfg, err := NewConfig(configDir) baseCfg, err := NewConfig(configDir)
...@@ -30,10 +31,9 @@ ...@@ -30,10 +31,9 @@
cmdConfig.Spec.Charts.Helm = make(map[string]CmdChart) cmdConfig.Spec.Charts.Helm = make(map[string]CmdChart)
cmdConfig.Spec.Charts.Ytt = make(map[string]CmdChart) cmdConfig.Spec.Charts.Ytt = make(map[string]CmdChart)
cmdConfig.Namespace = namespace cmdConfig.Namespace = namespace
cmdConfig.Logger = logger cmdConfig.Logger = logger
baseCfg, err := NewConfig(configDir) baseCfg, err := NewConfig(configDir)
fmt.Printf(">>> %+v\n", baseCfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -49,9 +49,14 @@ ...@@ -49,9 +49,14 @@
// then merge in all variables from the nsCfg // then merge in all variables from the nsCfg
cmdConfig.MergeVariables(nsCfg) cmdConfig.MergeVariables(nsCfg)
// TODO: merge baseCfg & nsCfg charts into cmdConfig for name, c := range baseCfg.Spec.Charts.Helm {
cmdConfig.Spec.Charts.Helm[name] = NewCmdChartFromChart(c)
}
for name, c := range nsCfg.Spec.Charts.Helm {
cmdConfig.Spec.Charts.Helm[name] = NewCmdChartFromChart(c)
}
cmdConfig.populate() cmdConfig.populate()
tmpDir, err := os.MkdirTemp(os.TempDir(), "beaver-") tmpDir, err := os.MkdirTemp(os.TempDir(), "beaver-")
if err != nil { if err != nil {
...@@ -53,9 +58,9 @@ ...@@ -53,9 +58,9 @@
cmdConfig.populate() cmdConfig.populate()
tmpDir, err := os.MkdirTemp(os.TempDir(), "beaver-") tmpDir, err := os.MkdirTemp(os.TempDir(), "beaver-")
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to create temp dir: %w", err)
} }
// - hydrate // - hydrate
...@@ -68,6 +73,7 @@ ...@@ -68,6 +73,7 @@
type CmdConfig struct { type CmdConfig struct {
Spec CmdSpec Spec CmdSpec
RootDir string
Namespace string Namespace string
Logger zerolog.Logger Logger zerolog.Logger
} }
...@@ -87,6 +93,13 @@ ...@@ -87,6 +93,13 @@
Files []string Files []string
} }
func NewCmdChartFromChart(c Chart) CmdChart {
return CmdChart{
Name: c.Path,
Files: nil,
}
}
// hydrate expands templated variables in our config with concrete values // hydrate expands templated variables in our config with concrete values
func (c *CmdConfig) hydrate(dirName string) error { func (c *CmdConfig) hydrate(dirName string) error {
if err := c.hydrateFiles(dirName); err != nil { if err := c.hydrateFiles(dirName); err != nil {
...@@ -105,7 +118,7 @@ ...@@ -105,7 +118,7 @@
} }
func (c *CmdConfig) populate() { func (c *CmdConfig) populate() {
c.Spec.Charts.Helm = findFiles(c.Namespace, c.Spec.Charts.Helm) c.Spec.Charts.Helm = findFiles(c.RootDir, c.Namespace, c.Spec.Charts.Helm)
c.Spec.Charts.Ytt = findFiles(c.Namespace, c.Spec.Charts.Ytt) c.Spec.Charts.Ytt = findFiles(c.RootDir, c.Namespace, c.Spec.Charts.Ytt)
} }
...@@ -110,6 +123,4 @@ ...@@ -110,6 +123,4 @@
} }
func findFiles(namespace string, charts map[string]CmdChart) map[string]CmdChart { func findFiles(rootdir, namespace string, charts map[string]CmdChart) map[string]CmdChart {
var fpath string
var files []string
for name, chart := range charts { for name, chart := range charts {
...@@ -115,3 +126,4 @@ ...@@ -115,3 +126,4 @@
for name, chart := range charts { for name, chart := range charts {
var files []string
for _, folder := range []string{"base", filepath.Join("environments", namespace)} { for _, folder := range []string{"base", filepath.Join("environments", namespace)} {
for _, ext := range []string{"yaml", "yml"} { for _, ext := range []string{"yaml", "yml"} {
...@@ -116,5 +128,5 @@ ...@@ -116,5 +128,5 @@
for _, folder := range []string{"base", filepath.Join("environments", namespace)} { for _, folder := range []string{"base", filepath.Join("environments", namespace)} {
for _, ext := range []string{"yaml", "yml"} { for _, ext := range []string{"yaml", "yml"} {
fpath = filepath.Join(folder, fmt.Sprintf("%s.%s", name, ext)) fpath := filepath.Join(rootdir, folder, fmt.Sprintf("%s.%s", name, ext))
if _, err := os.Stat(fpath); err == nil { if _, err := os.Stat(fpath); err == nil {
files = append(files, fpath) files = append(files, fpath)
...@@ -119,5 +131,3 @@ ...@@ -119,5 +131,3 @@
if _, err := os.Stat(fpath); err == nil { if _, err := os.Stat(fpath); err == nil {
files = append(files, fpath) files = append(files, fpath)
chart.Files = append(chart.Files, fpath)
charts[name] = chart
} }
...@@ -123,4 +133,3 @@ ...@@ -123,4 +133,3 @@
} }
} }
} }
...@@ -125,5 +134,7 @@ ...@@ -125,5 +134,7 @@
} }
} }
chart.Files = append(chart.Files, files...)
charts[name] = chart
} }
return charts return charts
} }
...@@ -131,6 +142,6 @@ ...@@ -131,6 +142,6 @@
func (c *CmdChart) hydrateFiles(dirName string, variables map[string]string) ([]string, error) { func (c *CmdChart) hydrateFiles(dirName string, variables map[string]string) ([]string, error) {
var hydratedFiles []string var hydratedFiles []string
for _, file := range c.Files { for _, file := range c.Files {
if tmpl, err := template.New(file).ParseFiles(file); err != nil { if tmpl, err := template.New(filepath.Base(file)).ParseFiles(file); err != nil {
return nil, err return nil, err
} else { } else {
...@@ -135,5 +146,5 @@ ...@@ -135,5 +146,5 @@
return nil, err return nil, err
} else { } else {
if tmpFile, err := ioutil.TempFile(dirName, fmt.Sprintf("%s-", file)); err != nil { if tmpFile, err := ioutil.TempFile(dirName, fmt.Sprintf("%s-", filepath.Base(file))); err != nil {
return nil, err return nil, fmt.Errorf("hydrateFiles failed to create tempfile: %w", err)
} else { } else {
...@@ -139,2 +150,5 @@ ...@@ -139,2 +150,5 @@
} else { } else {
defer func() {
_ = tmpFile.Close()
}()
if err := tmpl.Execute(tmpFile, variables); err != nil { if err := tmpl.Execute(tmpFile, variables); err != nil {
...@@ -140,5 +154,5 @@ ...@@ -140,5 +154,5 @@
if err := tmpl.Execute(tmpFile, variables); err != nil { if err := tmpl.Execute(tmpFile, variables); err != nil {
return nil, err return nil, fmt.Errorf("hydrateFiles failed to execute template: %w", err)
} }
hydratedFiles = append(hydratedFiles, tmpFile.Name()) hydratedFiles = append(hydratedFiles, tmpFile.Name())
} }
...@@ -158,7 +172,6 @@ ...@@ -158,7 +172,6 @@
c.Spec.Charts.Helm[key] = helmChart c.Spec.Charts.Helm[key] = helmChart
} }
} }
// FIXME: use generic to avoid repetition
for key, yttChart := range c.Spec.Charts.Ytt { for key, yttChart := range c.Spec.Charts.Ytt {
if files, err := yttChart.hydrateFiles(dirName, variables); err != nil { if files, err := yttChart.hydrateFiles(dirName, variables); err != nil {
return err return err
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import ( import (
"fmt" "fmt"
"path/filepath"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
...@@ -24,7 +25,9 @@ ...@@ -24,7 +25,9 @@
func TestCmdConfig(t *testing.T) { func TestCmdConfig(t *testing.T) {
tl := testutils.NewTestLogger(t) tl := testutils.NewTestLogger(t)
testNS := "ns1" testNS := "ns1"
c, err := NewCmdConfig(tl.Logger(), "fixtures/", testNS) absConfigDir, err := filepath.Abs("fixtures/")
require.NoError(t, err)
c, err := NewCmdConfig(tl.Logger(), absConfigDir, testNS)
require.NoError(t, err) require.NoError(t, err)
pgHelmChart, ok := c.Spec.Charts.Helm["postgres"] pgHelmChart, ok := c.Spec.Charts.Helm["postgres"]
...@@ -44,3 +47,7 @@ ...@@ -44,3 +47,7 @@
) )
*/ */
} }
func TestFindFiles(t *testing.T) {
}
...@@ -13,5 +13,5 @@ ...@@ -13,5 +13,5 @@
require.NoError(t, err) require.NoError(t, err)
// first config.spec.variables entry name should be VAULT_KV in our test file // first config.spec.variables entry name should be VAULT_KV in our test file
assert.Equal(t, "VAULT_KV", config.Spec.Variables[0].Name) assert.Equal(t, "VAULT_KV", config.Spec.Variables[0].Name)
assert.Equal(t, "k8s.orus.io", config.Spec.Variables[0].Value) assert.Equal(t, "orus.io", config.Spec.Variables[0].Value)
assert.Equal(t, "vendor/helm/postgresql", config.Spec.Charts.Helm["postgres"].Path) assert.Equal(t, "vendor/helm/postgresql", config.Spec.Charts.Helm["postgres"].Path)
...@@ -17,3 +17,3 @@ ...@@ -17,3 +17,3 @@
assert.Equal(t, "vendor/helm/postgresql", config.Spec.Charts.Helm["postgres"].Path) assert.Equal(t, "vendor/helm/postgresql", config.Spec.Charts.Helm["postgres"].Path)
assert.Equal(t, "vendor/ytt/odoo", config.Spec.Charts.Helm["odoo"].Path) assert.Equal(t, "vendor/ytt/odoo", config.Spec.Charts.Ytt["odoo"].Path)
} }
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