Skip to content

Commit

Permalink
Vcluster delete --ignore-not-found
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Kartik-Garg committed Apr 3, 2023
1 parent 74e1195 commit 763922f
Showing 1 changed file with 72 additions and 40 deletions.
112 changes: 72 additions & 40 deletions cmd/vclusterctl/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 763922f

Please sign in to comment.