diff --git a/cmd/fetch.go b/cmd/fetch.go index d4c5591a..ef41f308 100644 --- a/cmd/fetch.go +++ b/cmd/fetch.go @@ -34,7 +34,7 @@ func NewFetchCmd(globalCfg *config.GlobalImpl) *cobra.Command { f := cmd.Flags() f.IntVar(&fetchOptions.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited") f.StringVar(&fetchOptions.OutputDir, "output-dir", "", "directory to store charts (default: temporary directory which is deleted when the command terminates)") - f.StringVar(&fetchOptions.OutputDirTemplate, "output-dir-template", state.DefaultFetchOutputDirTemplate, "go text template for generating the output directory") + f.StringVar(&fetchOptions.OutputDirTemplate, "output-dir-template", state.DefaultFetchOutputDirTemplate, "go text template for generating the output directory. Available fields: {{ .OutputDir }}, {{ .ChartName }}, {{ .Release.* }}, {{ .Environment.* }}") return cmd } diff --git a/cmd/template.go b/cmd/template.go index 8713f5b3..a69506d8 100644 --- a/cmd/template.go +++ b/cmd/template.go @@ -35,7 +35,7 @@ func NewTemplateCmd(globalCfg *config.GlobalImpl) *cobra.Command { f.StringArrayVar(&templateOptions.Set, "set", nil, "additional values to be merged into the helm command --set flag") f.StringArrayVar(&templateOptions.Values, "values", nil, "additional value files to be merged into the helm command --values flag") f.StringVar(&templateOptions.OutputDir, "output-dir", "", "output directory to pass to helm template (helm template --output-dir)") - f.StringVar(&templateOptions.OutputDirTemplate, "output-dir-template", "", "go text template for generating the output directory. Default: {{ .OutputDir }}/{{ .State.BaseName }}-{{ .State.AbsPathSHA1 }}-{{ .Release.Name}}") + f.StringVar(&templateOptions.OutputDirTemplate, "output-dir-template", "", "go text template for generating the output directory. Available fields: {{ .OutputDir }}, {{ .State.* }}, {{ .Release.* }}, {{ .Environment.* }}. Default: {{ .OutputDir }}/{{ .State.BaseName }}-{{ .State.AbsPathSHA1 }}-{{ .Release.Name}}") f.IntVar(&templateOptions.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited") f.BoolVar(&templateOptions.Validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. Note that this requires access to a Kubernetes cluster to obtain information necessary for validating, like the template of available API versions") f.BoolVar(&templateOptions.IncludeCRDs, "include-crds", false, "include CRDs in the templated output") diff --git a/pkg/state/state.go b/pkg/state/state.go index ae699477..48c4baef 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -1565,7 +1565,7 @@ func (st *HelmState) processLocalChart(normalizedChart, dir string, release *Rel if helmfileCommand == "pull" && isLocal { chartAbsPath := strings.TrimSuffix(filepath.Clean(normalizedChart), "/") var err error - chartPath, err = generateChartPath(filepath.Base(chartAbsPath), dir, release, opts.OutputDirTemplate) + chartPath, err = st.generateChartPath(filepath.Base(chartAbsPath), dir, release, opts.OutputDirTemplate) if err != nil { return "", err } @@ -1589,7 +1589,7 @@ func (st *HelmState) forcedDownloadChart(chartName, dir string, release *Release return cachedPath, nil } - chartPath, err := generateChartPath(chartName, dir, release, opts.OutputDirTemplate) + chartPath, err := st.generateChartPath(chartName, dir, release, opts.OutputDirTemplate) if err != nil { return "", err } @@ -4308,9 +4308,10 @@ func (st *HelmState) GenerateOutputDir(outputDir string, release *ReleaseSpec, o } data := struct { - OutputDir string - State state - Release *ReleaseSpec + OutputDir string + State state + Release *ReleaseSpec + Environment environment.Environment }{ OutputDir: outputDir, State: state{ @@ -4319,7 +4320,8 @@ func (st *HelmState) GenerateOutputDir(outputDir string, release *ReleaseSpec, o AbsPath: stateAbsPath, AbsPathSHA1: sha1sum, }, - Release: release, + Release: release, + Environment: st.Env, } if err := t.Execute(buf, data); err != nil { @@ -4330,9 +4332,9 @@ func (st *HelmState) GenerateOutputDir(outputDir string, release *ReleaseSpec, o } // generateChartPath generates the path of the output directory of the `helmfile fetch` command. -// It uses a go template with data from the chart name, output directory and release spec. +// It uses a go template with data from the chart name, output directory, release spec, and environment. // If no template was provided (via the `--output-dir-template` flag) it uses the DefaultFetchOutputDirTemplate. -func generateChartPath(chartName string, outputDir string, release *ReleaseSpec, outputDirTemplate string) (string, error) { +func (st *HelmState) generateChartPath(chartName string, outputDir string, release *ReleaseSpec, outputDirTemplate string) (string, error) { if outputDirTemplate == "" { outputDirTemplate = DefaultFetchOutputDirTemplate } @@ -4344,13 +4346,15 @@ func generateChartPath(chartName string, outputDir string, release *ReleaseSpec, buf := &bytes.Buffer{} data := struct { - ChartName string - OutputDir string - Release ReleaseSpec + ChartName string + OutputDir string + Release ReleaseSpec + Environment environment.Environment }{ - ChartName: chartName, - OutputDir: outputDir, - Release: *release, + ChartName: chartName, + OutputDir: outputDir, + Release: *release, + Environment: st.Env, } if err := t.Execute(buf, data); err != nil { return "", fmt.Errorf("executing output-dir-template template: %w", err) @@ -4883,7 +4887,7 @@ func (st *HelmState) FullFilePath() (string, error) { func (st *HelmState) getOCIChartPath(tempDir string, release *ReleaseSpec, chartName, chartVersion, outputDirTemplate string) (string, error) { if outputDirTemplate != "" { - return generateChartPath(chartName, tempDir, release, outputDirTemplate) + return st.generateChartPath(chartName, tempDir, release, outputDirTemplate) } pathElems := []string{tempDir} diff --git a/pkg/state/state_test.go b/pkg/state/state_test.go index f9e8e642..10e3d314 100644 --- a/pkg/state/state_test.go +++ b/pkg/state/state_test.go @@ -3821,10 +3821,40 @@ func TestGenerateChartPath(t *testing.T) { outputDirTemplate: "{{ .OutputDir }", wantErr: true, }, + { + testName: "PathGeneratedWithGivenOutputDirTemplateWithEnvironmentName", + chartName: "chart-name", + release: &ReleaseSpec{Name: "release-name"}, + outputDir: "/output-dir", + outputDirTemplate: "{{ .OutputDir }}/{{ .Environment.Name }}", + wantErr: false, + expected: "/output-dir/test-env", + }, + { + testName: "PathGeneratedWithGivenOutputDirTemplateWithEnvironmentValues", + chartName: "chart-name", + release: &ReleaseSpec{Name: "release-name"}, + outputDir: "/output-dir", + outputDirTemplate: "{{ .OutputDir }}/{{ .Environment.Values.cluster.name }}", + wantErr: false, + expected: "/output-dir/my-test-cluster", + }, } for _, tt := range tests { t.Run(tt.testName, func(t *testing.T) { - got, err := generateChartPath(tt.chartName, tt.outputDir, tt.release, tt.outputDirTemplate) + st := &HelmState{ + ReleaseSetSpec: ReleaseSetSpec{ + Env: environment.Environment{ + Name: "test-env", + Values: map[string]any{ + "cluster": map[string]any{ + "name": "my-test-cluster", + }, + }, + }, + }, + } + got, err := st.generateChartPath(tt.chartName, tt.outputDir, tt.release, tt.outputDirTemplate) if tt.wantErr { require.Errorf(t, err, "GenerateChartPath() error \"%v\", want error", err)