Skip to content

Commit

Permalink
Feat: skip ocm cluster watch when not installed
Browse files Browse the repository at this point in the history
Signed-off-by: Yin Da <[email protected]>
  • Loading branch information
Somefive committed Mar 28, 2023
1 parent 69d4f88 commit 98ce2b2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
21 changes: 21 additions & 0 deletions pkg/util/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package cluster

import (
"context"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
ocmclient "open-cluster-management.io/api/client/cluster/clientset/versioned"
clusterv1Lister "open-cluster-management.io/api/client/cluster/listers/cluster/v1"
clusterv1 "open-cluster-management.io/api/cluster/v1"
Expand Down Expand Up @@ -63,3 +66,21 @@ func (c *cacheOCMClusterControl) Get(ctx context.Context, name string) (*cluster
func (c *cacheOCMClusterControl) List(ctx context.Context) ([]*clusterv1.ManagedCluster, error) {
return c.clusterLister.List(labels.Everything())
}

// IsOCMManagedClusterInstalled check if managed cluster is installed in the cluster
func IsOCMManagedClusterInstalled(ocmClient ocmclient.Interface) (bool, error) {
_, resources, err := ocmClient.Discovery().ServerGroupsAndResources()
if err != nil {
return false, fmt.Errorf("unable to get api-resources: %w", err)
}
for _, resource := range resources {
if gv, _ := schema.ParseGroupVersion(resource.GroupVersion); gv.Group == clusterv1.GroupName {
for _, rsc := range resource.APIResources {
if rsc.Kind == "ManagedCluster" {
return true, nil
}
}
}
}
return false, nil
}
8 changes: 7 additions & 1 deletion pkg/util/singleton/loopback.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package singleton
import (
"time"

"k8s.io/klog/v2"
controllerruntimeconfig "sigs.k8s.io/controller-runtime/pkg/client/config"

"github.com/oam-dev/cluster-gateway/pkg/config"
Expand Down Expand Up @@ -77,7 +78,12 @@ func InitLoopbackClient(ctx server.PostStartHookContext) error {
}

if utilfeature.DefaultMutableFeatureGate.Enabled(featuregates.OCMClusterCache) {
if err := setOCMClusterInformer(ocmClient, ctx.StopCh); err != nil {
installed, err := clusterutil.IsOCMManagedClusterInstalled(ocmClient)
if err != nil {
klog.Error(err)
} else if !installed {
klog.Infof("OCM ManagedCluster CRD not installed, skip bootstrapping informer for OCM ManagedCluster")
} else if err := setOCMClusterInformer(ocmClient, ctx.StopCh); err != nil {
return err
}
clusterControl = clusterutil.NewCacheOCMClusterControl(clusterLister)
Expand Down

0 comments on commit 98ce2b2

Please sign in to comment.