From 2e52967d686cae4df390feca8265b837f1c8f9d5 Mon Sep 17 00:00:00 2001 From: Kerstin Albers Date: Thu, 6 Feb 2025 14:31:33 +0100 Subject: [PATCH] feat: add template args to be used for template and within apply Signed-off-by: Kerstin Albers --- cmd/apply.go | 1 + cmd/template.go | 1 + docs/index.md | 2 ++ pkg/app/app.go | 2 ++ pkg/app/app_test.go | 10 ++++++++++ pkg/app/config.go | 2 ++ pkg/config/apply.go | 9 ++++++++- pkg/config/template.go | 7 +++++++ pkg/state/state.go | 6 ++++++ 9 files changed, 39 insertions(+), 1 deletion(-) diff --git a/cmd/apply.go b/cmd/apply.go index 6b5a23f4..b4b598ab 100644 --- a/cmd/apply.go +++ b/cmd/apply.go @@ -42,6 +42,7 @@ func NewApplyCmd(globalCfg *config.GlobalImpl) *cobra.Command { f.BoolVar(&applyOptions.StripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input") f.StringVar(&applyOptions.DiffArgs, "diff-args", "", `pass args to helm helm-diff`) f.StringVar(&applyOptions.SyncArgs, "sync-args", "", `pass args to helm upgrade`) + f.StringVar(&applyOptions.TemplateArgs, "template-args", "", `pass args to helm template`) f.StringVar(&globalCfg.GlobalOptions.Args, "args", "", "pass args to helm exec") f.BoolVar(&applyOptions.SkipCleanup, "skip-cleanup", false, "Stop cleaning up temporary values generated by helmfile and helm-secrets. Useful for debugging. Don't use in production for security") f.BoolVar(&applyOptions.SkipCRDs, "skip-crds", false, "if set, no CRDs will be installed on sync. By default, CRDs are installed if not already present") diff --git a/cmd/template.go b/cmd/template.go index 6cb81fea..5bb146a0 100644 --- a/cmd/template.go +++ b/cmd/template.go @@ -50,6 +50,7 @@ func NewTemplateCmd(globalCfg *config.GlobalImpl) *cobra.Command { f.BoolVar(&templateOptions.SkipSchemaValidation, "skip-schema-validation", false, `pass skip-schema-validation to "helm template" or "helm upgrade --install"`) f.StringVar(&templateOptions.KubeVersion, "kube-version", "", `pass --kube-version to "helm template". Overrides kubeVersion in helmfile.yaml`) f.StringArrayVar(&templateOptions.ShowOnly, "show-only", nil, `pass --show-only to "helm template"`) + f.StringVar(&templateOptions.TemplateArgs, "template-args", "", `pass args to helm template`) return cmd } diff --git a/docs/index.md b/docs/index.md index 59d3442f..a1a2c12c 100644 --- a/docs/index.md +++ b/docs/index.md @@ -187,6 +187,8 @@ helmDefaults: - "--suppress-secrets" syncArgs: - "--labels=app.kubernetes.io/managed-by=helmfile" + templateArgs: + - "--dry-run=server" # verify the chart before upgrading (only works with packaged charts not directories) (default false) verify: true keyring: path/to/keyring.gpg diff --git a/pkg/app/app.go b/pkg/app/app.go index aa73fbb2..7da166de 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -236,6 +236,7 @@ func (a *App) Template(c TemplateConfigProvider) error { Set: c.Set(), Values: c.Values(), KubeVersion: c.KubeVersion(), + TemplateArgs: c.TemplateArgs(), }, func() { ok, errs = a.template(run, c) }) @@ -406,6 +407,7 @@ func (a *App) Apply(c ApplyConfigProvider) error { Validate: c.Validate(), Concurrency: c.Concurrency(), IncludeTransitiveNeeds: c.IncludeNeeds(), + TemplateArgs: c.TemplateArgs(), }, func() { matched, updated, es := a.apply(run, c) diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index cf38aa2e..25d11b6b 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -2109,6 +2109,7 @@ type configImpl struct { includeTransitiveNeeds bool skipCharts bool kubeVersion string + templateArgs string } func (c configImpl) Selectors() []string { @@ -2207,6 +2208,10 @@ func (c configImpl) KubeVersion() string { return c.kubeVersion } +func (c configImpl) TemplateArgs() string { + return c.templateArgs +} + func (c configImpl) SkipSchemaValidation() bool { return c.skipSchemaValidation } @@ -2246,6 +2251,7 @@ type applyConfig struct { skipDiffOnInstall bool syncArgs string diffArgs string + templateArgs string logger *zap.SugaredLogger wait bool waitRetries int @@ -2398,6 +2404,10 @@ func (a applyConfig) DiffArgs() string { return a.diffArgs } +func (a applyConfig) TemplateArgs() string { + return a.templateArgs +} + // helmfile-template-only flags func (a applyConfig) IncludeCRDs() bool { return a.includeCRDs diff --git a/pkg/app/config.go b/pkg/app/config.go index e699d86f..a3d76d7e 100644 --- a/pkg/app/config.go +++ b/pkg/app/config.go @@ -79,6 +79,7 @@ type ApplyConfigProvider interface { DiffArgs() string SyncArgs() string + TemplateArgs() string SyncReleaseLabels() bool @@ -232,6 +233,7 @@ type TemplateConfigProvider interface { NoHooks() bool KubeVersion() string ShowOnly() []string + TemplateArgs() string DAGConfig diff --git a/pkg/config/apply.go b/pkg/config/apply.go index 81a9a195..006956a3 100644 --- a/pkg/config/apply.go +++ b/pkg/config/apply.go @@ -68,11 +68,13 @@ type ApplyOptions struct { SyncArgs string // HideNotes is the hide notes flag HideNotes bool - // TakeOwnership is true if the ownership should be taken TakeOwnership bool SyncReleaseLabels bool + + // TemplateArgs is the list of arguments to pass to helm template + TemplateArgs string } // NewApply creates a new Apply @@ -273,3 +275,8 @@ func (a *ApplyImpl) TakeOwnership() bool { func (a *ApplyImpl) SyncReleaseLabels() bool { return a.ApplyOptions.SyncReleaseLabels } + +// TemplateArgs returns the TemplateArgs. +func (a *ApplyImpl) TemplateArgs() string { + return a.ApplyOptions.TemplateArgs +} diff --git a/pkg/config/template.go b/pkg/config/template.go index 90c82691..b12736a1 100644 --- a/pkg/config/template.go +++ b/pkg/config/template.go @@ -44,6 +44,8 @@ type TemplateOptions struct { KubeVersion string // Propagate '--show-only` to helm template ShowOnly []string + // TemplateArgs + TemplateArgs string } // NewTemplateOptions creates a new Apply @@ -158,3 +160,8 @@ func (t *TemplateImpl) KubeVersion() string { func (t *TemplateImpl) ShowOnly() []string { return t.TemplateOptions.ShowOnly } + +// TemplateArgs returns the TemplateArgs +func (t *TemplateImpl) TemplateArgs() string { + return t.TemplateOptions.TemplateArgs +} diff --git a/pkg/state/state.go b/pkg/state/state.go index af26d017..54b0b9f7 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -1160,6 +1160,7 @@ type ChartPrepareOptions struct { KubeVersion string Set []string Values []string + TemplateArgs string // Delete wait DeleteWait bool DeleteTimeout int @@ -1337,6 +1338,10 @@ func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurre chartifyOpts.Validate = opts.Validate + if (helmfileCommand == "template" || helmfileCommand == "apply") && opts.TemplateArgs != "" { + chartifyOpts.TemplateArgs = opts.TemplateArgs + } + chartifyOpts.KubeVersion = st.getKubeVersion(release, opts.KubeVersion) chartifyOpts.ApiVersions = st.getApiVersions(release) @@ -1543,6 +1548,7 @@ type TemplateOpts struct { ShowOnly []string // Propagate '--skip-schema-validation' to helmv3 template and helm install SkipSchemaValidation bool + TemplateArgs string } type TemplateOpt interface{ Apply(*TemplateOpts) }