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