@@ -278,13 +278,14 @@ func (t *ClusterCacheTracker) newClusterAccessor(ctx context.Context, cluster cl
278
278
}
279
279
280
280
// Create a client and a cache for the cluster.
281
- c , cache , err := t .createClient (ctx , config , cluster , indexes )
281
+ c , uncachedClient , cache , err := t .createClient (ctx , config , cluster , indexes )
282
282
if err != nil {
283
283
return nil , err
284
284
}
285
285
286
286
// Detect if the controller is running on the workload cluster.
287
- runningOnCluster , err := t .runningOnWorkloadCluster (ctx , c , cluster )
287
+ // This function uses an uncached client to ensure pods aren't cached by the long-lived client.
288
+ runningOnCluster , err := t .runningOnWorkloadCluster (ctx , uncachedClient , cluster )
288
289
if err != nil {
289
290
return nil , err
290
291
}
@@ -303,7 +304,7 @@ func (t *ClusterCacheTracker) newClusterAccessor(ctx context.Context, cluster cl
303
304
config .Host = inClusterConfig .Host
304
305
305
306
// Create a new client and overwrite the previously created client.
306
- c , cache , err = t .createClient (ctx , config , cluster , indexes )
307
+ c , _ , cache , err = t .createClient (ctx , config , cluster , indexes )
307
308
if err != nil {
308
309
return nil , errors .Wrap (err , "error creating client for self-hosted cluster" )
309
310
}
@@ -355,26 +356,26 @@ func (t *ClusterCacheTracker) runningOnWorkloadCluster(ctx context.Context, c cl
355
356
return t .controllerPodMetadata .UID == pod .UID , nil
356
357
}
357
358
358
- // createClient creates a client and a mapper based on a rest.Config.
359
- func (t * ClusterCacheTracker ) createClient (ctx context.Context , config * rest.Config , cluster client.ObjectKey , indexes []Index ) (client.Client , * stoppableCache , error ) {
359
+ // createClient creates a cached client, and uncached client and a mapper based on a rest.Config.
360
+ func (t * ClusterCacheTracker ) createClient (ctx context.Context , config * rest.Config , cluster client.ObjectKey , indexes []Index ) (client.Client , client. Client , * stoppableCache , error ) {
360
361
// Create a http client for the cluster.
361
362
httpClient , err := rest .HTTPClientFor (config )
362
363
if err != nil {
363
- return nil , nil , errors .Wrapf (err , "error creating client for remote cluster %q: error creating http client" , cluster .String ())
364
+ return nil , nil , nil , errors .Wrapf (err , "error creating client for remote cluster %q: error creating http client" , cluster .String ())
364
365
}
365
366
366
367
// Create a mapper for it
367
368
mapper , err := apiutil .NewDynamicRESTMapper (config , httpClient )
368
369
if err != nil {
369
- return nil , nil , errors .Wrapf (err , "error creating client for remote cluster %q: error creating dynamic rest mapper" , cluster .String ())
370
+ return nil , nil , nil , errors .Wrapf (err , "error creating client for remote cluster %q: error creating dynamic rest mapper" , cluster .String ())
370
371
}
371
372
372
373
// Verify if we can get a rest mapping from the workload cluster apiserver.
373
374
// Note: This also checks if the apiserver is up in general. We do this already here
374
375
// to avoid further effort creating a cache and a client and to produce a clearer error message.
375
376
_ , err = mapper .RESTMapping (corev1 .SchemeGroupVersion .WithKind ("Node" ).GroupKind (), corev1 .SchemeGroupVersion .Version )
376
377
if err != nil {
377
- return nil , nil , errors .Wrapf (err , "error creating client for remote cluster %q: error getting rest mapping" , cluster .String ())
378
+ return nil , nil , nil , errors .Wrapf (err , "error creating client for remote cluster %q: error getting rest mapping" , cluster .String ())
378
379
}
379
380
380
381
// Create the cache for the remote cluster
@@ -385,7 +386,7 @@ func (t *ClusterCacheTracker) createClient(ctx context.Context, config *rest.Con
385
386
}
386
387
remoteCache , err := cache .New (config , cacheOptions )
387
388
if err != nil {
388
- return nil , nil , errors .Wrapf (err , "error creating client for remote cluster %q: error creating cache" , cluster .String ())
389
+ return nil , nil , nil , errors .Wrapf (err , "error creating client for remote cluster %q: error creating cache" , cluster .String ())
389
390
}
390
391
391
392
cacheCtx , cacheCtxCancel := context .WithCancel (ctx )
@@ -398,12 +399,12 @@ func (t *ClusterCacheTracker) createClient(ctx context.Context, config *rest.Con
398
399
399
400
for _ , index := range indexes {
400
401
if err := cache .IndexField (ctx , index .Object , index .Field , index .ExtractValue ); err != nil {
401
- return nil , nil , errors .Wrapf (err , "error adding index for field %q to cache for remote cluster %q" , index .Field , cluster .String ())
402
+ return nil , nil , nil , errors .Wrapf (err , "error adding index for field %q to cache for remote cluster %q" , index .Field , cluster .String ())
402
403
}
403
404
}
404
405
405
406
// Create the client for the remote cluster
406
- c , err := client .New (config , client.Options {
407
+ cachedClient , err := client .New (config , client.Options {
407
408
Scheme : t .scheme ,
408
409
Mapper : mapper ,
409
410
HTTPClient : httpClient ,
@@ -414,9 +415,19 @@ func (t *ClusterCacheTracker) createClient(ctx context.Context, config *rest.Con
414
415
},
415
416
})
416
417
if err != nil {
417
- return nil , nil , errors .Wrapf (err , "error creating client for remote cluster %q" , cluster .String ())
418
+ return nil , nil , nil , errors .Wrapf (err , "error creating client for remote cluster %q" , cluster .String ())
418
419
}
419
420
421
+ // Create an uncached client. This is used in `runningOnWorkloadCluster` to ensure we don't continuously cache
422
+ // pods in the client.
423
+ uncachedClient , err := client .New (config , client.Options {
424
+ Scheme : t .scheme ,
425
+ Mapper : mapper ,
426
+ HTTPClient : httpClient ,
427
+ })
428
+ if err != nil {
429
+ return nil , nil , nil , errors .Wrapf (err , "error creating uncached client for remote cluster %q" , cluster .String ())
430
+ }
420
431
// Start the cache!!!
421
432
go cache .Start (cacheCtx ) //nolint:errcheck
422
433
@@ -425,7 +436,7 @@ func (t *ClusterCacheTracker) createClient(ctx context.Context, config *rest.Con
425
436
defer cacheSyncCtxCancel ()
426
437
if ! cache .WaitForCacheSync (cacheSyncCtx ) {
427
438
cache .Stop ()
428
- return nil , nil , fmt .Errorf ("failed waiting for cache for remote cluster %v to sync: %w" , cluster , cacheCtx .Err ())
439
+ return nil , nil , nil , fmt .Errorf ("failed waiting for cache for remote cluster %v to sync: %w" , cluster , cacheCtx .Err ())
429
440
}
430
441
431
442
// Start cluster healthcheck!!!
@@ -435,7 +446,7 @@ func (t *ClusterCacheTracker) createClient(ctx context.Context, config *rest.Con
435
446
httpClient : httpClient ,
436
447
})
437
448
438
- return c , cache , nil
449
+ return cachedClient , uncachedClient , cache , nil
439
450
}
440
451
441
452
// deleteAccessor stops a clusterAccessor's cache and removes the clusterAccessor from the tracker.
0 commit comments