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

config: refactored hydratefiles in prevision of ytt

parent d82bb7691b85
No related branches found
No related tags found
No related merge requests found
...@@ -13,7 +13,8 @@ ...@@ -13,7 +13,8 @@
func RunCMD(name string, args ...string) (err error, stdout, stderr []string) { func RunCMD(name string, args ...string) (err error, stdout, stderr []string) {
// helm template -f base.yaml -f base.values.yaml -f ns.yaml -f ns.values.yaml // helm template -f base.yaml -f base.values.yaml -f ns.yaml -f ns.values.yaml
// ytt -f /chart-folder -f base.yaml -f ns.yaml -v ... -v ... // ytt -f $chartsTmpFile --file-mark "$(basename $chartsTmpFile):type=yaml-plain"\
// -f base/ytt/ -f base/ytt.yml -f ns1/ytt/ -f ns1/ytt.yml
c := cmd.NewCmd(name, args...) c := cmd.NewCmd(name, args...)
statusChan := c.Start() statusChan := c.Start()
status := <-statusChan status := <-statusChan
...@@ -134,9 +135,18 @@ ...@@ -134,9 +135,18 @@
return charts return charts
} }
func (c *CmdChart) hydrateFiles(dirName string, variables map[string]string) ([]string, error) { func hydrateFiles(dirName string, variables map[string]string, paths []string) ([]string, error) {
var hydratedFiles []string var result []string
for _, file := range c.ValuesFileNames { for _, path := range paths {
if tmpl, err := template.New(filepath.Base(file)).ParseFiles(file); err != nil { fileInfo, err := os.Stat(path)
if err != nil {
return nil, err
}
if fileInfo.IsDir() {
result = append(result, path)
continue
}
if tmpl, err := template.New(filepath.Base(path)).ParseFiles(path); err != nil {
return nil, err return nil, err
} else { } else {
...@@ -141,6 +151,6 @@ ...@@ -141,6 +151,6 @@
return nil, err return nil, err
} else { } else {
if tmpFile, err := ioutil.TempFile(dirName, fmt.Sprintf("%s-", filepath.Base(file))); err != nil { if tmpFile, err := ioutil.TempFile(dirName, fmt.Sprintf("%s-", filepath.Base(path))); err != nil {
return nil, fmt.Errorf("hydrateFiles failed to create tempfile: %w", err) return nil, fmt.Errorf("hydrateFiles failed to create tempfile: %w", err)
} else { } else {
defer func() { defer func() {
...@@ -149,7 +159,7 @@ ...@@ -149,7 +159,7 @@
if err := tmpl.Execute(tmpFile, variables); err != nil { if err := tmpl.Execute(tmpFile, variables); err != nil {
return nil, fmt.Errorf("hydrateFiles failed to execute template: %w", err) return nil, fmt.Errorf("hydrateFiles failed to execute template: %w", err)
} }
hydratedFiles = append(hydratedFiles, tmpFile.Name()) result = append(result, tmpFile.Name())
} }
} }
} }
...@@ -153,10 +163,10 @@ ...@@ -153,10 +163,10 @@
} }
} }
} }
return hydratedFiles, nil return result, nil
} }
func (c *CmdConfig) hydrateFiles(dirName string) error { func (c *CmdConfig) hydrateFiles(dirName string) error {
variables := c.prepareVariables(c.Spec.Variables) variables := c.prepareVariables(c.Spec.Variables)
for key, chart := range c.Spec.Charts { for key, chart := range c.Spec.Charts {
...@@ -157,9 +167,9 @@ ...@@ -157,9 +167,9 @@
} }
func (c *CmdConfig) hydrateFiles(dirName string) error { func (c *CmdConfig) hydrateFiles(dirName string) error {
variables := c.prepareVariables(c.Spec.Variables) variables := c.prepareVariables(c.Spec.Variables)
for key, chart := range c.Spec.Charts { for key, chart := range c.Spec.Charts {
if files, err := chart.hydrateFiles(dirName, variables); err != nil { if paths, err := hydrateFiles(dirName, variables, chart.ValuesFileNames); err != nil {
return err return err
} else { } else {
...@@ -164,6 +174,6 @@ ...@@ -164,6 +174,6 @@
return err return err
} else { } else {
chart.ValuesFileNames = files chart.ValuesFileNames = paths
c.Spec.Charts[key] = chart c.Spec.Charts[key] = chart
} }
} }
......
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