Skip to content

Commit

Permalink
🌱 Use a Deferred (cached) RESTMapper in ClusterCacheTracker
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <vincepri@vmware.com>
  • Loading branch information
vincepri committed Sep 11, 2020
1 parent feaff31 commit e5580ec
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions controllers/remote/cluster_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ import (
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
Expand All @@ -58,6 +62,7 @@ type clusterCache struct {
cache.Cache

lock sync.Mutex
mapper meta.RESTMapper
stopped bool
stop chan struct{}
}
Expand Down Expand Up @@ -272,7 +277,7 @@ func (m *ClusterCacheTracker) newDelegatingClient(ctx context.Context, cluster c
return nil, errors.Wrap(err, "error fetching REST client config for remote cluster")
}
config.Timeout = defaultClientTimeout
c, err := client.New(config, client.Options{Scheme: m.scheme})
c, err := client.New(config, client.Options{Scheme: m.scheme, Mapper: cache.mapper})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -326,10 +331,12 @@ func (m *ClusterCacheTracker) newClusterCache(ctx context.Context, cluster clien
return nil, errors.Wrap(err, "error fetching REST client config for remote cluster")
}

mapper, err := apiutil.NewDynamicRESTMapper(config)
// Use a delegating self-refreshing cached client instead of a static one.
delegate, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, errors.Wrap(err, "error creating dynamic rest mapper for remote cluster")
return nil, errors.Wrap(err, "error creating discovery client config for remote cluster")
}
mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(delegate))

cacheOptions := cache.Options{
Scheme: m.scheme,
Expand All @@ -342,8 +349,9 @@ func (m *ClusterCacheTracker) newClusterCache(ctx context.Context, cluster clien
stop := make(chan struct{})

cc := &clusterCache{
Cache: remoteCache,
stop: stop,
Cache: remoteCache,
stop: stop,
mapper: mapper,
}
m.clusterCaches[cluster] = cc

Expand Down

0 comments on commit e5580ec

Please sign in to comment.