diff --git a/pkg/helmexec/exec.go b/pkg/helmexec/exec.go index 6bc3fa7c..ddef397c 100644 --- a/pkg/helmexec/exec.go +++ b/pkg/helmexec/exec.go @@ -650,7 +650,7 @@ func (helm *execer) TemplateRelease(name string, chart string, flags ...string) var hasPostRenderer bool for _, f := range flags { - if strings.HasPrefix("--output-dir", f) { + if f == "--output-dir" || strings.HasPrefix(f, "--output-dir=") { outputToFile = true } if f == "--post-renderer" { @@ -678,13 +678,28 @@ func (helm *execer) TemplateRelease(name string, chart string, flags ...string) filteredFlags = append(filteredFlags, flags[i]) } + if outputDir == "" { + return fmt.Errorf("output dir not found for template command") + } + out, err := helm.exec(append(args, filteredFlags...), map[string]string{}, nil) if err != nil { return err } + templatesDir := filepath.Join(outputDir, "templates") + legacyOutputPath := filepath.Join(outputDir, name+".yaml") + + if removeErr := os.Remove(legacyOutputPath); removeErr != nil && !os.IsNotExist(removeErr) { + return removeErr + } + if len(out) > 0 { - outputPath := filepath.Join(outputDir, name+".yaml") + if mkdirErr := os.MkdirAll(templatesDir, 0755); mkdirErr != nil { + return mkdirErr + } + + outputPath := filepath.Join(templatesDir, name+".yaml") if writeErr := os.WriteFile(outputPath, append(out, '\n'), 0644); writeErr != nil { return writeErr } diff --git a/pkg/helmexec/exec_test.go b/pkg/helmexec/exec_test.go index f399cf34..760315a8 100644 --- a/pkg/helmexec/exec_test.go +++ b/pkg/helmexec/exec_test.go @@ -1260,12 +1260,14 @@ func Test_Template_PostRendererWithOutputDir(t *testing.T) { var buffer bytes.Buffer logger := NewLogger(&buffer, "debug") - runner := &mockRunner{output: []byte("apiVersion: v1\nkind: Namespace\n")} + runner := &mockRunner{} helm, err := New("helm", HelmExecOptions{}, logger, "config", "dev", runner) if err != nil { t.Fatalf("unexpected error: %v", err) } + runner.output = []byte("apiVersion: v1\nkind: Namespace\n") + err = helm.TemplateRelease("myrelease", "path/to/chart", "--post-renderer", "/bin/echo", "--output-dir", tmpDir, @@ -1275,7 +1277,7 @@ func Test_Template_PostRendererWithOutputDir(t *testing.T) { t.Fatalf("unexpected error: %v", err) } - outputPath := filepath.Join(tmpDir, "myrelease.yaml") + outputPath := filepath.Join(tmpDir, "templates", "myrelease.yaml") data, err := os.ReadFile(outputPath) if err != nil { t.Fatalf("expected output file %s to exist: %v", outputPath, err) diff --git a/test/integration/test-cases/issue-2515.sh b/test/integration/test-cases/issue-2515.sh index 43c8ea7c..5ec9eac4 100644 --- a/test/integration/test-cases/issue-2515.sh +++ b/test/integration/test-cases/issue-2515.sh @@ -17,9 +17,14 @@ else --output-dir-template "${issue_2515_output_dir}/{{.Release.Name}}" \ &> ${issue_2515_tmp}/log || fail "helmfile template should not fail" - issue_2515_output_file="${issue_2515_output_dir}/issue-2515/issue-2515.yaml" - if [ ! -f "${issue_2515_output_file}" ]; then - fail "Expected output file ${issue_2515_output_file} to exist" + issue_2515_templates_dir="${issue_2515_output_dir}/issue-2515/templates" + if [ ! -d "${issue_2515_templates_dir}" ]; then + fail "Expected templates directory ${issue_2515_templates_dir} to exist" + fi + + issue_2515_output_file=$(find "${issue_2515_templates_dir}" -type f \( -name '*.yaml' -o -name '*.yml' \) | head -n 1) + if [ -z "${issue_2515_output_file}" ]; then + fail "Expected rendered YAML file under ${issue_2515_templates_dir}" fi if grep -q "original-cm" "${issue_2515_output_file}"; then