feat(uninstall): add flag allow-not-found

It would accept release: not found as successful uninstallation, as technically it was successfully
uninstalled.

Fixes #9687

Signed-off-by: cndoit18 <cndoit18@outlook.com>
pull/9689/head
cndoit18 4 years ago
parent e9a2e42ab1
commit dd32b2320e
No known key found for this signature in database
GPG Key ID: A5E54CE7AC730381

@ -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
}

@ -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)
}

@ -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)
}

Loading…
Cancel
Save