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