Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/fragments/helm-operator-watched-secrets-race.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
entries:
- description: >
For Helm-based operators, fixed a concurrent map read and write that could crash the
operator with "fatal error: concurrent map read and map write" when reconciles of
different namespaces started at the same time.
kind: bugfix
breaking: false
12 changes: 7 additions & 5 deletions internal/helm/client/actionconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ type actionConfigGetter struct {
// Creates a new watcher for each namespace to not require cluster-wide secret access
func (acg *actionConfigGetter) getWatchedSecretsForNamespace(namespace string) *WatchedSecrets {
acg.watchedSecretsMutex.Lock()
if _, found := acg.watchedSecrets[namespace]; !found {
acg.watchedSecrets[namespace] = NewWatchedSecrets(acg.kubeClientSet, namespace)
acg.watchedSecrets[namespace].Run()
defer acg.watchedSecretsMutex.Unlock()
ws, found := acg.watchedSecrets[namespace]
if !found {
ws = NewWatchedSecrets(acg.kubeClientSet, namespace)
ws.Run()
acg.watchedSecrets[namespace] = ws
}
acg.watchedSecretsMutex.Unlock()
return acg.watchedSecrets[namespace]
return ws
}

func (acg *actionConfigGetter) ActionConfigFor(obj client.Object) (*action.Configuration, error) {
Expand Down
47 changes: 47 additions & 0 deletions internal/helm/client/actionconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2026 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
"fmt"
"sync"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/client-go/kubernetes/fake"
)

// Run with -race: reconciles of different namespaces look up and add watched secrets concurrently.
func TestGetWatchedSecretsForNamespaceConcurrently(t *testing.T) {
acg := &actionConfigGetter{
kubeClientSet: fake.NewClientset(),
watchedSecrets: map[string]*WatchedSecrets{},
watchedSecretsMutex: &sync.Mutex{},
}
const workers, namespaces = 8, 50
var wg sync.WaitGroup
for w := range workers {
wg.Add(1)
go func() {
defer wg.Done()
for n := range namespaces {
ns := fmt.Sprintf("ns-%d", (n+w)%namespaces)
assert.NotNil(t, acg.getWatchedSecretsForNamespace(ns))
}
}()
}
wg.Wait()
assert.Len(t, acg.watchedSecrets, namespaces)
}