feat: support .Environment.* in --output-dir-template

This commit adds support for accessing environment values in the --output-dir-template flag.

Previously, users could only access .OutputDir, .State.*, and .Release.* in the template.
Now .Environment.* is also available, allowing users to use environment values in the
output directory path.

Example usage:
  helmfile template -e test-1 --output-dir-template='{{ .OutputDir }}/{{ .Environment.cluster.name }}/{{ .Environment.Name }}/{{ .Release.Name }}'

This produces output like: ./gitops/my-test-cluster/test-1/release-name/

Changes:
- Add Environment field to GenerateOutputDir template data
- Add Environment field to generateChartPath template data (now a method on HelmState)
- Update help text for --output-dir-template flag in template and fetch commands
- Add test cases for Environment in template

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2026-01-23 08:51:32 +08:00
parent 7500eef7c6
commit ee65b88edf
4 changed files with 52 additions and 18 deletions

View File

@ -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
}

View File

@ -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")

View File

@ -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}

View File

@ -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)