Skip to content

Commit

Permalink
Merge pull request #11445 from sbueringer/pr-cc-context-fixes
Browse files Browse the repository at this point in the history
🐛 Improve context handling in ClusterCache
  • Loading branch information
k8s-ci-robot authored Nov 19, 2024
2 parents 56e5338 + 5a345f1 commit 806c284
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions controllers/clustercache/cluster_accessor_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ func createCachedClient(ctx context.Context, clusterAccessorConfig *clusterAcces
return nil, nil, errors.Wrapf(err, "error creating cache")
}

cacheCtx, cacheCtxCancel := context.WithCancel(ctx)
// Use a context that is independent of the passed in context, so the cache doesn't get stopped
// when the passed in context is canceled.
cacheCtx, cacheCtxCancel := context.WithCancel(context.Background())

// We need to be able to stop the cache's shared informers, so wrap this in a stoppableCache.
cache := &stoppableCache{
Expand Down Expand Up @@ -263,7 +265,7 @@ func createCachedClient(ctx context.Context, clusterAccessorConfig *clusterAcces
defer cacheSyncCtxCancel()
if !cache.WaitForCacheSync(cacheSyncCtx) {
cache.Stop()
return nil, nil, errors.Wrapf(cacheCtx.Err(), "error when waiting for cache to sync")
return nil, nil, fmt.Errorf("error when waiting for cache to sync: %w", cacheSyncCtx.Err())
}

// Wrap the cached client with a client that sets timeouts on all Get and List calls
Expand Down
6 changes: 4 additions & 2 deletions controllers/remote/cluster_cache_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,9 @@ func (t *ClusterCacheTracker) createCachedClient(ctx context.Context, config *re
return nil, errors.Wrapf(err, "error creating cached client for remote cluster %q: error creating cache", cluster.String())
}

cacheCtx, cacheCtxCancel := context.WithCancel(ctx)
// Use a context that is independent of the passed in context, so the cache doesn't get stopped
// when the passed in context is canceled.
cacheCtx, cacheCtxCancel := context.WithCancel(context.Background())

// We need to be able to stop the cache's shared informers, so wrap this in a stoppableCache.
cache := &stoppableCache{
Expand Down Expand Up @@ -549,7 +551,7 @@ func (t *ClusterCacheTracker) createCachedClient(ctx context.Context, config *re
defer cacheSyncCtxCancel()
if !cache.WaitForCacheSync(cacheSyncCtx) {
cache.Stop()
return nil, fmt.Errorf("failed waiting for cache for remote cluster %v to sync: %w", cluster, cacheCtx.Err())
return nil, fmt.Errorf("failed waiting for cache for remote cluster %v to sync: %w", cluster, cacheSyncCtx.Err())
}

// Wrap the cached client with a client that sets timeouts on all Get and List calls
Expand Down

0 comments on commit 806c284

Please sign in to comment.