Skip to content

Commit

Permalink
Enhanced forced deletion on karmadactl unjoin
Browse files Browse the repository at this point in the history
Signed-off-by: zhzhuang-zju <m17799853869@163.com>
  • Loading branch information
zhzhuang-zju committed Dec 19, 2023
1 parent 21b2842 commit 47cc75c
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion pkg/karmadactl/unjoin/unjoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"k8s.io/kubectl/pkg/util/templates"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
"github.com/karmada-io/karmada/pkg/karmadactl/options"
Expand Down Expand Up @@ -188,7 +189,19 @@ func (j *CommandUnjoinOption) RunUnJoinCluster(controlPlaneRestConfig, clusterCo
err := j.deleteClusterObject(controlPlaneKarmadaClient)
if err != nil {
klog.Errorf("Failed to delete cluster object. cluster name: %s, error: %v", j.ClusterName, err)
return err
if j.forceDeletion {
klog.Infof("Start forced deletion by remove work finalizer. cluster name: %s", j.ClusterName)
executionSpaceName := names.GenerateExecutionSpaceName(j.ClusterName)
err = removeWorkFinalizer(executionSpaceName, controlPlaneKarmadaClient)
if err != nil {
klog.Errorf("Failed to remove work's finalizer, error: %s", err)
return err
}
klog.Infof("Succeeded to remove work's finalizer. After confirming the success of the unjoin, manually delete remaining resources on the cluster %s.", j.ClusterName)
return nil
} else {
return err
}

Check failure on line 204 in pkg/karmadactl/unjoin/unjoin.go

View workflow job for this annotation

GitHub Actions / lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
}

// Attempt to delete the cluster role, cluster rolebindings and service account from the unjoining cluster
Expand Down Expand Up @@ -322,3 +335,25 @@ func deleteNamespaceFromUnjoinCluster(clusterKubeClient kubeclient.Interface, na

return nil
}

// removeWorkFinalizer remove the finalizer of works from the executionSpace
func removeWorkFinalizer(executionSpaceName string, controlPlaneKarmadaClient *karmadaclientset.Clientset) error {
list, err := controlPlaneKarmadaClient.WorkV1alpha1().Works(executionSpaceName).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to list work in executionSpace %s", executionSpaceName)
}

for i := range list.Items {
work := &list.Items[i]
if !controllerutil.ContainsFinalizer(work, util.ExecutionControllerFinalizer) {
continue
}

controllerutil.RemoveFinalizer(work, util.ExecutionControllerFinalizer)
_, err = controlPlaneKarmadaClient.WorkV1alpha1().Works(executionSpaceName).Update(context.TODO(), work, metav1.UpdateOptions{})
if err != nil {
return err
}
}
return nil
}

0 comments on commit 47cc75c

Please sign in to comment.