@@ -31,6 +31,7 @@ import (
31
31
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
32
"k8s.io/apimachinery/pkg/runtime"
33
33
"k8s.io/apimachinery/pkg/runtime/schema"
34
+ "k8s.io/apiserver/pkg/storage/names"
34
35
"k8s.io/client-go/kubernetes/scheme"
35
36
"k8s.io/client-go/tools/cache"
36
37
"k8s.io/kubernetes/test/e2e/framework/testfiles"
@@ -100,9 +101,9 @@ func visitManifests(cb func([]byte) error, files ...string) error {
100
101
return nil
101
102
}
102
103
103
- // PatchItems modifies the given items in place such that each test
104
- // gets its own instances, to avoid conflicts between different tests
105
- // and between tests and normal deployments.
104
+ // PatchItems modifies the given items in place such that each
105
+ // test gets its own instances, to avoid conflicts between different tests and
106
+ // between tests and normal deployments.
106
107
//
107
108
// This is done by:
108
109
// - creating namespaced items inside the test's namespace
@@ -287,18 +288,27 @@ var factories = map[What]ItemFactory{
287
288
{"StorageClass" }: & storageClassFactory {},
288
289
}
289
290
290
- // PatchName makes the name of some item unique by appending the
291
- // generated unique name.
292
- func (f * Framework ) PatchName (item * string ) {
291
+ // uniquifyName makes the name of some item unique per namespace by appending the
292
+ // generated unique name of the test namespace .
293
+ func (f * Framework ) uniquifyName (item * string ) {
293
294
if * item != "" {
294
295
* item = * item + "-" + f .UniqueName
295
296
}
296
297
}
297
298
298
- // PatchNamespace moves the item into the test's namespace. Not
299
+ // randomizeStorageClassName makes the name of the storage class unique per call
300
+ // by appending the generated unique name of the test namespace and a random 5
301
+ // character string
302
+ func (f * Framework ) randomizeStorageClassName (item * string ) {
303
+ if * item != "" {
304
+ * item = names .SimpleNameGenerator .GenerateName (* item + "-" + f .UniqueName + "-" )
305
+ }
306
+ }
307
+
308
+ // patchNamespace moves the item into the test's namespace. Not
299
309
// all items can be namespaced. For those, the name also needs to be
300
310
// patched.
301
- func (f * Framework ) PatchNamespace (item * string ) {
311
+ func (f * Framework ) patchNamespace (item * string ) {
302
312
if f .Namespace != nil {
303
313
* item = f .Namespace .GetName ()
304
314
}
@@ -307,31 +317,31 @@ func (f *Framework) PatchNamespace(item *string) {
307
317
func (f * Framework ) patchItemRecursively (item interface {}) error {
308
318
switch item := item .(type ) {
309
319
case * rbac.Subject :
310
- f .PatchNamespace (& item .Namespace )
320
+ f .patchNamespace (& item .Namespace )
311
321
case * rbac.RoleRef :
312
322
// TODO: avoid hard-coding this special name. Perhaps add a Framework.PredefinedRoles
313
323
// which contains all role names that are defined cluster-wide before the test starts?
314
324
// All those names are excempt from renaming. That list could be populated by querying
315
325
// and get extended by tests.
316
326
if item .Name != "e2e-test-privileged-psp" {
317
- f .PatchName (& item .Name )
327
+ f .uniquifyName (& item .Name )
318
328
}
319
329
case * rbac.ClusterRole :
320
- f .PatchName (& item .Name )
330
+ f .uniquifyName (& item .Name )
321
331
case * rbac.Role :
322
- f .PatchNamespace (& item .Namespace )
332
+ f .patchNamespace (& item .Namespace )
323
333
// Roles are namespaced, but because for RoleRef above we don't
324
334
// know whether the referenced role is a ClusterRole or Role
325
335
// and therefore always renames, we have to do the same here.
326
- f .PatchName (& item .Name )
336
+ f .uniquifyName (& item .Name )
327
337
case * storage.StorageClass :
328
- f .PatchName (& item .Name )
338
+ f .randomizeStorageClassName (& item .Name )
329
339
case * v1.ServiceAccount :
330
- f .PatchNamespace (& item .ObjectMeta .Namespace )
340
+ f .patchNamespace (& item .ObjectMeta .Namespace )
331
341
case * v1.Secret :
332
- f .PatchNamespace (& item .ObjectMeta .Namespace )
342
+ f .patchNamespace (& item .ObjectMeta .Namespace )
333
343
case * rbac.ClusterRoleBinding :
334
- f .PatchName (& item .Name )
344
+ f .uniquifyName (& item .Name )
335
345
for i := range item .Subjects {
336
346
if err := f .patchItemRecursively (& item .Subjects [i ]); err != nil {
337
347
return errors .Wrapf (err , "%T" , f )
@@ -341,7 +351,7 @@ func (f *Framework) patchItemRecursively(item interface{}) error {
341
351
return errors .Wrapf (err , "%T" , f )
342
352
}
343
353
case * rbac.RoleBinding :
344
- f .PatchNamespace (& item .Namespace )
354
+ f .patchNamespace (& item .Namespace )
345
355
for i := range item .Subjects {
346
356
if err := f .patchItemRecursively (& item .Subjects [i ]); err != nil {
347
357
return errors .Wrapf (err , "%T" , f )
@@ -351,11 +361,11 @@ func (f *Framework) patchItemRecursively(item interface{}) error {
351
361
return errors .Wrapf (err , "%T" , f )
352
362
}
353
363
case * v1.Service :
354
- f .PatchNamespace (& item .ObjectMeta .Namespace )
364
+ f .patchNamespace (& item .ObjectMeta .Namespace )
355
365
case * apps.StatefulSet :
356
- f .PatchNamespace (& item .ObjectMeta .Namespace )
366
+ f .patchNamespace (& item .ObjectMeta .Namespace )
357
367
case * apps.DaemonSet :
358
- f .PatchNamespace (& item .ObjectMeta .Namespace )
368
+ f .patchNamespace (& item .ObjectMeta .Namespace )
359
369
default :
360
370
return errors .Errorf ("missing support for patching item of type %T" , item )
361
371
}
0 commit comments