From fe6febba0deccb3eab031fe783b77f32f1e63ea5 Mon Sep 17 00:00:00 2001 From: "xin.li" Date: Fri, 24 Mar 2023 12:11:40 +0800 Subject: [PATCH] feat: add --purge-namespace flags for karmada deinit `karmada deinit` default not delete namespace Signed-off-by: xin.li --- pkg/karmadactl/deinit/deinit.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/pkg/karmadactl/deinit/deinit.go b/pkg/karmadactl/deinit/deinit.go index 8e9779b55..e73da95fa 100644 --- a/pkg/karmadactl/deinit/deinit.go +++ b/pkg/karmadactl/deinit/deinit.go @@ -37,8 +37,9 @@ type CommandDeInitOption struct { Namespace string // DryRun tells if run the command in dry-run mode, without making any server requests. - DryRun bool - Force bool + DryRun bool + Force bool + PurgeNamespace bool KubeClientSet *kubernetes.Clientset } @@ -81,6 +82,7 @@ func NewCmdDeInit(parentCommand string) *cobra.Command { flags.StringVar(&opts.Context, "context", "", "The name of the kubeconfig context to use") flags.BoolVar(&opts.DryRun, "dry-run", false, "Run the command in dry-run mode, without making any server requests.") flags.BoolVarP(&opts.Force, "force", "f", false, "Reset cluster without prompting for confirmation.") + flags.BoolVar(&opts.PurgeNamespace, "purge-namespace", false, "Run the command with purge-namespace, the namespace which Karmada components were installed will be deleted.") return cmd } @@ -143,14 +145,15 @@ func (o *CommandDeInitOption) delete() error { } // Delete namespace where Karmada components are installed - fmt.Printf("delete Namespace %q\n", o.Namespace) - if o.DryRun { - return nil + if o.PurgeNamespace { + fmt.Printf("delete Namespace %q\n", o.Namespace) + if o.DryRun { + return nil + } + if err = o.KubeClientSet.CoreV1().Namespaces().Delete(context.Background(), o.Namespace, metav1.DeleteOptions{}); err != nil { + return err + } } - if err = o.KubeClientSet.CoreV1().Namespaces().Delete(context.Background(), o.Namespace, metav1.DeleteOptions{}); err != nil { - return err - } - return nil }