From 27c6a8c1cf4af0754282d0f37a51754ff49682b0 Mon Sep 17 00:00:00 2001 From: George Jenkins Date: Sun, 15 Jan 2023 22:05:31 -0800 Subject: [PATCH] upgrade Signed-off-by: George Jenkins --- cmd/helm/install.go | 4 ++-- cmd/helm/upgrade.go | 9 ++++++++- pkg/action/dryrunmode.go | 9 +++++++++ pkg/action/install.go | 8 -------- pkg/action/upgrade.go | 33 ++++++++++++++++++++++++++++----- 5 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 pkg/action/dryrunmode.go diff --git a/cmd/helm/install.go b/cmd/helm/install.go index f03d981ea..658842390 100644 --- a/cmd/helm/install.go +++ b/cmd/helm/install.go @@ -122,7 +122,7 @@ To see the list of chart repositories, use 'helm repo list'. To search for charts in a repository, use 'helm search'. ` -func determineInstallDryRunMode(dryRunModeFlag string) (*action.DryRunMode, error) { +func determineDryRunMode(dryRunModeFlag string) (*action.DryRunMode, error) { switch dryRunModeFlag { case "none": case "false": // TODO: Remove "false" helm v4 @@ -153,7 +153,7 @@ func newInstallCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { }, RunE: func(_ *cobra.Command, args []string) error { - dryRunMode, err := determineInstallDryRunMode(dryRunModeFlag) + dryRunMode, err := determineDryRunMode(dryRunModeFlag) if err != nil { return err } diff --git a/cmd/helm/upgrade.go b/cmd/helm/upgrade.go index 02f4cf2a9..43fec2772 100644 --- a/cmd/helm/upgrade.go +++ b/cmd/helm/upgrade.go @@ -72,6 +72,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { valueOpts := &values.Options{} var outfmt output.Format var createNamespace bool + var dryRunModeFlag string cmd := &cobra.Command{ Use: "upgrade [RELEASE] [CHART]", @@ -106,6 +107,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { instClient.ChartPathOptions = client.ChartPathOptions instClient.Force = client.Force instClient.DryRun = client.DryRun + instClient.DryRunMode = client.DryRunMode instClient.DisableHooks = client.DisableHooks instClient.SkipCRDs = client.SkipCRDs instClient.Timeout = client.Timeout @@ -214,7 +216,12 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { f.BoolVar(&createNamespace, "create-namespace", false, "if --install is set, create the release namespace if not present") f.BoolVarP(&client.Install, "install", "i", false, "if a release by this name doesn't already exist, run an install") f.BoolVar(&client.Devel, "devel", false, "use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored") - f.BoolVar(&client.DryRun, "dry-run", false, "simulate an upgrade") + f.StringVar( + &dryRunModeFlag, + "dry-run", + "none", + `simulate an install. Must be "none", "server", or "client". If client strategy, X. If server strategy, Y. For backwards compatibility, boolean values "true" and "false" are also accepted. "true" being a synonym for "client". "false" meaning disable dry-run`, + ) f.BoolVar(&client.Recreate, "recreate-pods", false, "performs pods restart for the resource if applicable") f.MarkDeprecated("recreate-pods", "functionality will no longer be updated. Consult the documentation for other methods to recreate pods") f.BoolVar(&client.Force, "force", false, "force resource updates through a replacement strategy") diff --git a/pkg/action/dryrunmode.go b/pkg/action/dryrunmode.go new file mode 100644 index 000000000..db0e708fb --- /dev/null +++ b/pkg/action/dryrunmode.go @@ -0,0 +1,9 @@ +package action + +type DryRunMode string + +var ( + DryRunModeNone DryRunMode = "none" + DryRunModeClient DryRunMode = "client" + DryRunModeServer DryRunMode = "server" +) diff --git a/pkg/action/install.go b/pkg/action/install.go index 452e9ba44..835dc770b 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -62,14 +62,6 @@ const notesFileSuffix = "NOTES.txt" const defaultDirectoryPermission = 0755 -type DryRunMode string - -var ( - DryRunModeNone DryRunMode = "none" - DryRunModeClient DryRunMode = "client" - DryRunModeServer DryRunMode = "server" -) - // Install performs an installation operation. type Install struct { cfg *Configuration diff --git a/pkg/action/upgrade.go b/pkg/action/upgrade.go index 7bdeaae5b..002dcf443 100644 --- a/pkg/action/upgrade.go +++ b/pkg/action/upgrade.go @@ -69,9 +69,14 @@ type Upgrade struct { WaitForJobs bool // DisableHooks disables hook processing if set to true. DisableHooks bool - // DryRun controls whether the operation is prepared, but not executed. + // Deprecated: DryRun controls whether the operation is prepared, but not executed. // If `true`, the upgrade is prepared but not performed. DryRun bool + // DryRunMode controls whether the operation is prepared, but not executed. + // If set, the upgrade is prepared but not performed. + // For DryRunModeClient + // For DryRunModeServer + DryRunMode DryRunMode // Force will, if set to `true`, ignore certain warnings and perform the upgrade anyway. // // This should be used with caution. @@ -153,7 +158,7 @@ func (u *Upgrade) RunWithContext(ctx context.Context, name string, chart *chart. return res, err } - if !u.DryRun { + if !u.isDryRun() { u.cfg.Log("updating status for upgraded release for %s", name) if err := u.cfg.Releases.Update(upgradedRelease); err != nil { return res, err @@ -163,6 +168,23 @@ func (u *Upgrade) RunWithContext(ctx context.Context, name string, chart *chart. return res, nil } +func (u *Upgrade) isDryRun() bool { + if u.DryRunMode != "" { + switch u.DryRunMode { + case DryRunModeClient: + case DryRunModeServer: + return true + case DryRunModeNone: + return false + default: + panic("Invalid DryRun mode") + } + } + + // Fallback to legacy + return u.DryRun +} + // prepareUpgrade builds an upgraded release for an upgrade operation. func (u *Upgrade) prepareUpgrade(name string, chart *chart.Chart, vals map[string]interface{}) (*release.Release, *release.Release, error) { if chart == nil { @@ -230,8 +252,9 @@ func (u *Upgrade) prepareUpgrade(name string, chart *chart.Chart, vals map[strin if err != nil { return nil, nil, err } - // Interacts with cluster if possible - hooks, manifestDoc, notesTxt, err := u.cfg.renderResources(chart, valuesToRender, "", "", u.SubNotes, false, false, u.PostRenderer, true) + + interactWithRemote := u.DryRunMode == DryRunModeServer + hooks, manifestDoc, notesTxt, err := u.cfg.renderResources(chart, valuesToRender, "", "", u.SubNotes, false, false, u.PostRenderer, interactWithRemote) if err != nil { return nil, nil, err } @@ -309,7 +332,7 @@ func (u *Upgrade) performUpgrade(ctx context.Context, originalRelease, upgradedR return nil }) - if u.DryRun { + if u.isDryRun() { u.cfg.Log("dry run for %s", upgradedRelease.Name) if len(u.Description) > 0 { upgradedRelease.Info.Description = u.Description