Skip to content

Commit 38c5da0

Browse files
committed
regenerate namespace name if the name is already taken
1 parent 0c3c2cd commit 38c5da0

File tree

1 file changed

+8
-24
lines changed

1 file changed

+8
-24
lines changed

test/e2e/framework/util.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -305,25 +305,6 @@ func WaitForDefaultServiceAccountInNamespace(c clientset.Interface, namespace st
305305
return waitForServiceAccountInNamespace(c, namespace, "default", ServiceAccountProvisionTimeout)
306306
}
307307

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-
327308
// CreateTestingNS should be used by every test, note that we append a common prefix to the provided test name.
328309
// Please see NewFramework instead of using this directly.
329310
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
335316
// We don't use ObjectMeta.GenerateName feature, as in case of API call
336317
// failure we don't know whether the namespace was created and what is its
337318
// name.
338-
name, err := findAvailableNamespaceName(baseName, c)
339-
if err != nil {
340-
return nil, err
341-
}
319+
name := fmt.Sprintf("%v-%v", baseName, RandomSuffix())
342320

343321
namespaceObj := &v1.Namespace{
344322
ObjectMeta: metav1.ObjectMeta{
@@ -354,7 +332,13 @@ func CreateTestingNS(baseName string, c clientset.Interface, labels map[string]s
354332
var err error
355333
got, err = c.CoreV1().Namespaces().Create(context.TODO(), namespaceObj, metav1.CreateOptions{})
356334
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+
}
358342
return false, nil
359343
}
360344
return true, nil

0 commit comments

Comments
 (0)