# HG changeset patch # User Florent Aide <florent.aide@gmail.com> # Date 1652774528 -7200 # Tue May 17 10:02:08 2022 +0200 # Node ID 479ccd4c275448e63cf6b994ac80ff7422abd6e2 # Parent a2f65e81b2628e21dedc784d5dcbd6b1008a5084 runner: cleanup the mess and make all tests pass again :p diff --git a/runner/cmd.go b/runner/cmd.go --- a/runner/cmd.go +++ b/runner/cmd.go @@ -27,13 +27,13 @@ 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) - fmt.Printf(">>> %+v\n", baseCfg) if err != nil { return nil, err } @@ -49,13 +49,18 @@ // 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 { - 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,25 +118,23 @@ } 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) } -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 { + var files []string 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) - chart.Files = append(chart.Files, fpath) - charts[name] = chart } - } } + chart.Files = append(chart.Files, files...) + charts[name] = chart } return charts } @@ -131,14 +142,17 @@ 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 { - 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 { + defer func() { + _ = tmpFile.Close() + }() 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 diff --git a/runner/cmd_test.go b/runner/cmd_test.go --- a/runner/cmd_test.go +++ b/runner/cmd_test.go @@ -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) { + +} diff --git a/runner/config_test.go b/runner/config_test.go --- a/runner/config_test.go +++ b/runner/config_test.go @@ -13,7 +13,7 @@ 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) - assert.Equal(t, "vendor/ytt/odoo", config.Spec.Charts.Helm["odoo"].Path) + assert.Equal(t, "vendor/ytt/odoo", config.Spec.Charts.Ytt["odoo"].Path) }