From 8b8105fcb0ac6c5ba178c9df09e148083b572546 Mon Sep 17 00:00:00 2001 From: fang duan Date: Sat, 17 Oct 2020 13:20:51 +0800 Subject: [PATCH] Add --delete-namespace option for helm uninstall Signed-off-by: fang duan --- cmd/helm/uninstall.go | 4 ++++ pkg/action/uninstall.go | 48 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/cmd/helm/uninstall.go b/cmd/helm/uninstall.go index 509918e53..602c8a010 100644 --- a/cmd/helm/uninstall.go +++ b/cmd/helm/uninstall.go @@ -54,6 +54,9 @@ func newUninstallCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { return compListReleases(toComplete, cfg) }, RunE: func(cmd *cobra.Command, args []string) error { + + client.Namespace = settings.Namespace() + for i := 0; i < len(args); i++ { res, err := client.Run(args[i]) @@ -72,6 +75,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.DeleteNamespace, "delete-namespace", false, "delete the release namespace if present(be careful, this will remove release history)") 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") f.DurationVar(&client.Timeout, "timeout", 300*time.Second, "time to wait for any individual Kubernetes operation (like Jobs for hooks)") diff --git a/pkg/action/uninstall.go b/pkg/action/uninstall.go index c466c6ee2..83bda3f01 100644 --- a/pkg/action/uninstall.go +++ b/pkg/action/uninstall.go @@ -17,10 +17,14 @@ limitations under the License. package action import ( + "bytes" "strings" "time" "github.com/pkg/errors" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/yaml" "helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/release" @@ -34,11 +38,13 @@ import ( type Uninstall struct { cfg *Configuration - DisableHooks bool - DryRun bool - KeepHistory bool - Timeout time.Duration - Description string + DisableHooks bool + DryRun bool + DeleteNamespace bool + Namespace string + KeepHistory bool + Timeout time.Duration + Description string } // NewUninstall creates a new Uninstall object with the given configuration. @@ -63,6 +69,38 @@ func (u *Uninstall) Run(name string) (*release.UninstallReleaseResponse, error) return &release.UninstallReleaseResponse{Release: r}, nil } + if u.DeleteNamespace { + u.cfg.Log("uninstall: Deleting namespace %s", u.Namespace) + + ns := &v1.Namespace{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "v1", + Kind: "Namespace", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: u.Namespace, + Labels: map[string]string{ + "name": u.Namespace, + }, + }, + } + + buf, err := yaml.Marshal(ns) + if err != nil { + return nil, err + } + resourceList, err := u.cfg.KubeClient.Build(bytes.NewBuffer(buf), true) + if err != nil { + return nil, err + } + + _, errs := u.cfg.KubeClient.Delete(resourceList) + if len(errs) > 0 { + return nil, errors.New(joinErrors(errs)) + } + return &release.UninstallReleaseResponse{}, nil + } + if err := chartutil.ValidateReleaseName(name); err != nil { return nil, errors.Errorf("uninstall: Release name is invalid: %s", name) }