Extract join| unjoin parameters and pass struct to avoid too many parameters in the signature

Signed-off-by: wawa0210 <xiaozhang0210@hotmail.com>
This commit is contained in:
wawa0210 2021-10-30 13:04:04 +08:00
parent e6d7ce369d
commit a47fdfd73d
No known key found for this signature in database
GPG Key ID: 900C83A2C098B3B1
3 changed files with 34 additions and 21 deletions

View File

@ -173,8 +173,14 @@ func (d *ClusterDetector) joinClusterAPICluster(clusterWideKey keys.ClusterWideK
if err != nil {
klog.Fatalf("Failed to get cluster-api management cluster rest config. kubeconfig: %s, err: %v", kubeconfigPath, err)
}
err = karmadactl.JoinCluster(d.ControllerPlaneConfig, clusterRestConfig, options.DefaultKarmadaClusterNamespace, clusterWideKey.Name, false)
opts := karmadactl.CommandJoinOption{
GlobalCommandOptions: options.GlobalCommandOptions{
ClusterNamespace: options.DefaultKarmadaClusterNamespace,
DryRun: false,
},
ClusterName: clusterWideKey.Name,
}
err = karmadactl.JoinCluster(d.ControllerPlaneConfig, clusterRestConfig, opts)
if err != nil {
klog.Errorf("Failed to join cluster-api's cluster(%s): %v", clusterWideKey.Name, err)
return err
@ -186,7 +192,14 @@ func (d *ClusterDetector) joinClusterAPICluster(clusterWideKey keys.ClusterWideK
func (d *ClusterDetector) unJoinClusterAPICluster(clusterName string) error {
klog.Infof("Begin to unJoin cluster-api's Cluster(%s) to karmada", clusterName)
err := karmadactl.UnJoinCluster(d.ControllerPlaneConfig, nil, options.DefaultKarmadaClusterNamespace, clusterName, false, false)
opts := karmadactl.CommandUnjoinOption{
GlobalCommandOptions: options.GlobalCommandOptions{
ClusterNamespace: options.DefaultKarmadaClusterNamespace,
DryRun: false,
},
ClusterName: clusterName,
}
err := karmadactl.UnJoinCluster(d.ControllerPlaneConfig, nil, opts)
if err != nil {
klog.Errorf("Failed to unJoin cluster-api's cluster(%s): %v", clusterName, err)
return err

View File

@ -160,35 +160,35 @@ func RunJoin(cmdOut io.Writer, karmadaConfig KarmadaConfig, opts CommandJoinOpti
return err
}
return JoinCluster(controlPlaneRestConfig, clusterConfig, opts.ClusterNamespace, opts.ClusterName, opts.DryRun)
return JoinCluster(controlPlaneRestConfig, clusterConfig, opts)
}
// JoinCluster join the cluster into karmada.
func JoinCluster(controlPlaneRestConfig, clusterConfig *rest.Config, clusterNamespace, clusterName string, dryRun bool) (err error) {
func JoinCluster(controlPlaneRestConfig, clusterConfig *rest.Config, opts CommandJoinOption) (err error) {
controlPlaneKubeClient := kubeclient.NewForConfigOrDie(controlPlaneRestConfig)
clusterKubeClient := kubeclient.NewForConfigOrDie(clusterConfig)
klog.V(1).Infof("joining cluster config. endpoint: %s", clusterConfig.Host)
// ensure namespace where the cluster object be stored exists in control plane.
if _, err = ensureNamespaceExist(controlPlaneKubeClient, clusterNamespace, dryRun); err != nil {
if _, err = ensureNamespaceExist(controlPlaneKubeClient, opts.ClusterNamespace, opts.DryRun); err != nil {
return err
}
clusterSecret, err := generateSecretInMemberCluster(clusterKubeClient, clusterNamespace, clusterName, dryRun)
clusterSecret, err := generateSecretInMemberCluster(clusterKubeClient, opts.ClusterNamespace, opts.ClusterName, opts.DryRun)
if err != nil {
return err
}
if dryRun {
if opts.DryRun {
return nil
}
// create secret in control plane
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: clusterNamespace,
Name: clusterName,
Namespace: opts.ClusterNamespace,
Name: opts.ClusterName,
},
Data: map[string][]byte{
clusterv1alpha1.SecretCADataKey: clusterSecret.Data["ca.crt"],
@ -201,7 +201,7 @@ func JoinCluster(controlPlaneRestConfig, clusterConfig *rest.Config, clusterName
return err
}
cluster, err := generateClusterInControllerPlane(controlPlaneRestConfig, clusterConfig, clusterName, *secret)
cluster, err := generateClusterInControllerPlane(controlPlaneRestConfig, clusterConfig, opts.ClusterName, *secret)
if err != nil {
return err
}

View File

@ -125,17 +125,17 @@ func RunUnjoin(cmdOut io.Writer, karmadaConfig KarmadaConfig, opts CommandUnjoin
}
}
return UnJoinCluster(controlPlaneRestConfig, clusterConfig, opts.ClusterNamespace, opts.ClusterName, opts.forceDeletion, opts.DryRun)
return UnJoinCluster(controlPlaneRestConfig, clusterConfig, opts)
}
// UnJoinCluster unJoin the cluster from karmada.
func UnJoinCluster(controlPlaneRestConfig, clusterConfig *rest.Config, clusterNamespace, clusterName string, forceDeletion, dryRun bool) (err error) {
func UnJoinCluster(controlPlaneRestConfig, clusterConfig *rest.Config, opts CommandUnjoinOption) (err error) {
controlPlaneKarmadaClient := karmadaclientset.NewForConfigOrDie(controlPlaneRestConfig)
// delete the cluster object in host cluster that associates the unjoining cluster
err = deleteClusterObject(controlPlaneKarmadaClient, clusterName, dryRun)
err = deleteClusterObject(controlPlaneKarmadaClient, opts.ClusterName, opts.DryRun)
if err != nil {
klog.Errorf("Failed to delete cluster object. cluster name: %s, error: %v", clusterName, err)
klog.Errorf("Failed to delete cluster object. cluster name: %s, error: %v", opts.ClusterName, err)
return err
}
@ -147,23 +147,23 @@ func UnJoinCluster(controlPlaneRestConfig, clusterConfig *rest.Config, clusterNa
klog.V(1).Infof("unjoining cluster config. endpoint: %s", clusterConfig.Host)
// delete RBAC resource from unjoining cluster
err = deleteRBACResources(clusterKubeClient, clusterName, forceDeletion, dryRun)
err = deleteRBACResources(clusterKubeClient, opts.ClusterName, opts.forceDeletion, opts.DryRun)
if err != nil {
klog.Errorf("Failed to delete RBAC resource in unjoining cluster %q: %v", clusterName, err)
klog.Errorf("Failed to delete RBAC resource in unjoining cluster %q: %v", opts.ClusterName, err)
return err
}
// delete service account from unjoining cluster
err = deleteServiceAccount(clusterKubeClient, clusterNamespace, clusterName, forceDeletion, dryRun)
err = deleteServiceAccount(clusterKubeClient, opts.ClusterNamespace, opts.ClusterName, opts.forceDeletion, opts.DryRun)
if err != nil {
klog.Errorf("Failed to delete service account in unjoining cluster %q: %v", clusterName, err)
klog.Errorf("Failed to delete service account in unjoining cluster %q: %v", opts.ClusterName, err)
return err
}
// delete namespace from unjoining cluster
err = deleteNamespaceFromUnjoinCluster(clusterKubeClient, clusterNamespace, clusterName, forceDeletion, dryRun)
err = deleteNamespaceFromUnjoinCluster(clusterKubeClient, opts.ClusterNamespace, opts.ClusterName, opts.forceDeletion, opts.DryRun)
if err != nil {
klog.Errorf("Failed to delete namespace in unjoining cluster %q: %v", clusterName, err)
klog.Errorf("Failed to delete namespace in unjoining cluster %q: %v", opts.ClusterName, err)
return err
}
}