Skip to content

🐛 clustercache: Prevent concurrent map read/write when creating a cache #11707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2025

Conversation

chrischdi
Copy link
Member

What this PR does / why we need it:

The clustercache has an issue with concurrent read writes. Found data races:

WARNING: DATA RACE
Write at 0x00c000d48480 by goroutine 378:
  runtime.mapaccess2()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/runtime/map.go:479 +0x28c
  sigs.k8s.io/controller-runtime/pkg/cache.defaultOpts()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:523 +0x650
  sigs.k8s.io/controller-runtime/pkg/cache.New()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:340 +0x70
  sigs.k8s.io/cluster-api/controllers/clustercache.createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client.go:224 +0x120
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient.func2()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:194 +0x90

Previous read at 0x00c000d48480 by goroutine 377:
  runtime.mapdelete()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/runtime/map.go:741 +0x43c
  sigs.k8s.io/controller-runtime/pkg/cache.defaultOpts()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:472 +0x144
  sigs.k8s.io/controller-runtime/pkg/cache.New()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:340 +0x70
  sigs.k8s.io/cluster-api/controllers/clustercache.createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client.go:224 +0x120
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient.func2()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:194 +0x90

Goroutine 378 (running) created at:
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:192 +0xb80
  testing.tRunner()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1743 +0x40

Goroutine 377 (running) created at:
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:192 +0xb80
  testing.tRunner()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1743 +0x40
==================
==================
WARNING: DATA RACE
Write at 0x00c0002ed690 by goroutine 378:
  sigs.k8s.io/controller-runtime/pkg/cache.defaultOpts()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:523 +0x660
  sigs.k8s.io/controller-runtime/pkg/cache.New()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:340 +0x70
  sigs.k8s.io/cluster-api/controllers/clustercache.createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client.go:224 +0x120
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient.func2()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:194 +0x90

Previous read at 0x00c0002ed690 by goroutine 377:
  sigs.k8s.io/controller-runtime/pkg/cache.defaultOpts()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:472 +0x308
  sigs.k8s.io/controller-runtime/pkg/cache.New()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:340 +0x70
  sigs.k8s.io/cluster-api/controllers/clustercache.createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client.go:224 +0x120
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient.func2()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:194 +0x90

Goroutine 378 (running) created at:
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:192 +0xb80
  testing.tRunner()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1743 +0x40

Goroutine 377 (running) created at:
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:192 +0xb80
  testing.tRunner()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1743 +0x40
==================
==================
WARNING: DATA RACE
Read at 0x00c000d484b0 by goroutine 377:
  runtime.mapdelete()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/runtime/map.go:741 +0x43c
  sigs.k8s.io/controller-runtime/pkg/cache.defaultOpts()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:487 +0x418
  sigs.k8s.io/controller-runtime/pkg/cache.New()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:340 +0x70
  sigs.k8s.io/cluster-api/controllers/clustercache.createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client.go:224 +0x120
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient.func2()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:194 +0x90

Previous write at 0x00c000d484b0 by goroutine 378:
  runtime.mapaccess2_faststr()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/runtime/map_faststr.go:117 +0x42c
  sigs.k8s.io/controller-runtime/pkg/cache.defaultOpts()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:509 +0x1270
  sigs.k8s.io/controller-runtime/pkg/cache.New()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:340 +0x70
  sigs.k8s.io/cluster-api/controllers/clustercache.createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client.go:224 +0x120
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient.func2()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:194 +0x90

Goroutine 377 (running) created at:
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:192 +0xb80
  testing.tRunner()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1743 +0x40

Goroutine 378 (running) created at:
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:192 +0xb80
  testing.tRunner()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1743 +0x40
==================
==================
WARNING: DATA RACE
Read at 0x00c00107a590 by goroutine 377:
  sigs.k8s.io/controller-runtime/pkg/cache.defaultOpts()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:487 +0xcf0
  sigs.k8s.io/controller-runtime/pkg/cache.New()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:340 +0x70
  sigs.k8s.io/cluster-api/controllers/clustercache.createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client.go:224 +0x120
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient.func2()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:194 +0x90

