From af10de419be7e0d4fdb586a9c9cb24df3f1380db Mon Sep 17 00:00:00 2001 From: Joeyyy09 Date: Fri, 31 May 2024 15:31:49 +0530 Subject: [PATCH] Support Force Delete Cluster Signed-off-by: Joeyyy09 Add check for runtime Signed-off-by: Joeyyy09 Update docs Signed-off-by: Joeyyy09 Rebase to main Signed-off-by: Joeyyy09 Add force flag Signed-off-by: Joeyyy09 Modify according to the patch Signed-off-by: Joeyyy09 --- pkg/kwokctl/cmd/delete/cluster/cluster.go | 16 ++++++++++++---- .../en/docs/generated/kwokctl_delete_cluster.md | 1 + 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/pkg/kwokctl/cmd/delete/cluster/cluster.go b/pkg/kwokctl/cmd/delete/cluster/cluster.go index cf2fbbfc1..3afe5297c 100644 --- a/pkg/kwokctl/cmd/delete/cluster/cluster.go +++ b/pkg/kwokctl/cmd/delete/cluster/cluster.go @@ -36,6 +36,7 @@ type flagpole struct { Name string Kubeconfig string All bool + Force bool } // NewCommand returns a new cobra.Command for cluster deletion @@ -56,7 +57,7 @@ func NewCommand(ctx context.Context) *cobra.Command { } cmd.Flags().StringVar(&flags.Kubeconfig, "kubeconfig", flags.Kubeconfig, "The path to the kubeconfig file that will remove the deleted cluster") cmd.Flags().BoolVar(&flags.All, "all", flags.All, "Delete all clusters managed by kwokctl") - + cmd.Flags().BoolVar(&flags.Force, "force", false, "Force delete the cluster") return cmd } @@ -70,13 +71,13 @@ func runE(ctx context.Context, flags *flagpole) error { return err } for _, cluster := range clusters { - err = deleteCluster(ctx, cluster, flags.Kubeconfig) + err = deleteCluster(ctx, cluster, flags.Kubeconfig, flags.Force) if err != nil { return err } } } else { - err = deleteCluster(ctx, flags.Name, flags.Kubeconfig) + err = deleteCluster(ctx, flags.Name, flags.Kubeconfig, flags.Force) if err != nil { return err } @@ -85,7 +86,7 @@ func runE(ctx context.Context, flags *flagpole) error { return nil } -func deleteCluster(ctx context.Context, clusterName string, kubeconfigPath string) error { +func deleteCluster(ctx context.Context, clusterName string, kubeconfigPath string, force bool) error { name := config.ClusterName(clusterName) workdir := path.Join(config.ClustersDir, clusterName) @@ -108,6 +109,13 @@ func deleteCluster(ctx context.Context, clusterName string, kubeconfigPath strin return err } + if err := rt.Available(ctx); err != nil { + if !force { + return err + } + logger.Warn("Unavailable runtime but proceed with force delete", "err", err) + } + // Stop the cluster start := time.Now() logger.Info("Cluster is stopping") diff --git a/site/content/en/docs/generated/kwokctl_delete_cluster.md b/site/content/en/docs/generated/kwokctl_delete_cluster.md index 463a2d479..14a2423db 100644 --- a/site/content/en/docs/generated/kwokctl_delete_cluster.md +++ b/site/content/en/docs/generated/kwokctl_delete_cluster.md @@ -10,6 +10,7 @@ kwokctl delete cluster [flags] ``` --all Delete all clusters managed by kwokctl + --force Force delete the cluster -h, --help help for cluster --kubeconfig string The path to the kubeconfig file that will remove the deleted cluster (default "~/.kube/config") ```