diff --git a/cmd/apply.go b/cmd/apply.go index 1cbb74f7..3e994d3d 100644 --- a/cmd/apply.go +++ b/cmd/apply.go @@ -70,8 +70,10 @@ func NewApplyCmd(globalCfg *config.GlobalImpl) *cobra.Command { f.StringArrayVar(&applyOptions.PostRendererArgs, "post-renderer-args", nil, `pass --post-renderer-args to "helm template" or "helm upgrade --install"`) f.StringVar(&applyOptions.Cascade, "cascade", "", "pass cascade to helm exec, default: background") f.StringArrayVar(&applyOptions.SuppressOutputLineRegex, "suppress-output-line-regex", nil, "a list of regex patterns to suppress output lines from the diff output") - f.StringVar(&applyOptions.DryRun, "dry-run", "", "pass dry-run to helm exec") - f.Lookup("dry-run").NoOptDefVal = "client" + f.StringVar(&applyOptions.DiffDryRun, "diff-dry-run", "", "pass dry-run to helm-diff exec") + f.Lookup("diff-dry-run").NoOptDefVal = "client" + f.StringVar(&applyOptions.DiffDryRun, "sync-dry-run", "", "pass dry-run to helm exec") + f.Lookup("sync-dry-run").NoOptDefVal = "client" return cmd } diff --git a/cmd/diff.go b/cmd/diff.go index 020bc6f1..7dc761f3 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -55,6 +55,8 @@ func NewDiffCmd(globalCfg *config.GlobalImpl) *cobra.Command { f.StringVar(&diffOptions.PostRenderer, "post-renderer", "", `pass --post-renderer to "helm template" or "helm upgrade --install"`) f.StringArrayVar(&diffOptions.PostRendererArgs, "post-renderer-args", nil, `pass --post-renderer-args to "helm template" or "helm upgrade --install"`) f.StringArrayVar(&diffOptions.SuppressOutputLineRegex, "suppress-output-line-regex", nil, "a list of regex patterns to suppress output lines from the diff output") + f.StringVar(&diffOptions.DryRun, "dry-run", "", "pass dry-run to helm-diff") + f.Lookup("dry-run").NoOptDefVal = "client" return cmd } diff --git a/pkg/app/app.go b/pkg/app/app.go index c0d314ce..f2d89e28 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -1424,6 +1424,7 @@ func (a *App) apply(r *Run, c ApplyConfigProvider) (bool, bool, []error) { PostRenderer: c.PostRenderer(), PostRendererArgs: c.PostRendererArgs(), SuppressOutputLineRegex: c.SuppressOutputLineRegex(), + DryRun: c.DryRun("diff"), } infoMsg, releasesToBeUpdated, releasesToBeDeleted, errs := r.diff(false, detailedExitCode, c, diffOpts) @@ -1536,7 +1537,7 @@ Do you really want to apply? PostRenderer: c.PostRenderer(), PostRendererArgs: c.PostRendererArgs(), SyncArgs: c.SyncArgs(), - DryRun: c.DryRun(), + DryRun: c.DryRun("sync"), } return subst.SyncReleases(&affectedReleases, helm, c.Values(), c.Concurrency(), syncOpts) })) @@ -1663,6 +1664,7 @@ func (a *App) diff(r *Run, c DiffConfigProvider) (*string, bool, bool, []error) PostRenderer: c.PostRenderer(), PostRendererArgs: c.PostRendererArgs(), SuppressOutputLineRegex: c.SuppressOutputLineRegex(), + DryRun: c.DryRun("diff"), } filtered := &Run{ @@ -1930,7 +1932,7 @@ Do you really want to sync? PostRenderer: c.PostRenderer(), PostRendererArgs: c.PostRendererArgs(), SyncArgs: c.SyncArgs(), - DryRun: c.DryRun(), + DryRun: c.DryRun("sync"), } return subst.SyncReleases(&affectedReleases, helm, c.Values(), c.Concurrency(), opts) })) diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index 394883ac..e7866b4a 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -2194,10 +2194,11 @@ func (c configImpl) ShowOnly() []string { } type applyConfig struct { - args string - cascade string - dryRun string - values []string + args string + cascade string + diffDryRun string + syncDryRun string + values []string // TODO: Remove this function once Helmfile v0.x retainValuesFiles bool @@ -2416,8 +2417,15 @@ func (a applyConfig) ShowOnly() []string { return a.showOnly } -func (a applyConfig) DryRun() string { - return a.dryRun +func (a applyConfig) DryRun(stage string) string { + switch stage { + case "diff": + return a.diffDryRun + case "sync": + return a.syncDryRun + default: + return "" + } } type depsConfig struct { diff --git a/pkg/app/config.go b/pkg/app/config.go index 7bf17e5e..22cc7b8d 100644 --- a/pkg/app/config.go +++ b/pkg/app/config.go @@ -52,7 +52,7 @@ type ApplyConfigProvider interface { PostRendererArgs() []string Cascade() string SuppressOutputLineRegex() []string - DryRun() string + DryRun(string) string Values() []string Set() []string @@ -100,7 +100,7 @@ type SyncConfigProvider interface { PostRenderer() string PostRendererArgs() []string Cascade() string - DryRun() string + DryRun(string) string Values() []string Set() []string @@ -134,6 +134,7 @@ type DiffConfigProvider interface { Validate() bool SkipCRDs() bool SkipDeps() bool + DryRun(string) string IncludeTests() bool diff --git a/pkg/app/diff_test.go b/pkg/app/diff_test.go index f33c04a6..cdf7d812 100644 --- a/pkg/app/diff_test.go +++ b/pkg/app/diff_test.go @@ -44,6 +44,7 @@ type diffConfig struct { skipDiffOnInstall bool reuseValues bool logger *zap.SugaredLogger + dryRun string } func (a diffConfig) Args() string { @@ -174,6 +175,9 @@ func (a diffConfig) SuppressOutputLineRegex() []string { return a.suppressOutputLineRegex } +func (a diffConfig) DryRun(string) string { + return a.dryRun +} func TestDiff(t *testing.T) { type flags struct { skipNeeds bool diff --git a/pkg/config/apply.go b/pkg/config/apply.go index 70be7efa..cee779bf 100644 --- a/pkg/config/apply.go +++ b/pkg/config/apply.go @@ -66,8 +66,10 @@ type ApplyOptions struct { SuppressOutputLineRegex []string // SyncArgs is the list of arguments to pass to helm upgrade. SyncArgs string - // DryRun is for helm dry-run flag - DryRun string + // DiffDryRun is for helm dry-run flag + DiffDryRun string + // SyncDryRun is for helm dry-run flag + SyncDryRun string } // NewApply creates a new Apply @@ -250,7 +252,14 @@ func (a *ApplyImpl) SyncArgs() string { return a.ApplyOptions.SyncArgs } -// DryRun returns dry-run flag -func (a *ApplyImpl) DryRun() string { - return a.ApplyOptions.DryRun +// DiffDryRun returns dry-run flag +func (a *ApplyImpl) DryRun(stage string) string { + switch stage { + case "diff": + return a.ApplyOptions.DiffDryRun + case "sync": + return a.ApplyOptions.SyncDryRun + default: + return "" + } } diff --git a/pkg/config/diff.go b/pkg/config/diff.go index 0fe525e8..56a2b1ec 100644 --- a/pkg/config/diff.go +++ b/pkg/config/diff.go @@ -48,6 +48,8 @@ type DiffOptions struct { DiffArgs string // SuppressOutputLineRegex is a list of regexes to suppress output lines SuppressOutputLineRegex []string + // DryRun is for helm-diff dry-run flag + DryRun string } // NewDiffOptions creates a new Apply @@ -199,3 +201,8 @@ func (t *DiffImpl) PostRendererArgs() []string { func (t *DiffImpl) SuppressOutputLineRegex() []string { return t.DiffOptions.SuppressOutputLineRegex } + +// DryRun returns dry-run flag +func (t *DiffImpl) DryRun(string) string { + return t.DiffOptions.DryRun +} diff --git a/pkg/config/sync.go b/pkg/config/sync.go index baa1622f..3139b486 100644 --- a/pkg/config/sync.go +++ b/pkg/config/sync.go @@ -143,6 +143,6 @@ func (t *SyncImpl) SyncArgs() string { } // DryRun returns dry-run flag -func (t *SyncImpl) DryRun() string { +func (t *SyncImpl) DryRun(string) string { return t.SyncOptions.DryRun } diff --git a/pkg/state/helmx.go b/pkg/state/helmx.go index 1790cc68..0e12fdc2 100644 --- a/pkg/state/helmx.go +++ b/pkg/state/helmx.go @@ -109,12 +109,12 @@ func (st *HelmState) appendWaitFlags(flags []string, release *ReleaseSpec, ops * // appendDryRunFlags appends the necessary flags for a dry run to the given flags slice. // If the opt parameter is not nil and opt.DryRun is not empty, the "--dry-run" flag and the value of opt.DryRun are appended to the flags slice. // The updated flags slice is returned. -func (st *HelmState) appendDryRunFlags(flags []string, helm helmexec.Interface, opt *SyncOpts) []string { +func (st *HelmState) appendDryRunFlags(flags []string, helm helmexec.Interface, dryRun string) []string { if !helm.IsVersionAtLeast("3.13.0") { return flags } - if opt != nil && opt.DryRun != "" { - flags = append(flags, fmt.Sprintf("--dry-run=%s", opt.DryRun)) + if dryRun != "" { + flags = append(flags, fmt.Sprintf("--dry-run=%s", dryRun)) } return flags } diff --git a/pkg/state/helmx_test.go b/pkg/state/helmx_test.go index d699e3bb..e96bca79 100644 --- a/pkg/state/helmx_test.go +++ b/pkg/state/helmx_test.go @@ -328,9 +328,7 @@ func TestAppendDryRunFlags(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { st := &HelmState{} - got := st.appendDryRunFlags([]string{}, tt.args.helm, &SyncOpts{ - DryRun: tt.args.dryRun, - }) + got := st.appendDryRunFlags([]string{}, tt.args.helm, tt.args.dryRun) require.Equalf(t, tt.args.expected, got, "appendDryRunFlags() = %v, want %v", got, tt.args.expected) }) } diff --git a/pkg/state/state.go b/pkg/state/state.go index 56824cb9..f503665d 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -1967,6 +1967,7 @@ type DiffOpts struct { PostRenderer string PostRendererArgs []string SuppressOutputLineRegex []string + DryRun string } func (o *DiffOpts) Apply(opts *DiffOpts) { @@ -2673,7 +2674,9 @@ func (st *HelmState) flagsForUpgrade(helm helmexec.Interface, release *ReleaseSp postRendererArgs = opt.PostRendererArgs } flags = st.appendPostRenderArgsFlags(flags, release, postRendererArgs) - flags = st.appendDryRunFlags(flags, helm, opt) + if opt != nil && opt.DryRun != "" { + flags = st.appendDryRunFlags(flags, helm, opt.DryRun) + } flags = st.appendExtraSyncFlags(flags, opt) @@ -2795,6 +2798,9 @@ func (st *HelmState) flagsForDiff(helm helmexec.Interface, release *ReleaseSpec, if err != nil { return nil, files, err } + if opt != nil && opt.DryRun != "" { + flags = st.appendDryRunFlags(flags, helm, opt.DryRun) + } return append(flags, common...), files, nil }