From 9e198fa89d3c798dec1012bb4dff7107e22700d7 Mon Sep 17 00:00:00 2001 From: Alex Petrov Date: Thu, 24 Nov 2022 14:24:46 -0500 Subject: [PATCH 1/2] fix: reinstall previously uninstalled chart with --keep-history Before, if a chart was uninstalled using the `--keep-history` flag, the next attempt to upgrade/install the chart would result in an error. This commit fixes that by correctly checking if the last release of the chart stored in history has been uninstalled and running the install with the `--replace` flag if that's the case. Signed-off-by: Alex Petrov --- .../output/upgrade-uninstalled-with-keep-history.txt | 7 +++++++ cmd/helm/upgrade.go | 7 ++++++- cmd/helm/upgrade_test.go | 6 ++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 cmd/helm/testdata/output/upgrade-uninstalled-with-keep-history.txt diff --git a/cmd/helm/testdata/output/upgrade-uninstalled-with-keep-history.txt b/cmd/helm/testdata/output/upgrade-uninstalled-with-keep-history.txt new file mode 100644 index 000000000..f53b8715a --- /dev/null +++ b/cmd/helm/testdata/output/upgrade-uninstalled-with-keep-history.txt @@ -0,0 +1,7 @@ +Release "funny-bunny" does not exist. Installing it now. +NAME: funny-bunny +LAST DEPLOYED: Fri Sep 2 22:04:05 1977 +NAMESPACE: default +STATUS: deployed +REVISION: 3 +TEST SUITE: None diff --git a/cmd/helm/upgrade.go b/cmd/helm/upgrade.go index 33db703c4..5d3c1d020 100644 --- a/cmd/helm/upgrade.go +++ b/cmd/helm/upgrade.go @@ -36,6 +36,7 @@ import ( "helm.sh/helm/v3/pkg/cli/values" "helm.sh/helm/v3/pkg/downloader" "helm.sh/helm/v3/pkg/getter" + "helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/storage/driver" ) @@ -96,7 +97,8 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { // If a release does not exist, install it. histClient := action.NewHistory(cfg) histClient.Max = 1 - if _, err := histClient.Run(args[0]); err == driver.ErrReleaseNotFound { + rel, err := histClient.Run(args[0]) + if err == driver.ErrReleaseNotFound || (len(rel) > 0 && rel[len(rel)-1].Info.Status == release.StatusUninstalled) { // Only print this to stdout for table output if outfmt == output.Table { fmt.Fprintf(out, "Release %q does not exist. Installing it now.\n", args[0]) @@ -118,6 +120,9 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { instClient.SubNotes = client.SubNotes instClient.Description = client.Description instClient.DependencyUpdate = client.DependencyUpdate + if len(rel) > 0 && rel[len(rel)-1].Info.Status == release.StatusUninstalled { + instClient.Replace = true + } rel, err := runInstall(args, instClient, valueOpts, out) if err != nil { diff --git a/cmd/helm/upgrade_test.go b/cmd/helm/upgrade_test.go index 8afcb139b..e687a5425 100644 --- a/cmd/helm/upgrade_test.go +++ b/cmd/helm/upgrade_test.go @@ -176,6 +176,12 @@ func TestUpgradeCmd(t *testing.T) { wantError: true, rels: []*release.Release{relWithStatusMock("funny-bunny", 2, ch, release.StatusPendingInstall)}, }, + { + name: "install a previously uninstalled release with '--keep-history' using 'upgrade --install'", + cmd: fmt.Sprintf("upgrade funny-bunny -i '%s'", chartPath), + golden: "output/upgrade-uninstalled-with-keep-history.txt", + rels: []*release.Release{relWithStatusMock("funny-bunny", 2, ch, release.StatusUninstalled)}, + }, } runTestCmd(t, tests) } From f908379f1f8e3d764b0a52dcba2d234490fc0ffc Mon Sep 17 00:00:00 2001 From: Alex Petrov Date: Fri, 25 Nov 2022 21:05:12 -0500 Subject: [PATCH 2/2] refactor: create a helper for checking if a release is uninstalled Signed-off-by: Alex Petrov --- cmd/helm/upgrade.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cmd/helm/upgrade.go b/cmd/helm/upgrade.go index 5d3c1d020..42b59f60a 100644 --- a/cmd/helm/upgrade.go +++ b/cmd/helm/upgrade.go @@ -97,8 +97,8 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { // If a release does not exist, install it. histClient := action.NewHistory(cfg) histClient.Max = 1 - rel, err := histClient.Run(args[0]) - if err == driver.ErrReleaseNotFound || (len(rel) > 0 && rel[len(rel)-1].Info.Status == release.StatusUninstalled) { + versions, err := histClient.Run(args[0]) + if err == driver.ErrReleaseNotFound || isReleaseUninstalled(versions) { // Only print this to stdout for table output if outfmt == output.Table { fmt.Fprintf(out, "Release %q does not exist. Installing it now.\n", args[0]) @@ -120,7 +120,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { instClient.SubNotes = client.SubNotes instClient.Description = client.Description instClient.DependencyUpdate = client.DependencyUpdate - if len(rel) > 0 && rel[len(rel)-1].Info.Status == release.StatusUninstalled { + if isReleaseUninstalled(versions) { instClient.Replace = true } @@ -254,3 +254,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { return cmd } + +func isReleaseUninstalled(versions []*release.Release) bool { + return len(versions) > 0 && versions[len(versions)-1].Info.Status == release.StatusUninstalled +}