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 <[email protected]>
  • Loading branch information
zhzhuang-zju committed Dec 19, 2023
1 parent 21b2842 commit d3fda20
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 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
}
}

// Attempt to delete the cluster role, cluster rolebindings and service account from the unjoining cluster
Expand Down Expand Up @@ -322,3 +335,24 @@ 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 _, item := range list.Items {
if !controllerutil.ContainsFinalizer(&item, util.ExecutionControllerFinalizer) {

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

View workflow job for this annotation

GitHub Actions / lint

G601: Implicit memory aliasing in for loop. (gosec)
continue
}

controllerutil.RemoveFinalizer(&item, util.ExecutionControllerFinalizer)

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

View workflow job for this annotation

GitHub Actions / lint

G601: Implicit memory aliasing in for loop. (gosec)
_, err = controlPlaneKarmadaClient.WorkV1alpha1().Works(executionSpaceName).Update(context.TODO(), &item, metav1.UpdateOptions{})

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

View workflow job for this annotation

GitHub Actions / lint

G601: Implicit memory aliasing in for loop. (gosec)
if err != nil {
return err
}
}
return nil
}

0 comments on commit d3fda20

Please sign in to comment.