Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#11707 from chrischdi/pr-cc-cache-m…
Browse files Browse the repository at this point in the history
…ap-rw

🐛  clustercache: Prevent concurrent map read/write when creating a cache
  • Loading branch information
k8s-ci-robot authored Jan 20, 2025
2 parents ba7102f + 3aa7711 commit 1b5e6b8
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions controllers/clustercache/cluster_accessor_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package clustercache
import (
"context"
"fmt"
"maps"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -213,13 +214,19 @@ func createUncachedClient(scheme *runtime.Scheme, config *rest.Config, httpClien

// createCachedClient creates a cached client for the given cluster, based on the rest.Config.
func createCachedClient(ctx context.Context, clusterAccessorConfig *clusterAccessorConfig, config *rest.Config, httpClient *http.Client, mapper meta.RESTMapper) (client.Client, *stoppableCache, error) {
// Create the cache for the cluster.
// The byObject map needs to be cloned to not hit concurrent read/writes on the Namespaces map.
byObject := maps.Clone(clusterAccessorConfig.Cache.ByObject)
for k, v := range byObject {
v.Namespaces = maps.Clone(v.Namespaces)
byObject[k] = v
}

cacheOptions := cache.Options{
HTTPClient: httpClient,
Scheme: clusterAccessorConfig.Scheme,
Mapper: mapper,
SyncPeriod: clusterAccessorConfig.Cache.SyncPeriod,
ByObject: clusterAccessorConfig.Cache.ByObject,
ByObject: byObject,
}
remoteCache, err := cache.New(config, cacheOptions)
if err != nil {
Expand Down

0 comments on commit 1b5e6b8

Please sign in to comment.