@@ -17,6 +17,7 @@ limitations under the License.
1717package join
1818
1919import (
20+ "errors"
2021 "fmt"
2122 "strings"
2223
@@ -135,10 +136,10 @@ func (j *CommandJoinOption) Complete(args []string) error {
135136// Validate checks option and return a slice of found errs.
136137func (j * CommandJoinOption ) Validate (args []string ) error {
137138 if len (args ) > 1 {
138- return fmt . Errorf ("only the cluster name is allowed as an argument" )
139+ return errors . New ("only the cluster name is allowed as an argument" )
139140 }
140141 if len (j .ClusterName ) == 0 {
141- return fmt . Errorf ("cluster name is required" )
142+ return errors . New ("cluster name is required" )
142143 }
143144 if errMsgs := validation .ValidateClusterName (j .ClusterName ); len (errMsgs ) != 0 {
144145 return fmt .Errorf ("invalid cluster name(%s): %s" , j .ClusterName , strings .Join (errMsgs , ";" ))
@@ -196,11 +197,21 @@ func (j *CommandJoinOption) Run(f cmdutil.Factory) error {
196197 return j .RunJoinCluster (controlPlaneRestConfig , clusterConfig )
197198}
198199
200+ var controlPlaneKubeClientBuilder = func (controlPlaneRestConfig * rest.Config ) kubeclient.Interface {
201+ return kubeclient .NewForConfigOrDie (controlPlaneRestConfig )
202+ }
203+ var karmadaClientBuilder = func (controlPlaneRestConfig * rest.Config ) karmadaclientset.Interface {
204+ return karmadaclientset .NewForConfigOrDie (controlPlaneRestConfig )
205+ }
206+ var clusterKubeClientBuilder = func (clusterConfig * rest.Config ) kubeclient.Interface {
207+ return kubeclient .NewForConfigOrDie (clusterConfig )
208+ }
209+
199210// RunJoinCluster join the cluster into karmada.
200211func (j * CommandJoinOption ) RunJoinCluster (controlPlaneRestConfig , clusterConfig * rest.Config ) (err error ) {
201- controlPlaneKubeClient := kubeclient . NewForConfigOrDie (controlPlaneRestConfig )
202- karmadaClient := karmadaclientset . NewForConfigOrDie (controlPlaneRestConfig )
203- clusterKubeClient := kubeclient . NewForConfigOrDie (clusterConfig )
212+ controlPlaneKubeClient := controlPlaneKubeClientBuilder (controlPlaneRestConfig )
213+ karmadaClient := karmadaClientBuilder (controlPlaneRestConfig )
214+ clusterKubeClient := clusterKubeClientBuilder (clusterConfig )
204215
205216 klog .V (1 ).Infof ("Joining cluster config. endpoint: %s" , clusterConfig .Host )
206217
@@ -288,7 +299,7 @@ func generateClusterInControllerPlane(opts util.ClusterRegisterOption) (*cluster
288299 clusterObj .Spec .ProxyURL = url .String ()
289300 }
290301
291- controlPlaneKarmadaClient := karmadaclientset . NewForConfigOrDie (opts .ControlPlaneConfig )
302+ controlPlaneKarmadaClient := karmadaClientBuilder (opts .ControlPlaneConfig )
292303 cluster , err := util .CreateClusterObject (controlPlaneKarmadaClient , clusterObj )
293304 if err != nil {
294305 return nil , fmt .Errorf ("failed to create cluster(%s) object. error: %v" , opts .ClusterName , err )
0 commit comments