From 8fbcfee16ad6e35a407f897def1ccda857cf0f85 Mon Sep 17 00:00:00 2001 From: zze <632404164@qq.com> Date: Tue, 12 Apr 2022 16:19:25 +0800 Subject: [PATCH] feat: Add --only--release option to uninstall --- cmd/helm/uninstall.go | 1 + pkg/action/resource_policy.go | 8 +++++++- pkg/action/uninstall.go | 3 ++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/helm/uninstall.go b/cmd/helm/uninstall.go index 67f778f15..fc992489a 100644 --- a/cmd/helm/uninstall.go +++ b/cmd/helm/uninstall.go @@ -68,6 +68,7 @@ func newUninstallCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { } f := cmd.Flags() + f.BoolVar(&client.OnlyRelease, "only-release", false, "keep all k8s resources when uninstall") 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.KeepHistory, "keep-history", false, "remove all associated resources and mark the release as deleted, but retain the release history") diff --git a/pkg/action/resource_policy.go b/pkg/action/resource_policy.go index 63e83f3d9..5c8cab6e8 100644 --- a/pkg/action/resource_policy.go +++ b/pkg/action/resource_policy.go @@ -23,8 +23,14 @@ import ( "helm.sh/helm/v3/pkg/releaseutil" ) -func filterManifestsToKeep(manifests []releaseutil.Manifest) (keep, remaining []releaseutil.Manifest) { +func filterManifestsToKeep(manifests []releaseutil.Manifest, onlyRelease bool) (keep, remaining []releaseutil.Manifest) { for _, m := range manifests { + if onlyRelease { + if m.Head.Metadata != nil { + keep = append(keep, m) + } + continue + } if m.Head.Metadata == nil || m.Head.Metadata.Annotations == nil || len(m.Head.Metadata.Annotations) == 0 { remaining = append(remaining, m) continue diff --git a/pkg/action/uninstall.go b/pkg/action/uninstall.go index 65993df4c..4e7c7e06d 100644 --- a/pkg/action/uninstall.go +++ b/pkg/action/uninstall.go @@ -36,6 +36,7 @@ type Uninstall struct { cfg *Configuration DisableHooks bool + OnlyRelease bool DryRun bool KeepHistory bool Wait bool @@ -200,7 +201,7 @@ func (u *Uninstall) deleteRelease(rel *release.Release) (kube.ResourceList, stri return nil, rel.Manifest, []error{errors.Wrap(err, "corrupted release record. You must manually delete the resources")} } - filesToKeep, filesToDelete := filterManifestsToKeep(files) + filesToKeep, filesToDelete := filterManifestsToKeep(files, u.OnlyRelease) var kept string for _, f := range filesToKeep { kept += "[" + f.Head.Kind + "] " + f.Head.Metadata.Name + "\n"