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
26 changes: 26 additions & 0 deletions pkg/internal/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package internal

import (
"fmt"
"sync"

"k8s.io/client-go/kubernetes/scheme"

proposalv1alpha1 "github.com/openshift/lightspeed-agentic-operator/api/v1alpha1"
)

var (
addSchemesOnce sync.Once
addSchemesErr error
)

// AddSchemes registers the proposalv1alpha1 scheme with the global Kubernetes scheme.
// This function is safe to call concurrently and will only execute once.
func AddSchemes() error {
addSchemesOnce.Do(func() {
if err := proposalv1alpha1.AddToScheme(scheme.Scheme); err != nil {
addSchemesErr = fmt.Errorf("failed to add proposalv1alpha1 to scheme: %w", err)
}
})
return addSchemesErr
}
35 changes: 35 additions & 0 deletions pkg/internal/schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package internal

import (
"sync"
"testing"
)

// TestAddSchemesConcurrent verifies that AddSchemes can be called concurrently
// without causing race conditions or panics. This is a regression test for
// "fatal error: concurrent map writes" when integration tests run in parallel.
func TestAddSchemesConcurrent(t *testing.T) {
const numGoroutines = 10

var wg sync.WaitGroup
errChan := make(chan error, numGoroutines)

// Call AddSchemes concurrently from multiple goroutines
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if err := AddSchemes(); err != nil {
errChan <- err
}
}()
}

wg.Wait()
close(errChan)

// Check if any goroutine encountered an error
for err := range errChan {
t.Errorf("AddSchemes failed: %v", err)
}
}
6 changes: 3 additions & 3 deletions pkg/proposal/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kutilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/kubernetes/scheme"

configv1 "github.com/openshift/api/config/v1"
proposalv1alpha1 "github.com/openshift/lightspeed-agentic-operator/api/v1alpha1"

"github.com/openshift/cluster-version-operator/pkg/internal"
)

func init() {
err := proposalv1alpha1.AddToScheme(scheme.Scheme)
if err != nil {
if err := internal.AddSchemes(); err != nil {
panic(err)
}
}
Expand Down
10 changes: 1 addition & 9 deletions pkg/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
operatorinformers "github.com/openshift/client-go/operator/informers/externalversions"
"github.com/openshift/library-go/pkg/config/clusterstatus"
libgoleaderelection "github.com/openshift/library-go/pkg/config/leaderelection"
proposalv1alpha1 "github.com/openshift/lightspeed-agentic-operator/api/v1alpha1"

"github.com/openshift/cluster-version-operator/pkg/autoupdate"
"github.com/openshift/cluster-version-operator/pkg/clusterconditions"
Expand Down Expand Up @@ -615,13 +614,6 @@ type Context struct {
OperatorInformerFactory operatorinformers.SharedInformerFactory
}

func addSchemes() error {
if err := proposalv1alpha1.AddToScheme(scheme.Scheme); err != nil {
return fmt.Errorf("failed to add proposalv1alpha1 to scheme: %w", err)
}
return nil
}

// NewControllerContext initializes the default Context for the current Options. It does
// not start any background processes.
func (o *Options) NewControllerContext(
Expand All @@ -647,7 +639,7 @@ func (o *Options) NewControllerContext(
cvoKubeClient := cb.KubeClientOrDie(o.Namespace, useProtobuf)
o.PromQLTarget.KubeClient = cvoKubeClient

if err := addSchemes(); err != nil {
if err := internal.AddSchemes(); err != nil {
return nil, err
}
rtClient := cb.RuntimeControllerClientOrDie("runtime-controller-client")
Expand Down