Previous write at 0x00c00107a590 by goroutine 378:
  sigs.k8s.io/controller-runtime/pkg/cache.defaultOpts()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:509 +0x1280
  sigs.k8s.io/controller-runtime/pkg/cache.New()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:340 +0x70
  sigs.k8s.io/cluster-api/controllers/clustercache.createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client.go:224 +0x120
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient.func2()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:194 +0x90

Goroutine 377 (running) created at:
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:192 +0xb80
  testing.tRunner()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1743 +0x40

Goroutine 378 (running) created at:
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:192 +0xb80
  testing.tRunner()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1743 +0x40
==================
==================
WARNING: DATA RACE
Write at 0x00c000d48480 by goroutine 377:
  runtime.mapaccess2()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/runtime/map.go:479 +0x28c
  sigs.k8s.io/controller-runtime/pkg/cache.defaultOpts()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:523 +0x650
  sigs.k8s.io/controller-runtime/pkg/cache.New()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:340 +0x70
  sigs.k8s.io/cluster-api/controllers/clustercache.createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client.go:224 +0x120
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient.func2()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:194 +0x90

Previous read at 0x00c000d48480 by goroutine 378:
  runtime.mapiterinit()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/runtime/map.go:877 +0x27c
  sigs.k8s.io/controller-runtime/pkg/cache.defaultOpts()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:472 +0x2a8
  sigs.k8s.io/controller-runtime/pkg/cache.New()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:340 +0x70
  sigs.k8s.io/cluster-api/controllers/clustercache.createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client.go:224 +0x120
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient.func2()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:194 +0x90

Goroutine 377 (running) created at:
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:192 +0xb80
  testing.tRunner()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1743 +0x40

Goroutine 378 (running) created at:
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:192 +0xb80
  testing.tRunner()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1743 +0x40
==================
==================
WARNING: DATA RACE
Write at 0x00c000d48480 by goroutine 378:
  runtime.mapaccess2()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/runtime/map.go:479 +0x28c
  sigs.k8s.io/controller-runtime/pkg/cache.defaultOpts()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:523 +0x650
  sigs.k8s.io/controller-runtime/pkg/cache.New()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:340 +0x70
  sigs.k8s.io/cluster-api/controllers/clustercache.createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client.go:224 +0x120
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient.func2()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:194 +0x90

Previous write at 0x00c000d48480 by goroutine 377:
  runtime.mapaccess2()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/runtime/map.go:479 +0x28c
  sigs.k8s.io/controller-runtime/pkg/cache.defaultOpts()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:523 +0x650
  sigs.k8s.io/controller-runtime/pkg/cache.New()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:340 +0x70
  sigs.k8s.io/cluster-api/controllers/clustercache.createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client.go:224 +0x120
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient.func2()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:194 +0x90

Goroutine 378 (running) created at:
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:192 +0xb80
  testing.tRunner()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1743 +0x40

Goroutine 377 (running) created at:
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:192 +0xb80
  testing.tRunner()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1743 +0x40
==================
==================
WARNING: DATA RACE
Read at 0x00c000d484b0 by goroutine 378:
  sigs.k8s.io/controller-runtime/pkg/cache.New()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:371 +0x604
  sigs.k8s.io/cluster-api/controllers/clustercache.createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client.go:224 +0x120
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient.func2()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:194 +0x90

Previous write at 0x00c000d484b0 by goroutine 377:
  runtime.mapaccess2_faststr()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/runtime/map_faststr.go:117 +0x42c
  sigs.k8s.io/controller-runtime/pkg/cache.defaultOpts()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:509 +0x1270
  sigs.k8s.io/controller-runtime/pkg/cache.New()
      /Users/schlotterc/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/cache/cache.go:340 +0x70
  sigs.k8s.io/cluster-api/controllers/clustercache.createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client.go:224 +0x120
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient.func2()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:194 +0x90

