mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #90591 from cofyc/fix-e2e-namespace-creation
e2e: regenerate namespace name if the name is already taken
This commit is contained in:
commit
a0344973a3
@ -304,25 +304,6 @@ func WaitForDefaultServiceAccountInNamespace(c clientset.Interface, namespace st
|
|||||||
return waitForServiceAccountInNamespace(c, namespace, "default", ServiceAccountProvisionTimeout)
|
return waitForServiceAccountInNamespace(c, namespace, "default", ServiceAccountProvisionTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
// findAvailableNamespaceName random namespace name starting with baseName.
|
|
||||||
func findAvailableNamespaceName(baseName string, c clientset.Interface) (string, error) {
|
|
||||||
var name string
|
|
||||||
err := wait.PollImmediate(Poll, 30*time.Second, func() (bool, error) {
|
|
||||||
name = fmt.Sprintf("%v-%v", baseName, RandomSuffix())
|
|
||||||
_, err := c.CoreV1().Namespaces().Get(context.TODO(), name, metav1.GetOptions{})
|
|
||||||
if err == nil {
|
|
||||||
// Already taken
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
if apierrors.IsNotFound(err) {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
Logf("Unexpected error while getting namespace: %v", err)
|
|
||||||
return false, nil
|
|
||||||
})
|
|
||||||
return name, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateTestingNS should be used by every test, note that we append a common prefix to the provided test name.
|
// CreateTestingNS should be used by every test, note that we append a common prefix to the provided test name.
|
||||||
// Please see NewFramework instead of using this directly.
|
// Please see NewFramework instead of using this directly.
|
||||||
func CreateTestingNS(baseName string, c clientset.Interface, labels map[string]string) (*v1.Namespace, error) {
|
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
|
|||||||
// We don't use ObjectMeta.GenerateName feature, as in case of API call
|
// We don't use ObjectMeta.GenerateName feature, as in case of API call
|
||||||
// failure we don't know whether the namespace was created and what is its
|
// failure we don't know whether the namespace was created and what is its
|
||||||
// name.
|
// name.
|
||||||
name, err := findAvailableNamespaceName(baseName, c)
|
name := fmt.Sprintf("%v-%v", baseName, RandomSuffix())
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
namespaceObj := &v1.Namespace{
|
namespaceObj := &v1.Namespace{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
@ -353,7 +331,13 @@ func CreateTestingNS(baseName string, c clientset.Interface, labels map[string]s
|
|||||||
var err error
|
var err error
|
||||||
got, err = c.CoreV1().Namespaces().Create(context.TODO(), namespaceObj, metav1.CreateOptions{})
|
got, err = c.CoreV1().Namespaces().Create(context.TODO(), namespaceObj, metav1.CreateOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Logf("Unexpected error while creating namespace: %v", err)
|
if apierrors.IsAlreadyExists(err) {
|
||||||
|
// regenerate on conflict
|
||||||
|
Logf("Namespace name %q was already taken, generate a new name and retry", namespaceObj.Name)
|
||||||
|
namespaceObj.Name = fmt.Sprintf("%v-%v", baseName, RandomSuffix())
|
||||||
|
} else {
|
||||||
|
Logf("Unexpected error while creating namespace: %v", err)
|
||||||
|
}
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user