From 48dbda2fa8d1e8981c271a56fe51bdf8b131fac2 Mon Sep 17 00:00:00 2001 From: suzaku Date: Mon, 17 Jan 2022 10:53:53 +0800 Subject: [PATCH 1/2] Fix #3352, add support for --ignore-not-found just like kubectl delete Signed-off-by: suzaku --- cmd/helm/uninstall.go | 1 + pkg/action/uninstall.go | 4 ++++ pkg/action/uninstall_test.go | 11 +++++++++++ 3 files changed, 16 insertions(+) diff --git a/cmd/helm/uninstall.go b/cmd/helm/uninstall.go index 1a47e972cd3..9ced8fef06e 100644 --- a/cmd/helm/uninstall.go +++ b/cmd/helm/uninstall.go @@ -74,6 +74,7 @@ func newUninstallCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { f := cmd.Flags() f.BoolVar(&client.DryRun, "dry-run", false, "simulate a uninstall") f.BoolVar(&client.DisableHooks, "no-hooks", false, "prevent hooks from running during uninstallation") + f.BoolVar(&client.IgnoreNotFound, "ignore-not-found", false, `Treat "release not found" as a successful uninstall`) f.BoolVar(&client.KeepHistory, "keep-history", false, "remove all associated resources and mark the release as deleted, but retain the release history") f.BoolVar(&client.Wait, "wait", false, "if set, will wait until all the resources are deleted before returning. It will wait for as long as --timeout") f.StringVar(&client.DeletionPropagation, "cascade", "background", "Must be \"background\", \"orphan\", or \"foreground\". Selects the deletion cascading strategy for the dependents. Defaults to background.") diff --git a/pkg/action/uninstall.go b/pkg/action/uninstall.go index 801498544fa..a08cb2320a4 100644 --- a/pkg/action/uninstall.go +++ b/pkg/action/uninstall.go @@ -38,6 +38,7 @@ type Uninstall struct { DisableHooks bool DryRun bool + IgnoreNotFound bool KeepHistory bool Wait bool DeletionPropagation string @@ -73,6 +74,9 @@ func (u *Uninstall) Run(name string) (*release.UninstallReleaseResponse, error) rels, err := u.cfg.Releases.History(name) if err != nil { + if u.IgnoreNotFound { + return nil, nil + } return nil, errors.Wrapf(err, "uninstall: Release not loaded: %s", name) } if len(rels) < 1 { diff --git a/pkg/action/uninstall_test.go b/pkg/action/uninstall_test.go index 311a34923b9..869ffb8c72b 100644 --- a/pkg/action/uninstall_test.go +++ b/pkg/action/uninstall_test.go @@ -32,6 +32,17 @@ func uninstallAction(t *testing.T) *Uninstall { return unAction } +func TestUninstallRelease_ignoreNotFound(t *testing.T) { + unAction := uninstallAction(t) + unAction.DryRun = false + unAction.IgnoreNotFound = true + + is := assert.New(t) + res, err := unAction.Run("release-non-exist") + is.Nil(res) + is.NoError(err) +} + func TestUninstallRelease_deleteRelease(t *testing.T) { is := assert.New(t) From 5b0898513820e5952e0293b81fd098429f0e394f Mon Sep 17 00:00:00 2001 From: satoru Date: Sun, 4 Sep 2022 16:21:54 +0800 Subject: [PATCH 2/2] goimports Signed-off-by: satoru --- pkg/action/uninstall.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/action/uninstall.go b/pkg/action/uninstall.go index a08cb2320a4..40d82243edb 100644 --- a/pkg/action/uninstall.go +++ b/pkg/action/uninstall.go @@ -21,6 +21,7 @@ import ( "time" "github.com/pkg/errors" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "helm.sh/helm/v3/pkg/chartutil"