feat: support .Environment.* in --output-dir-template (#2375)
* 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>
* fix: address PR review comments for --output-dir-template
- Clarify .Environment.Name, .Environment.KubeContext, .Environment.Values.* in help text
- Update generateChartPath comment to reflect broader usage (fetch, pull, OCI)
- Add tests for GenerateOutputDir with Environment fields
Signed-off-by: yxxhero <aiopsclub@163.com>
* fix: address additional PR review comments
- Move HelmState setup outside test loop to reduce duplication
- Document Environment field (.Name, .KubeContext, .Values) in template data structs
Signed-off-by: yxxhero <aiopsclub@163.com>
---------
Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
parent
58df057dcc
commit
503c397810
|
|
@ -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.Name }}, {{ .Environment.KubeContext }}, {{ .Environment.Values.* }}")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.Name }}, {{ .Environment.KubeContext }}, {{ .Environment.Values.* }}. 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")
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -4326,10 +4326,12 @@ func (st *HelmState) GenerateOutputDir(outputDir string, release *ReleaseSpec, o
|
|||
AbsPathSHA1 string
|
||||
}
|
||||
|
||||
// Template data for output-dir-template. Environment provides .Name, .KubeContext, and .Values fields.
|
||||
data := struct {
|
||||
OutputDir string
|
||||
State state
|
||||
Release *ReleaseSpec
|
||||
OutputDir string
|
||||
State state
|
||||
Release *ReleaseSpec
|
||||
Environment environment.Environment
|
||||
}{
|
||||
OutputDir: outputDir,
|
||||
State: state{
|
||||
|
|
@ -4338,7 +4340,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 {
|
||||
|
|
@ -4348,10 +4351,10 @@ func (st *HelmState) GenerateOutputDir(outputDir string, release *ReleaseSpec, o
|
|||
return buf.String(), nil
|
||||
}
|
||||
|
||||
// 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.
|
||||
// generateChartPath generates the path of the output directory for chart downloads (e.g., fetch, pull, OCI chart downloads).
|
||||
// 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
|
||||
}
|
||||
|
|
@ -4362,14 +4365,17 @@ func generateChartPath(chartName string, outputDir string, release *ReleaseSpec,
|
|||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
// Template data for output-dir-template. Environment provides .Name, .KubeContext, and .Values fields.
|
||||
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)
|
||||
|
|
@ -5045,7 +5051,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}
|
||||
|
|
|
|||
|
|
@ -4027,10 +4027,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",
|
||||
},
|
||||
}
|
||||
st := &HelmState{
|
||||
ReleaseSetSpec: ReleaseSetSpec{
|
||||
Env: environment.Environment{
|
||||
Name: "test-env",
|
||||
Values: map[string]any{
|
||||
"cluster": map[string]any{
|
||||
"name": "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)
|
||||
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)
|
||||
|
|
@ -4042,6 +4072,68 @@ func TestGenerateChartPath(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGenerateOutputDir(t *testing.T) {
|
||||
tests := []struct {
|
||||
testName string
|
||||
release *ReleaseSpec
|
||||
outputDir string
|
||||
outputDirTemplate string
|
||||
wantErr bool
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
testName: "PathGeneratedWithEnvironmentName",
|
||||
release: &ReleaseSpec{Name: "release-name"},
|
||||
outputDir: "/output-dir",
|
||||
outputDirTemplate: "{{ .OutputDir }}/{{ .Environment.Name }}/{{ .Release.Name }}",
|
||||
wantErr: false,
|
||||
expected: "/output-dir/test-env/release-name",
|
||||
},
|
||||
{
|
||||
testName: "PathGeneratedWithEnvironmentValues",
|
||||
release: &ReleaseSpec{Name: "release-name"},
|
||||
outputDir: "/output-dir",
|
||||
outputDirTemplate: "{{ .OutputDir }}/{{ .Environment.Values.cluster.name }}/{{ .Release.Name }}",
|
||||
wantErr: false,
|
||||
expected: "/output-dir/my-test-cluster/release-name",
|
||||
},
|
||||
{
|
||||
testName: "PathGeneratedWithEnvironmentKubeContext",
|
||||
release: &ReleaseSpec{Name: "release-name"},
|
||||
outputDir: "/output-dir",
|
||||
outputDirTemplate: "{{ .OutputDir }}/{{ .Environment.KubeContext }}/{{ .Release.Name }}",
|
||||
wantErr: false,
|
||||
expected: "/output-dir/test-kubecontext/release-name",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.testName, func(t *testing.T) {
|
||||
st := &HelmState{
|
||||
FilePath: "test.yaml",
|
||||
ReleaseSetSpec: ReleaseSetSpec{
|
||||
Env: environment.Environment{
|
||||
Name: "test-env",
|
||||
KubeContext: "test-kubecontext",
|
||||
Values: map[string]any{
|
||||
"cluster": map[string]any{
|
||||
"name": "my-test-cluster",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
got, err := st.GenerateOutputDir(tt.outputDir, tt.release, tt.outputDirTemplate)
|
||||
|
||||
if tt.wantErr {
|
||||
require.Errorf(t, err, "GenerateOutputDir() error \"%v\", want error", err)
|
||||
} else {
|
||||
require.NoError(t, err, "GenerateOutputDir() error \"%v\", want no error", err)
|
||||
}
|
||||
require.Equalf(t, tt.expected, got, "GenerateOutputDir() got \"%v\", want \"%v\"", got, tt.expected)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommonDiffFlags(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
|
|||
Loading…
Reference in New Issue