From 96c43a07da68e63fa92aa0ad489f07a7cc4b44a1 Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Mon, 13 Nov 2023 21:21:43 +0100 Subject: [PATCH] fix(uninstall): supersede deployed releases This ensures that when `helm uninstall` is run with `--keep-history`, any release in a `deployed` state other than the last release (e.g. due to a failed upgrade) is being marked as `superseded`. As a by-effect, running `helm upgrade` on a release which has been uninstalled after an upgrade failure now no longer works. But instead fails with a `"" has no deployed releases` error. Which is the (likely) intended behavior, and prevents other side-effects like rolling back to a release version which happened before the uninstall if `--atomic` was provided. Signed-off-by: Hidde Beydals --- .../uninstall-keep-history-earlier-deployed.txt | 1 + cmd/helm/uninstall_test.go | 9 +++++++++ pkg/action/uninstall.go | 13 +++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 cmd/helm/testdata/output/uninstall-keep-history-earlier-deployed.txt diff --git a/cmd/helm/testdata/output/uninstall-keep-history-earlier-deployed.txt b/cmd/helm/testdata/output/uninstall-keep-history-earlier-deployed.txt new file mode 100644 index 000000000..f5454b88d --- /dev/null +++ b/cmd/helm/testdata/output/uninstall-keep-history-earlier-deployed.txt @@ -0,0 +1 @@ +release "aeneas" uninstalled diff --git a/cmd/helm/uninstall_test.go b/cmd/helm/uninstall_test.go index 23b61058e..129362ce2 100644 --- a/cmd/helm/uninstall_test.go +++ b/cmd/helm/uninstall_test.go @@ -57,6 +57,15 @@ func TestUninstall(t *testing.T) { golden: "output/uninstall-keep-history.txt", rels: []*release.Release{release.Mock(&release.MockReleaseOptions{Name: "aeneas"})}, }, + { + name: "keep history with earlier deployed release", + cmd: "uninstall aeneas --keep-history", + golden: "output/uninstall-keep-history-earlier-deployed.txt", + rels: []*release.Release{ + release.Mock(&release.MockReleaseOptions{Name: "aeneas", Version: 1, Status: release.StatusDeployed}), + release.Mock(&release.MockReleaseOptions{Name: "aeneas", Version: 2, Status: release.StatusFailed}), + }, + }, { name: "wait", cmd: "uninstall aeneas --wait", diff --git a/pkg/action/uninstall.go b/pkg/action/uninstall.go index 40d82243e..953231f9b 100644 --- a/pkg/action/uninstall.go +++ b/pkg/action/uninstall.go @@ -28,6 +28,7 @@ import ( "helm.sh/helm/v3/pkg/kube" "helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/releaseutil" + "helm.sh/helm/v3/pkg/storage/driver" helmtime "helm.sh/helm/v3/pkg/time" ) @@ -166,6 +167,18 @@ func (u *Uninstall) Run(name string) (*release.UninstallReleaseResponse, error) return res, nil } + deployed, err := u.cfg.Releases.DeployedAll(rel.Name) + if err != nil && !errors.Is(err, driver.ErrNoDeployedReleases) { + return nil, err + } + // Supersede all previous deployments, see issue #12556 (which is a + // variation on #2941). + for _, rel := range deployed { + u.cfg.Log("superseding previous deployment %d", rel.Version) + rel.Info.Status = release.StatusSuperseded + u.cfg.recordRelease(rel) + } + if err := u.cfg.Releases.Update(rel); err != nil { u.cfg.Log("uninstall: Failed to store updated release: %s", err) }