Add --delete-namespace option for helm uninstall

Signed-off-by: fang duan <df1228@gmail.com>
pull/8912/head
fang duan 5 years ago
parent e1d4c67437
commit 8b8105fcb0
No known key found for this signature in database
GPG Key ID: 3B0F7D1148EB3839

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

@ -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"
@ -36,6 +40,8 @@ type Uninstall struct {
DisableHooks bool
DryRun bool
DeleteNamespace bool
Namespace string
KeepHistory bool
Timeout time.Duration
Description string
@ -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)
}

Loading…
Cancel
Save