This is an automated email from the ASF dual-hosted git repository.

cdeppisch pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/main by this push:
     new 6b1b5f60c (fix#5315) fatal error: concurrent map read and map write
6b1b5f60c is described below

commit 6b1b5f60c7e7fc760f7cd6d046e99567ef154cd8
Author: Thomas Diesler <tdies...@redhat.com>
AuthorDate: Wed Apr 3 13:23:35 2024 +0200

    (fix#5315) fatal error: concurrent map read and map write
---
 e2e/support/test_support.go |  5 -----
 pkg/client/client.go        | 10 ++++++++++
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/e2e/support/test_support.go b/e2e/support/test_support.go
index bc2d726cc..d74dcbf94 100644
--- a/e2e/support/test_support.go
+++ b/e2e/support/test_support.go
@@ -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{}
@@ -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
@@ -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()
diff --git a/pkg/client/client.go b/pkg/client/client.go
index 15fafbcb0..201afae34 100644
--- a/pkg/client/client.go
+++ b/pkg/client/client.go
@@ -22,6 +22,7 @@ import (
        "fmt"
        "os"
        "path/filepath"
+       "sync"
 
        "github.com/sirupsen/logrus"
        "k8s.io/client-go/scale"
@@ -53,6 +54,8 @@ const (
        kubeConfigEnvVar         = "KUBECONFIG"
 )
 
+var newClientMutex sync.Mutex
+
 // Client is an abstraction for a k8s client.
 type Client interface {
        ctrl.Client
@@ -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

Reply via email to