From 763922f4fac61c41a3786ece1e0678771d642983 Mon Sep 17 00:00:00 2001 From: Kartik-Garg Date: Mon, 3 Apr 2023 19:49:49 +0530 Subject: [PATCH] Vcluster delete --ignore-not-found Added a new flag --ignore-not-found, which ignores the resource if the flag is mentioned instead of throwing and error and exiting Signed-off-by: Kartik-Garg --- cmd/vclusterctl/cmd/delete.go | 112 ++++++++++++++++++++++------------ 1 file changed, 72 insertions(+), 40 deletions(-) diff --git a/cmd/vclusterctl/cmd/delete.go b/cmd/vclusterctl/cmd/delete.go index f9fd448c10..cd65b3248d 100644 --- a/cmd/vclusterctl/cmd/delete.go +++ b/cmd/vclusterctl/cmd/delete.go @@ -3,8 +3,10 @@ package cmd import ( "context" "fmt" - "github.com/loft-sh/vcluster/pkg/util/translate" "os/exec" + "strings" + + "github.com/loft-sh/vcluster/pkg/util/translate" "github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/app/localkubernetes" "github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/find" @@ -31,10 +33,11 @@ type DeleteCmd struct { DeleteNamespace bool AutoDeleteNamespace bool - rawConfig *clientcmdapi.Config - restConfig *rest.Config - kubeClient *kubernetes.Clientset - log log.Logger + IgnoreNotFound bool + rawConfig *clientcmdapi.Config + restConfig *rest.Config + kubeClient *kubernetes.Clientset + log log.Logger } // NewDeleteCmd creates a new command @@ -68,6 +71,7 @@ vcluster delete test --namespace test cobraCmd.Flags().BoolVar(&cmd.KeepPVC, "keep-pvc", false, "If enabled, vcluster will not delete the persistent volume claim of the vcluster") cobraCmd.Flags().BoolVar(&cmd.DeleteNamespace, "delete-namespace", false, "If enabled, vcluster will delete the namespace of the vcluster. In the case of multi-namespace mode, will also delete all other namespaces created by vcluster") cobraCmd.Flags().BoolVar(&cmd.AutoDeleteNamespace, "auto-delete-namespace", true, "If enabled, vcluster will delete the namespace of the vcluster if it was created by vclusterctl. In the case of multi-namespace mode, will also delete all other namespaces created by vcluster") + cobraCmd.Flags().BoolVar(&cmd.IgnoreNotFound, "ignore-not-found", false, "If enabled, vcluster will not error out in case vcluster does not exists") return cobraCmd } @@ -90,7 +94,13 @@ func (cmd *DeleteCmd) Run(cobraCmd *cobra.Command, args []string) error { // prepare client err = cmd.prepare(args[0]) if err != nil { - return err + if cmd.IgnoreNotFound { + if strings.Contains(err.Error(), "couldn't find vcluster") { + cmd.log.Donef("vcluster %s not found in namespace %s, ignoring since --ignore-not-found flag is set", args[0], cmd.Namespace) + } + } else { + return err + } } // check if namespace @@ -107,7 +117,13 @@ func (cmd *DeleteCmd) Run(cobraCmd *cobra.Command, args []string) error { cmd.log.Infof("Delete vcluster %s...", args[0]) err = helm.NewClient(cmd.rawConfig, cmd.log, helmBinaryPath).Delete(args[0], cmd.Namespace) if err != nil { - return err + if cmd.IgnoreNotFound { + if strings.Contains(err.Error(), "was not found in namespace") { + cmd.log.Donef("vcluster %s not found in namespace %s, ignoring since --ignore-not-found flag is set", args[0], cmd.Namespace) + } + } else { + return err + } } cmd.log.Donef("Successfully deleted virtual cluster %s in namespace %s", args[0], cmd.Namespace) @@ -182,45 +198,61 @@ func (cmd *DeleteCmd) Run(cobraCmd *cobra.Command, args []string) error { } func (cmd *DeleteCmd) prepare(vClusterName string) error { - vCluster, err := find.GetVCluster(cmd.Context, vClusterName, cmd.Namespace) - if err != nil { - return err - } + // new changes here, we need to print new code here which just creates a new kubeclient and not do any of the stuff below, since vcluster does not exist + if cmd.IgnoreNotFound { + restConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}).ClientConfig() + kubeClient, err := kubernetes.NewForConfig(restConfig) + if err != nil { + return err + } + rawConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}).RawConfig() + if err != nil { + return err + } + cmd.rawConfig = &rawConfig + cmd.kubeClient = kubeClient + cmd.restConfig = restConfig + } else { + vCluster, err := find.GetVCluster(cmd.Context, vClusterName, cmd.Namespace) + if err != nil { + return err + } - // load the raw config - rawConfig, err := vCluster.ClientFactory.RawConfig() - if err != nil { - return fmt.Errorf("there is an error loading your current kube config (%v), please make sure you have access to a kubernetes cluster and the command `kubectl get namespaces` is working", err) - } - err = deleteContext(&rawConfig, find.VClusterContextName(vCluster.Name, vCluster.Namespace, vCluster.Context), vCluster.Context) - if err != nil { - return errors.Wrap(err, "delete kube context") - } + // load the raw config + rawConfig, err := vCluster.ClientFactory.RawConfig() + if err != nil { + return fmt.Errorf("there is an error loading your current kube config (%v), please make sure you have access to a kubernetes cluster and the command `kubectl get namespaces` is working", err) + } + err = deleteContext(&rawConfig, find.VClusterContextName(vCluster.Name, vCluster.Namespace, vCluster.Context), vCluster.Context) + if err != nil { + return errors.Wrap(err, "delete kube context") + } - rawConfig.CurrentContext = vCluster.Context - restConfig, err := vCluster.ClientFactory.ClientConfig() - if err != nil { - return err - } + rawConfig.CurrentContext = vCluster.Context + restConfig, err := vCluster.ClientFactory.ClientConfig() + if err != nil { + return err + } - err = localkubernetes.CleanupLocal(vClusterName, vCluster.Namespace, &rawConfig, cmd.log) - if err != nil { - cmd.log.Warnf("error cleaning up: %v", err) - } + err = localkubernetes.CleanupLocal(vClusterName, vCluster.Namespace, &rawConfig, cmd.log) + if err != nil { + cmd.log.Warnf("error cleaning up: %v", err) + } - // construct proxy name - proxyName := find.VClusterConnectBackgroundProxyName(vClusterName, vCluster.Namespace, rawConfig.CurrentContext) - _ = localkubernetes.CleanupBackgroundProxy(proxyName, cmd.log) + // construct proxy name + proxyName := find.VClusterConnectBackgroundProxyName(vClusterName, vCluster.Namespace, rawConfig.CurrentContext) + _ = localkubernetes.CleanupBackgroundProxy(proxyName, cmd.log) - kubeClient, err := kubernetes.NewForConfig(restConfig) - if err != nil { - return err - } + kubeClient, err := kubernetes.NewForConfig(restConfig) + if err != nil { + return err + } - cmd.Namespace = vCluster.Namespace - cmd.rawConfig = &rawConfig - cmd.restConfig = restConfig - cmd.kubeClient = kubeClient + cmd.Namespace = vCluster.Namespace + cmd.rawConfig = &rawConfig + cmd.restConfig = restConfig + cmd.kubeClient = kubeClient + } return nil }