Skip to content

Commit

Permalink
pkg/search: unit test controller
Browse files Browse the repository at this point in the history
In this commit, we unit test karmada search controller
on creating new controller and handling the following events
watched by the shared informer: `addCluster`, `updateCluster`,
`deleteCluster`, `addResourceRegistry`, `updateResourceRegistry`,
and `deleteResourceRegistry` making sure those handled and cached
correctly to the indexer.

Signed-off-by: Mohamed Awnallah <mohamedmohey2352@gmail.com>
  • Loading branch information
mohamedawnallah committed Nov 16, 2024
1 parent c76add3 commit 93f07b5
Show file tree
Hide file tree
Showing 2 changed files with 772 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/search/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"

clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
Expand Down Expand Up @@ -349,6 +350,13 @@ func (c *Controller) getRegistryBackendHandler(cluster string, matchedRegistries
return handler, nil
}

var controlPlaneClientBuilder = func(restConfig *rest.Config) client.Client {
return gclient.NewForConfigOrDie(restConfig)
}
var clusterDynamicClientBuilder = func(cluster string, controlPlaneClient client.Client) (*util.DynamicClusterClient, error) {
return util.NewClusterDynamicClientSet(cluster, controlPlaneClient)
}

// doCacheCluster processes the resourceRegistry object
// TODO: update status
func (c *Controller) doCacheCluster(cluster string) error {
Expand Down Expand Up @@ -386,9 +394,9 @@ func (c *Controller) doCacheCluster(cluster string) error {
// STEP2: added/updated cluster, builds an informer manager for a specific cluster.
if !c.InformerManager.IsManagerExist(cluster) {
klog.Info("Try to build informer manager for cluster ", cluster)
controlPlaneClient := gclient.NewForConfigOrDie(c.restConfig)
controlPlaneClient := controlPlaneClientBuilder(c.restConfig)

clusterDynamicClient, err := util.NewClusterDynamicClientSet(cluster, controlPlaneClient)
clusterDynamicClient, err := clusterDynamicClientBuilder(cluster, controlPlaneClient)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 93f07b5

Please sign in to comment.