@@ -32,10 +32,13 @@ import (
32
32
"github.com/onsi/ginkgo/v2"
33
33
"github.com/pkg/errors"
34
34
admissionv1 "k8s.io/api/admissionregistration/v1"
35
+ appsv1 "k8s.io/api/apps/v1"
35
36
corev1 "k8s.io/api/core/v1"
37
+ rbacv1 "k8s.io/api/rbac/v1"
36
38
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
37
39
apierrors "k8s.io/apimachinery/pkg/api/errors"
38
40
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
41
+ "k8s.io/apimachinery/pkg/runtime"
39
42
kerrors "k8s.io/apimachinery/pkg/util/errors"
40
43
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
41
44
"k8s.io/apimachinery/pkg/util/wait"
@@ -104,29 +107,34 @@ func init() {
104
107
// Add logger for ginkgo.
105
108
klog .SetOutput (ginkgo .GinkgoWriter )
106
109
107
- // Calculate the scheme.
108
- utilruntime .Must (apiextensionsv1 .AddToScheme (scheme .Scheme ))
109
- utilruntime .Must (admissionv1 .AddToScheme (scheme .Scheme ))
110
- utilruntime .Must (clusterv1 .AddToScheme (scheme .Scheme ))
111
- utilruntime .Must (bootstrapv1 .AddToScheme (scheme .Scheme ))
112
- utilruntime .Must (expv1 .AddToScheme (scheme .Scheme ))
113
- utilruntime .Must (controlplanev1 .AddToScheme (scheme .Scheme ))
114
- utilruntime .Must (admissionv1 .AddToScheme (scheme .Scheme ))
115
- utilruntime .Must (runtimev1 .AddToScheme (scheme .Scheme ))
116
- utilruntime .Must (ipamv1 .AddToScheme (scheme .Scheme ))
117
- utilruntime .Must (builder .AddTransitionV1Beta2ToScheme (scheme .Scheme ))
118
- utilruntime .Must (addonsv1 .AddToScheme (scheme .Scheme ))
110
+ // Calculate the global scheme used by fakeclients.
111
+ registerSchemes (scheme .Scheme )
112
+ }
113
+
114
+ func registerSchemes (s * runtime.Scheme ) {
115
+ utilruntime .Must (admissionv1 .AddToScheme (s ))
116
+ utilruntime .Must (apiextensionsv1 .AddToScheme (s ))
117
+
118
+ utilruntime .Must (addonsv1 .AddToScheme (s ))
119
+ utilruntime .Must (bootstrapv1 .AddToScheme (s ))
120
+ utilruntime .Must (clusterv1 .AddToScheme (s ))
121
+ utilruntime .Must (controlplanev1 .AddToScheme (s ))
122
+ utilruntime .Must (expv1 .AddToScheme (s ))
123
+ utilruntime .Must (ipamv1 .AddToScheme (s ))
124
+ utilruntime .Must (runtimev1 .AddToScheme (s ))
119
125
}
120
126
121
127
// RunInput is the input for Run.
122
128
type RunInput struct {
123
- M * testing.M
124
- ManagerUncachedObjs []client.Object
125
- ManagerCacheOptions cache.Options
126
- SetupIndexes func (ctx context.Context , mgr ctrl.Manager )
127
- SetupReconcilers func (ctx context.Context , mgr ctrl.Manager )
128
- SetupEnv func (e * Environment )
129
- MinK8sVersion string
129
+ M * testing.M
130
+ ManagerUncachedObjs []client.Object
131
+ ManagerCacheOptions cache.Options
132
+ SetupIndexes func (ctx context.Context , mgr ctrl.Manager )
133
+ SetupReconcilers func (ctx context.Context , mgr ctrl.Manager )
134
+ SetupEnv func (e * Environment )
135
+ MinK8sVersion string
136
+ AdditionalSchemeBuilder runtime.SchemeBuilder
137
+ AdditionalCRDDirectoryPaths []string
130
138
}
131
139
132
140
// Run executes the tests of the given testing.M in a test environment.
@@ -147,8 +155,20 @@ func Run(ctx context.Context, input RunInput) int {
147
155
return input .M .Run ()
148
156
}
149
157
158
+ // Calculate the scheme.
159
+ scheme := runtime .NewScheme ()
160
+ registerSchemes (scheme )
161
+ // Register additional schemes from k8s APIs.
162
+ utilruntime .Must (appsv1 .AddToScheme (scheme ))
163
+ utilruntime .Must (corev1 .AddToScheme (scheme ))
164
+ utilruntime .Must (rbacv1 .AddToScheme (scheme ))
165
+ // Register additionally passed schemes.
166
+ if input .AdditionalSchemeBuilder != nil {
167
+ utilruntime .Must (input .AdditionalSchemeBuilder .AddToScheme (scheme ))
168
+ }
169
+
150
170
// Bootstrapping test environment
151
- env := newEnvironment (input .ManagerCacheOptions , input .ManagerUncachedObjs ... )
171
+ env := newEnvironment (scheme , input . AdditionalCRDDirectoryPaths , input .ManagerCacheOptions , input .ManagerUncachedObjs ... )
152
172
153
173
ctx , cancel := context .WithCancelCause (ctx )
154
174
env .cancelManager = cancel
@@ -233,20 +253,24 @@ type Environment struct {
233
253
//
234
254
// This function should be called only once for each package you're running tests within,
235
255
// usually the environment is initialized in a suite_test.go file within a `BeforeSuite` ginkgo block.
236
- func newEnvironment (managerCacheOptions cache.Options , uncachedObjs ... client.Object ) * Environment {
256
+ func newEnvironment (scheme * runtime. Scheme , additionalCRDDirectoryPaths [] string , managerCacheOptions cache.Options , uncachedObjs ... client.Object ) * Environment {
237
257
// Get the root of the current file to use in CRD paths.
238
258
_ , filename , _ , _ := goruntime .Caller (0 ) //nolint:dogsled
239
259
root := path .Join (path .Dir (filename ), ".." , ".." , ".." )
240
260
261
+ crdDirectoryPaths := []string {
262
+ filepath .Join (root , "config" , "crd" , "bases" ),
263
+ filepath .Join (root , "controlplane" , "kubeadm" , "config" , "crd" , "bases" ),
264
+ filepath .Join (root , "bootstrap" , "kubeadm" , "config" , "crd" , "bases" ),
265
+ }
266
+ for _ , path := range additionalCRDDirectoryPaths {
267
+ crdDirectoryPaths = append (crdDirectoryPaths , filepath .Join (root , path ))
268
+ }
269
+
241
270
// Create the test environment.
242
271
env := & envtest.Environment {
243
272
ErrorIfCRDPathMissing : true ,
244
- CRDDirectoryPaths : []string {
245
- filepath .Join (root , "config" , "crd" , "bases" ),
246
- filepath .Join (root , "controlplane" , "kubeadm" , "config" , "crd" , "bases" ),
247
- filepath .Join (root , "bootstrap" , "kubeadm" , "config" , "crd" , "bases" ),
248
- filepath .Join (root , "util" , "test" , "builder" , "crd" ),
249
- },
273
+ CRDDirectoryPaths : crdDirectoryPaths ,
250
274
CRDs : []* apiextensionsv1.CustomResourceDefinition {
251
275
builder .GenericBootstrapConfigCRD .DeepCopy (),
252
276
builder .GenericBootstrapConfigTemplateCRD .DeepCopy (),
@@ -296,7 +320,7 @@ func newEnvironment(managerCacheOptions cache.Options, uncachedObjs ...client.Ob
296
320
Controller : config.Controller {
297
321
UsePriorityQueue : ptr.To [bool ](feature .Gates .Enabled (feature .PriorityQueue )),
298
322
},
299
- Scheme : scheme . Scheme ,
323
+ Scheme : scheme ,
300
324
Metrics : metricsserver.Options {
301
325
BindAddress : "0" ,
302
326
},
0 commit comments