Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/clusterdiscovery: unit test clusterAPICluster #5777

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions pkg/clusterdiscovery/clusterapi/clusterapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package clusterapi

import (
"context"
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -55,6 +56,21 @@ var (
}
)

var (
cacheGenericListerBuilder = func(clusterDetector *ClusterDetector, objectGVR schema.GroupVersionResource, objectKey keys.ClusterWideKey) (runtime.Object, error) {
return clusterDetector.InformerManager.Lister(objectGVR).Get(objectKey.NamespaceKey())
}
joinClusterRunner = func(opts *join.CommandJoinOption, controlPlaneRestConfig, clusterConfig *rest.Config) error {
return opts.RunJoinCluster(controlPlaneRestConfig, clusterConfig)
}
unjoinClusterRunner = func(opts *unjoin.CommandUnjoinOption, controlPlaneRestConfig, clusterConfig *rest.Config) error {
return opts.RunUnJoinCluster(controlPlaneRestConfig, clusterConfig)
}
kubeconfigFileGenerator = func(clusterName string, kubeconfigData []byte) (string, error) {
return generateKubeconfigFile(clusterName, kubeconfigData)
}
)

// ClusterDetector is a cluster watcher which watched cluster object in cluster-api management cluster and reconcile the events.
type ClusterDetector struct {
ControllerPlaneConfig *rest.Config
Expand Down Expand Up @@ -123,8 +139,9 @@ func (d *ClusterDetector) OnDelete(obj interface{}) {
func (d *ClusterDetector) Reconcile(key util.QueueKey) error {
clusterWideKey, ok := key.(keys.ClusterWideKey)
if !ok {
klog.Errorf("Invalid key")
return fmt.Errorf("invalid key")
errMsg := fmt.Sprintf("expected keys.ClusterWideKey, but got %T type", key)
klog.Error(errMsg)
return errors.New(errMsg)
}
klog.Infof("Reconciling cluster-api object: %s", clusterWideKey)

Expand Down Expand Up @@ -158,7 +175,7 @@ func (d *ClusterDetector) GetUnstructuredObject(objectKey keys.ClusterWideKey) (
Resource: resourceCluster,
}

object, err := d.InformerManager.Lister(objectGVR).Get(objectKey.NamespaceKey())
object, err := cacheGenericListerBuilder(d, objectGVR, objectKey)
if err != nil {
if !apierrors.IsNotFound(err) {
klog.Errorf("Failed to get object(%s), error: %v", objectKey, err)
Expand Down Expand Up @@ -193,7 +210,7 @@ func (d *ClusterDetector) joinClusterAPICluster(clusterWideKey keys.ClusterWideK
return err
}

kubeconfigPath, err := generateKubeconfigFile(clusterWideKey.Name, secret.Data[secretutil.KubeconfigDataName])
kubeconfigPath, err := kubeconfigFileGenerator(clusterWideKey.Name, secret.Data[secretutil.KubeconfigDataName])
if err != nil {
return err
}
Expand All @@ -207,7 +224,7 @@ func (d *ClusterDetector) joinClusterAPICluster(clusterWideKey keys.ClusterWideK
ClusterNamespace: options.DefaultKarmadaClusterNamespace,
ClusterName: clusterWideKey.Name,
}
err = opts.RunJoinCluster(d.ControllerPlaneConfig, clusterRestConfig)
err = joinClusterRunner(&opts, d.ControllerPlaneConfig, clusterRestConfig)
if err != nil {
klog.Errorf("Failed to join cluster-api's cluster(%s): %v", clusterWideKey.Name, err)
return err
Expand All @@ -225,7 +242,7 @@ func (d *ClusterDetector) unJoinClusterAPICluster(clusterName string) error {
ClusterName: clusterName,
Wait: options.DefaultKarmadactlCommandDuration,
}
err := opts.RunUnJoinCluster(d.ControllerPlaneConfig, nil)
err := unjoinClusterRunner(&opts, d.ControllerPlaneConfig, nil)
if err != nil {
klog.Errorf("Failed to unJoin cluster-api's cluster(%s): %v", clusterName, err)
return err
Expand Down
Loading
Loading