diff --git a/e2e/support/test_support.go b/e2e/support/test_support.go index bc2d726cc4..d74dcbf94a 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 15fafbcb02..201afae340 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