Skip to content

Commit e8d4784

Browse files
authored
Merge pull request #10606 from sbueringer/pr-cct-improv
🌱 ClusterCacheTracker: use indexes field instead of passing it around
2 parents 9dc9c8f + 77e6c1d commit e8d4784

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

controllers/remote/cluster_cache_tracker.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func NewClusterCacheTracker(manager ctrl.Manager, options ClusterCacheTrackerOpt
181181

182182
// GetClient returns a cached client for the given cluster.
183183
func (t *ClusterCacheTracker) GetClient(ctx context.Context, cluster client.ObjectKey) (client.Client, error) {
184-
accessor, err := t.getClusterAccessor(ctx, cluster, t.indexes...)
184+
accessor, err := t.getClusterAccessor(ctx, cluster)
185185
if err != nil {
186186
return nil, err
187187
}
@@ -196,7 +196,7 @@ func (t *ClusterCacheTracker) GetReader(ctx context.Context, cluster client.Obje
196196

197197
// GetRESTConfig returns a cached REST config for the given cluster.
198198
func (t *ClusterCacheTracker) GetRESTConfig(ctc context.Context, cluster client.ObjectKey) (*rest.Config, error) {
199-
accessor, err := t.getClusterAccessor(ctc, cluster, t.indexes...)
199+
accessor, err := t.getClusterAccessor(ctc, cluster)
200200
if err != nil {
201201
return nil, err
202202
}
@@ -206,7 +206,7 @@ func (t *ClusterCacheTracker) GetRESTConfig(ctc context.Context, cluster client.
206206

207207
// GetEtcdClientCertificateKey returns a cached certificate key to be used for generating certificates for accessing etcd in the given cluster.
208208
func (t *ClusterCacheTracker) GetEtcdClientCertificateKey(ctx context.Context, cluster client.ObjectKey) (*rsa.PrivateKey, error) {
209-
accessor, err := t.getClusterAccessor(ctx, cluster, t.indexes...)
209+
accessor, err := t.getClusterAccessor(ctx, cluster)
210210
if err != nil {
211211
return nil, err
212212
}
@@ -254,7 +254,7 @@ func (t *ClusterCacheTracker) storeAccessor(cluster client.ObjectKey, accessor *
254254
// It then falls back to create a new clusterAccessor if needed.
255255
// If there is already another go routine trying to create a clusterAccessor
256256
// for the same cluster, an error is returned.
257-
func (t *ClusterCacheTracker) getClusterAccessor(ctx context.Context, cluster client.ObjectKey, indexes ...Index) (*clusterAccessor, error) {
257+
func (t *ClusterCacheTracker) getClusterAccessor(ctx context.Context, cluster client.ObjectKey) (*clusterAccessor, error) {
258258
log := ctrl.LoggerFrom(ctx, "cluster", klog.KRef(cluster.Namespace, cluster.Name))
259259

260260
// If the clusterAccessor already exists, return early.
@@ -279,7 +279,7 @@ func (t *ClusterCacheTracker) getClusterAccessor(ctx context.Context, cluster cl
279279

280280
// We are the go routine who has to initialize the clusterAccessor.
281281
log.V(4).Info("Creating new cluster accessor")
282-
accessor, err := t.newClusterAccessor(ctx, cluster, indexes...)
282+
accessor, err := t.newClusterAccessor(ctx, cluster)
283283
if err != nil {
284284
return nil, errors.Wrap(err, "failed to create cluster accessor")
285285
}
@@ -290,7 +290,7 @@ func (t *ClusterCacheTracker) getClusterAccessor(ctx context.Context, cluster cl
290290
}
291291

292292
// newClusterAccessor creates a new clusterAccessor.
293-
func (t *ClusterCacheTracker) newClusterAccessor(ctx context.Context, cluster client.ObjectKey, indexes ...Index) (*clusterAccessor, error) {
293+
func (t *ClusterCacheTracker) newClusterAccessor(ctx context.Context, cluster client.ObjectKey) (*clusterAccessor, error) {
294294
log := ctrl.LoggerFrom(ctx)
295295

296296
// Get a rest config for the remote cluster.
@@ -348,7 +348,7 @@ func (t *ClusterCacheTracker) newClusterAccessor(ctx context.Context, cluster cl
348348
}
349349

350350
// Create a client and a cache for the cluster.
351-
cachedClient, err := t.createCachedClient(ctx, config, cluster, httpClient, mapper, indexes)
351+
cachedClient, err := t.createCachedClient(ctx, config, cluster, httpClient, mapper)
352352
if err != nil {
353353
return nil, err
354354
}
@@ -442,7 +442,7 @@ type cachedClientOutput struct {
442442
}
443443

444444
// createCachedClient creates a cached client for the given cluster, based on a rest.Config.
445-
func (t *ClusterCacheTracker) createCachedClient(ctx context.Context, config *rest.Config, cluster client.ObjectKey, httpClient *http.Client, mapper meta.RESTMapper, indexes []Index) (*cachedClientOutput, error) {
445+
func (t *ClusterCacheTracker) createCachedClient(ctx context.Context, config *rest.Config, cluster client.ObjectKey, httpClient *http.Client, mapper meta.RESTMapper) (*cachedClientOutput, error) {
446446
// Create the cache for the remote cluster
447447
cacheOptions := cache.Options{
448448
HTTPClient: httpClient,
@@ -462,7 +462,7 @@ func (t *ClusterCacheTracker) createCachedClient(ctx context.Context, config *re
462462
cancelFunc: cacheCtxCancel,
463463
}
464464

465-
for _, index := range indexes {
465+
for _, index := range t.indexes {
466466
if err := cache.IndexField(ctx, index.Object, index.Field, index.ExtractValue); err != nil {
467467
return nil, errors.Wrapf(err, "error creating cached client for remote cluster %q: error adding index for field %q to cache", cluster.String(), index.Field)
468468
}
@@ -566,7 +566,7 @@ func (t *ClusterCacheTracker) Watch(ctx context.Context, input WatchInput) error
566566
return errors.New("input.Name is required")
567567
}
568568

569-
accessor, err := t.getClusterAccessor(ctx, input.Cluster, t.indexes...)
569+
accessor, err := t.getClusterAccessor(ctx, input.Cluster)
570570
if err != nil {
571571
return errors.Wrapf(err, "failed to add %s watch on cluster %s", input.Kind, klog.KRef(input.Cluster.Namespace, input.Cluster.Name))
572572
}

0 commit comments

Comments
 (0)