Skip to content

Commit 1b5e6b8

Browse files
authored
Merge pull request #11707 from chrischdi/pr-cc-cache-map-rw
🐛 clustercache: Prevent concurrent map read/write when creating a cache
2 parents ba7102f + 3aa7711 commit 1b5e6b8

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

controllers/clustercache/cluster_accessor_client.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package clustercache
1919
import (
2020
"context"
2121
"fmt"
22+
"maps"
2223
"net/http"
2324
"sync"
2425
"time"
@@ -213,13 +214,19 @@ func createUncachedClient(scheme *runtime.Scheme, config *rest.Config, httpClien
213214

214215
// createCachedClient creates a cached client for the given cluster, based on the rest.Config.
215216
func createCachedClient(ctx context.Context, clusterAccessorConfig *clusterAccessorConfig, config *rest.Config, httpClient *http.Client, mapper meta.RESTMapper) (client.Client, *stoppableCache, error) {
216-
// Create the cache for the cluster.
217+
// The byObject map needs to be cloned to not hit concurrent read/writes on the Namespaces map.
218+
byObject := maps.Clone(clusterAccessorConfig.Cache.ByObject)
219+
for k, v := range byObject {
220+
v.Namespaces = maps.Clone(v.Namespaces)
221+
byObject[k] = v
222+
}
223+
217224
cacheOptions := cache.Options{
218225
HTTPClient: httpClient,
219226
Scheme: clusterAccessorConfig.Scheme,
220227
Mapper: mapper,
221228
SyncPeriod: clusterAccessorConfig.Cache.SyncPeriod,
222-
ByObject: clusterAccessorConfig.Cache.ByObject,
229+
ByObject: byObject,
223230
}
224231
remoteCache, err := cache.New(config, cacheOptions)
225232
if err != nil {

0 commit comments

Comments
 (0)