@@ -29,9 +29,12 @@ import (
2929 cmdutil "k8s.io/kubectl/pkg/cmd/util"
3030 "k8s.io/kubectl/pkg/util/templates"
3131
32+ appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
3233 appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
34+ "github.com/apecloud/kubeblocks/pkg/constant"
3335
3436 "github.com/apecloud/kbcli/pkg/action"
37+ "github.com/apecloud/kbcli/pkg/printer"
3538 "github.com/apecloud/kbcli/pkg/types"
3639 "github.com/apecloud/kbcli/pkg/util"
3740)
4043 deleteExample = templates .Examples (`
4144 # delete a cluster named mycluster
4245 kbcli cluster delete mycluster
46+
4347 # delete a cluster by label selector
4448 kbcli cluster delete --selector clusterdefinition.kubeblocks.io/name=apecloud-mysql
49+
50+ # delete a cluster named mycluster forcedly
51+ kbcli cluster delete mycluster --force
4552` )
4653)
4754
@@ -80,9 +87,25 @@ func clusterPreDeleteHook(o *action.DeleteOptions, object runtime.Object) error
8087 if err != nil {
8188 return err
8289 }
83- if cluster .Spec .TerminationPolicy == appsv1alpha1 .DoNotTerminate {
90+ if cluster .Spec .TerminationPolicy == appsv1 .DoNotTerminate {
8491 return fmt .Errorf ("cluster %s is protected by termination policy %s, skip deleting" , cluster .Name , appsv1alpha1 .DoNotTerminate )
8592 }
93+
94+ if o .Force {
95+ fmt .Fprintf (o .Out , printer .BoldRed ("WARNING: Using --force may lead to potential data loss or residual dirty data if the cluster depends on other clusters.\n " ))
96+ components := util .GetComponentsOrShards (cluster )
97+ for _ , componentName := range components {
98+ dynamicClient , err := o .Factory .DynamicClient ()
99+ if err != nil {
100+ return err
101+ }
102+ err = util .AddAnnotationToComponentOrShard (dynamicClient , constant .GenerateClusterComponentName (cluster .Name , componentName ), cluster .Namespace , constant .SkipPreTerminateAnnotationKey , "true" )
103+ if err != nil {
104+ return fmt .Errorf ("failed to add annotation to component %s: %v" , componentName , err )
105+ }
106+ }
107+ }
108+
86109 return nil
87110}
88111
@@ -96,12 +119,12 @@ func clusterPostDeleteHook(o *action.DeleteOptions, object runtime.Object) error
96119 return nil
97120}
98121
99- func getClusterFromObject (object runtime.Object ) (* appsv1alpha1 .Cluster , error ) {
100- if object .GetObjectKind ().GroupVersionKind ().Kind != appsv1alpha1 .ClusterKind {
122+ func getClusterFromObject (object runtime.Object ) (* appsv1 .Cluster , error ) {
123+ if object .GetObjectKind ().GroupVersionKind ().Kind != appsv1 .ClusterKind {
101124 return nil , fmt .Errorf ("object %s is not of kind %s" , object .GetObjectKind ().GroupVersionKind ().Kind , appsv1alpha1 .ClusterKind )
102125 }
103126 u := object .(* unstructured.Unstructured )
104- cluster := & appsv1alpha1 .Cluster {}
127+ cluster := & appsv1 .Cluster {}
105128 if err := runtime .DefaultUnstructuredConverter .FromUnstructured (u .Object , cluster ); err != nil {
106129 return nil , err
107130 }
0 commit comments