@@ -305,25 +305,6 @@ func WaitForDefaultServiceAccountInNamespace(c clientset.Interface, namespace st
305
305
return waitForServiceAccountInNamespace (c , namespace , "default" , ServiceAccountProvisionTimeout )
306
306
}
307
307
308
- // findAvailableNamespaceName random namespace name starting with baseName.
309
- func findAvailableNamespaceName (baseName string , c clientset.Interface ) (string , error ) {
310
- var name string
311
- err := wait .PollImmediate (Poll , 30 * time .Second , func () (bool , error ) {
312
- name = fmt .Sprintf ("%v-%v" , baseName , RandomSuffix ())
313
- _ , err := c .CoreV1 ().Namespaces ().Get (context .TODO (), name , metav1.GetOptions {})
314
- if err == nil {
315
- // Already taken
316
- return false , nil
317
- }
318
- if apierrors .IsNotFound (err ) {
319
- return true , nil
320
- }
321
- Logf ("Unexpected error while getting namespace: %v" , err )
322
- return false , nil
323
- })
324
- return name , err
325
- }
326
-
327
308
// CreateTestingNS should be used by every test, note that we append a common prefix to the provided test name.
328
309
// Please see NewFramework instead of using this directly.
329
310
func CreateTestingNS (baseName string , c clientset.Interface , labels map [string ]string ) (* v1.Namespace , error ) {
@@ -335,10 +316,7 @@ func CreateTestingNS(baseName string, c clientset.Interface, labels map[string]s
335
316
// We don't use ObjectMeta.GenerateName feature, as in case of API call
336
317
// failure we don't know whether the namespace was created and what is its
337
318
// name.
338
- name , err := findAvailableNamespaceName (baseName , c )
339
- if err != nil {
340
- return nil , err
341
- }
319
+ name := fmt .Sprintf ("%v-%v" , baseName , RandomSuffix ())
342
320
343
321
namespaceObj := & v1.Namespace {
344
322
ObjectMeta : metav1.ObjectMeta {
@@ -354,7 +332,13 @@ func CreateTestingNS(baseName string, c clientset.Interface, labels map[string]s
354
332
var err error
355
333
got , err = c .CoreV1 ().Namespaces ().Create (context .TODO (), namespaceObj , metav1.CreateOptions {})
356
334
if err != nil {
357
- Logf ("Unexpected error while creating namespace: %v" , err )
335
+ if apierrors .IsAlreadyExists (err ) {
336
+ // regenerate on conflict
337
+ Logf ("Namespace name %q was already taken, generate a new name and retry" , namespaceObj .Name )
338
+ namespaceObj .Name = fmt .Sprintf ("%v-%v" , baseName , RandomSuffix ())
339
+ } else {
340
+ Logf ("Unexpected error while creating namespace: %v" , err )
341
+ }
358
342
return false , nil
359
343
}
360
344
return true , nil
0 commit comments