Goroutine 378 (running) created at:
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:192 +0xb80
  testing.tRunner()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1743 +0x40

Goroutine 377 (running) created at:
  sigs.k8s.io/cluster-api/controllers/clustercache.Test_createCachedClient()
      /Users/schlotterc/go/src/sigs.k8s.io/cluster-api/controllers/clustercache/cluster_accessor_client_test.go:192 +0xb80
  testing.tRunner()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /Users/schlotterc/.bin/go-archive/go1.23.2.darwin-arm64/src/testing/testing.go:1743 +0x40

Reproducible with the following unit test and by running it including the -race flag, e.g. go test -race -count=1 -timeout 300s -run '^Test_createCachedClient$':

func Test_createCachedClient(t *testing.T) {
	g := NewWithT(t)
	labelSelectorRequirement, err := labels.NewRequirement("foo", selection.In, []string{"a", "b", "c", "d"})
	g.Expect(err).ToNot(HaveOccurred())

	mapper := env.Client.RESTMapper()
	restConfig := env.Config
	httpClient := env.GetHTTPClient()

	clusterAccessorConfig := buildClusterAccessorConfig(env.Scheme(), Options{
		SecretClient: env.GetAPIReader(),
		Cache: CacheOptions{
			ByObject: map[client.Object]cache.ByObject{
				&corev1.Pod{}: {
					Namespaces: map[string]cache.Config{
						metav1.NamespaceSystem: {
							LabelSelector: labels.NewSelector().Add(*labelSelectorRequirement),
						},
					},
				},
				&corev1.Secret{}: {},
			},
		},
	}, &metav1.ObjectMeta{})

	m := sync.RWMutex{}
	wg := sync.WaitGroup{}
	// Block until we finished the setup
	m.Lock()
	for range 2 {
		wg.Add(1)
		go func() {
			m.RLock()
			createCachedClient(ctx, clusterAccessorConfig, restConfig, httpClient, mapper)
			wg.Done()
		}()
	}
	m.Unlock()
	wg.Wait()
}

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jan 20, 2025
@chrischdi
Copy link
Member Author

/area clustercache

@k8s-ci-robot k8s-ci-robot added the do-not-merge/needs-area PR is missing an area label label Jan 20, 2025
@k8s-ci-robot
Copy link
Contributor

@chrischdi: The label(s) area/clustercache cannot be applied, because the repository doesn't have them.

In response to this:

/area clustercache

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Jan 20, 2025
@chrischdi
Copy link
Member Author

/assign @sbueringer @fabriziopandini

@chrischdi
Copy link
Member Author

/area clustercache

@k8s-ci-robot
Copy link
Contributor

@chrischdi: The label(s) area/clustercache cannot be applied, because the repository doesn't have them.

In response to this:

/area clustercache

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@chrischdi chrischdi added the area/clustercache Issues or PRs related to the clustercachetracker label Jan 20, 2025
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/needs-area PR is missing an area label label Jan 20, 2025
@chrischdi chrischdi changed the title 🌱 clustercache: Prevent concurrent map read/write when creating a cache 🐛 clustercache: Prevent concurrent map read/write when creating a cache Jan 20, 2025
@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's open a small issue to track removal of this code once it's fixed in CR

@sbueringer
Copy link
Member

Thx!

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 20, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: c0111678afb5789b766c1bc7ed7ee54cf5e5dc89

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sbueringer

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 20, 2025
@sbueringer
Copy link
Member

/cherry-pick release-1.9

@k8s-infra-cherrypick-robot

@sbueringer: once the present PR merges, I will cherry-pick it on top of release-1.9 in a new PR and assign it to you.

In response to this:

/cherry-pick release-1.9

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot merged commit 1b5e6b8 into kubernetes-sigs:main Jan 20, 2025
24 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.10 milestone Jan 20, 2025
@k8s-infra-cherrypick-robot

@sbueringer: new pull request created: #11709

In response to this:

/cherry-pick release-1.9

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/clustercache Issues or PRs related to the clustercachetracker cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants