Skip to content

Commit e222de3

Browse files
authored
pkg/test/framework.go: protect AddToFrameworkScheme with mutex (#409)
* pkg/test/framework.go: protect AddToFrameworkScheme with mutex If more than 1 test concurrently ran AddToFrameworkScheme, there would be concurrent map read/write errors. By using a mutex, we can ensure that there is not more than 1 function modifying the scheme at the same time. * pkg/test/framework.go: make mutex declaration concise * pkg/test/framework.go: suggestions from fanminshi
1 parent cac9158 commit e222de3

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

pkg/test/framework.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package test
1717
import (
1818
goctx "context"
1919
"fmt"
20+
"sync"
2021
"time"
2122

2223
extensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
@@ -34,7 +35,12 @@ import (
3435
dynclient "sigs.k8s.io/controller-runtime/pkg/client"
3536
)
3637

37-
var Global *Framework
38+
var (
39+
// mutex for AddToFrameworkScheme
40+
mutex = sync.Mutex{}
41+
// Global framework struct
42+
Global *Framework
43+
)
3844

3945
type Framework struct {
4046
KubeConfig *rest.Config
@@ -101,6 +107,8 @@ type addToSchemeFunc func(*runtime.Scheme) error
101107
// by the time this function is called. If the CRD takes more than 5 seconds to
102108
// become ready, this function throws an error
103109
func AddToFrameworkScheme(addToScheme addToSchemeFunc, obj runtime.Object) error {
110+
mutex.Lock()
111+
defer mutex.Unlock()
104112
err := addToScheme(Global.Scheme)
105113
if err != nil {
106114
return err

0 commit comments

Comments
 (0)