Skip to content

Commit

Permalink
(fix#5315) fatal error: concurrent map read and map write
Browse files Browse the repository at this point in the history
  • Loading branch information
tdiesler authored and christophd committed Apr 9, 2024
1 parent d5a80e4 commit 6b1b5f6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
5 changes: 0 additions & 5 deletions e2e/support/test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ var NoOlmOperatorImage string

var testContext = context.TODO()
var testClient client.Client
var clientMutex = sync.Mutex{}

var testSetupMutex = sync.Mutex{}
var kamelInstallMutex = sync.Mutex{}
Expand All @@ -162,8 +161,6 @@ func TestContext() context.Context {
}

func TestClient(t *testing.T) client.Client {
clientMutex.Lock()
defer clientMutex.Unlock()

if testClient != nil {
return testClient
Expand All @@ -178,8 +175,6 @@ func TestClient(t *testing.T) client.Client {
}

func RefreshClient(t *testing.T) client.Client {
clientMutex.Lock()
defer clientMutex.Unlock()

var err error
testClient, err = NewTestClient()
Expand Down
10 changes: 10 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"os"
"path/filepath"
"sync"

"github.com/sirupsen/logrus"
"k8s.io/client-go/scale"
Expand Down Expand Up @@ -53,6 +54,8 @@ const (
kubeConfigEnvVar = "KUBECONFIG"
)

var newClientMutex sync.Mutex

// Client is an abstraction for a k8s client.
type Client interface {
ctrl.Client
Expand Down Expand Up @@ -125,6 +128,13 @@ func NewClient(fastDiscovery bool) (Client, error) {

// NewClientWithConfig creates a new k8s client that can be used from outside or in the cluster.
func NewClientWithConfig(fastDiscovery bool, cfg *rest.Config) (Client, error) {

// The below call to apis.AddToScheme is not thread safe in the k8s API
// We try to synchronize here across all k8s clients
// https://github.com/apache/camel-k/issues/5315
newClientMutex.Lock()
defer newClientMutex.Unlock()

clientScheme := scheme.Scheme

// Setup Scheme for all resources
Expand Down

0 comments on commit 6b1b5f6

Please sign in to comment.