diff --git a/cmd/helm/uninstall.go b/cmd/helm/uninstall.go index 67f778f15..a46f2e960 100644 --- a/cmd/helm/uninstall.go +++ b/cmd/helm/uninstall.go @@ -74,6 +74,6 @@ func newUninstallCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { 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.DurationVar(&client.Timeout, "timeout", 300*time.Second, "time to wait for any individual Kubernetes operation (like Jobs for hooks)") f.StringVar(&client.Description, "description", "", "add a custom description") - + f.BoolVar(&client.AllowNotFound, "allow-not-found", false, "if a release by this name doesn't already exist, it will not return an error") return cmd } diff --git a/pkg/action/uninstall.go b/pkg/action/uninstall.go index 65993df4c..d5c054266 100644 --- a/pkg/action/uninstall.go +++ b/pkg/action/uninstall.go @@ -26,6 +26,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" ) @@ -35,12 +36,13 @@ import ( type Uninstall struct { cfg *Configuration - DisableHooks bool - DryRun bool - KeepHistory bool - Wait bool - Timeout time.Duration - Description string + DisableHooks bool + DryRun bool + KeepHistory bool + Wait bool + Timeout time.Duration + Description string + AllowNotFound bool } // NewUninstall creates a new Uninstall object with the given configuration. @@ -70,6 +72,9 @@ func (u *Uninstall) Run(name string) (*release.UninstallReleaseResponse, error) } rels, err := u.cfg.Releases.History(name) + if u.AllowNotFound && errors.Is(err, driver.ErrReleaseNotFound) { + return &release.UninstallReleaseResponse{}, nil + } if err != nil { return nil, errors.Wrapf(err, "uninstall: Release not loaded: %s", name) } diff --git a/pkg/action/uninstall_test.go b/pkg/action/uninstall_test.go index 9cc75520b..e2200f9c6 100644 --- a/pkg/action/uninstall_test.go +++ b/pkg/action/uninstall_test.go @@ -95,3 +95,16 @@ func TestUninstallRelease_Wait(t *testing.T) { is.Contains(err.Error(), "U timed out") is.Equal(res.Release.Info.Status, release.StatusUninstalled) } + +func TestUninstallRelease_allowNotFound(t *testing.T) { + is := assert.New(t) + + unAction := uninstallAction(t) + unAction.DisableHooks = true + unAction.DryRun = false + unAction.KeepHistory = true + unAction.AllowNotFound = true + res, err := unAction.Run("chart-release-that-does-not-exist") + is.NoError(err) + is.Nil(res.Release) +}