Skip to content

Commit a034497

Browse files
authored
Merge pull request kubernetes#90591 from cofyc/fix-e2e-namespace-creation
e2e: regenerate namespace name if the name is already taken
2 parents 1ebb55e + 38c5da0 commit a034497

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
@@ -304,25 +304,6 @@ func WaitForDefaultServiceAccountInNamespace(c clientset.Interface, namespace st
304304
return waitForServiceAccountInNamespace(c, namespace, "default", ServiceAccountProvisionTimeout)
305305
}
306306

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

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

0 commit comments

Comments
 (0)