From 411239e786f61e85ff2b6818729415920ce9f880 Mon Sep 17 00:00:00 2001 From: zhzhuang-zju Date: Fri, 24 Jan 2025 11:09:39 +0800 Subject: [PATCH] Restrict the cluster sync mode for the unjoin and unregister command Signed-off-by: zhzhuang-zju --- pkg/karmadactl/unjoin/unjoin.go | 11 ++++++++++- pkg/karmadactl/unregister/unregister.go | 11 ++++++++++- pkg/karmadactl/util/cluster.go | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/pkg/karmadactl/unjoin/unjoin.go b/pkg/karmadactl/unjoin/unjoin.go index 7876c871fa07..b366b6f00095 100644 --- a/pkg/karmadactl/unjoin/unjoin.go +++ b/pkg/karmadactl/unjoin/unjoin.go @@ -28,6 +28,7 @@ import ( "k8s.io/klog/v2" "k8s.io/kubectl/pkg/util/templates" + clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned" "github.com/karmada-io/karmada/pkg/karmadactl/options" cmdutil "github.com/karmada-io/karmada/pkg/karmadactl/util" @@ -194,8 +195,16 @@ func (j *CommandUnjoinOption) RunUnJoinCluster(controlPlaneRestConfig, clusterCo controlPlaneKarmadaClient := controlPlaneKarmadaClientBuilder(controlPlaneRestConfig) controlPlaneKubeClient := controlPlaneKubeClientBuilder(controlPlaneRestConfig) + syncMode, err := cmdutil.GetClusterSyncMode(controlPlaneKarmadaClient, j.ClusterName) + if err != nil { + return err + } + if syncMode == clusterv1alpha1.Pull { + return fmt.Errorf("cluster %s is a %s mode member cluster, please use command `unregister` if you want to unregister cluster", j.ClusterName, syncMode) + } + // delete the cluster object in host cluster that associates the unjoining cluster - err := cmdutil.DeleteClusterObject(controlPlaneKubeClient, controlPlaneKarmadaClient, j.ClusterName, j.Wait, j.DryRun, j.forceDeletion) + err = cmdutil.DeleteClusterObject(controlPlaneKubeClient, controlPlaneKarmadaClient, j.ClusterName, j.Wait, j.DryRun, j.forceDeletion) if err != nil { klog.Errorf("Failed to delete cluster object. cluster name: %s, error: %v", j.ClusterName, err) return err diff --git a/pkg/karmadactl/unregister/unregister.go b/pkg/karmadactl/unregister/unregister.go index 353bb8df3b6d..367f00171874 100644 --- a/pkg/karmadactl/unregister/unregister.go +++ b/pkg/karmadactl/unregister/unregister.go @@ -35,6 +35,7 @@ import ( "k8s.io/klog/v2" "k8s.io/kubectl/pkg/util/templates" + clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" "github.com/karmada-io/karmada/pkg/apis/cluster/validation" karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned" "github.com/karmada-io/karmada/pkg/karmadactl/options" @@ -310,6 +311,14 @@ func (j *CommandUnregisterOption) getKarmadaAgentConfig(agent *appsv1.Deployment // RunUnregisterCluster unregister the pull mode cluster from karmada. func (j *CommandUnregisterOption) RunUnregisterCluster() error { + syncMode, err := cmdutil.GetClusterSyncMode(j.ControlPlaneClient, j.ClusterName) + if err != nil { + return err + } + if syncMode == clusterv1alpha1.Push { + return fmt.Errorf("cluster %s is a %s mode member cluster, please use command `unjoin` if you want to unregister cluster", j.ClusterName, syncMode) + } + if j.DryRun { return nil } @@ -318,7 +327,7 @@ func (j *CommandUnregisterOption) RunUnregisterCluster() error { // 1. delete the work object from the Karmada control plane // When deleting a cluster, the deletion triggers the removal of executionSpace, which can lead to the deletion of RBAC roles related to work. // Therefore, the deletion of work should be performed before deleting the cluster. - err := cmdutil.EnsureWorksDeleted(j.ControlPlaneClient, names.GenerateExecutionSpaceName(j.ClusterName), j.Wait) + err = cmdutil.EnsureWorksDeleted(j.ControlPlaneClient, names.GenerateExecutionSpaceName(j.ClusterName), j.Wait) if err != nil { klog.Errorf("Failed to delete works object. cluster name: %s, error: %v", j.ClusterName, err) return err diff --git a/pkg/karmadactl/util/cluster.go b/pkg/karmadactl/util/cluster.go index f5da3f4fab35..38089af0fdbb 100644 --- a/pkg/karmadactl/util/cluster.go +++ b/pkg/karmadactl/util/cluster.go @@ -29,6 +29,7 @@ import ( "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned" "github.com/karmada-io/karmada/pkg/util" "github.com/karmada-io/karmada/pkg/util/names" @@ -95,6 +96,19 @@ func DeleteClusterObject(controlPlaneKubeClient kubeclient.Interface, controlPla return err } +// GetClusterSyncMode return the sync mode of the member cluster. +func GetClusterSyncMode(controlPlaneKarmadaClient karmadaclientset.Interface, clusterName string) (clusterv1alpha1.ClusterSyncMode, error) { + target, err := controlPlaneKarmadaClient.ClusterV1alpha1().Clusters().Get(context.TODO(), clusterName, metav1.GetOptions{}) + if apierrors.IsNotFound(err) { + return clusterv1alpha1.Push, fmt.Errorf("no cluster object %s found in karmada control Plane", clusterName) + } + if err != nil { + klog.Errorf("Failed to get cluster object. cluster name: %s, error: %v", clusterName, err) + return clusterv1alpha1.Push, err + } + return target.Spec.SyncMode, nil +} + // removeWorkFinalizer removes the finalizer of works in the executionSpace. func removeWorkFinalizer(executionSpaceName string, controlPlaneKarmadaClient karmadaclientset.Interface) error { list, err := controlPlaneKarmadaClient.WorkV1alpha1().Works(executionSpaceName).List(context.TODO(), metav1.ListOptions{})