Skip to content

Commit

Permalink
Merge pull request kubernetes#72054 from mborsz/createnamespace
Browse files Browse the repository at this point in the history
In CreateTestingNS generate name upfront to avoid namespace leakage.
  • Loading branch information
k8s-ci-robot authored Dec 14, 2018
2 parents 57ea592 + 3223d9b commit 8547036
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions test/e2e/framework/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,25 @@ func WaitForPersistentVolumeClaimsPhase(phase v1.PersistentVolumeClaimPhase, c c
return fmt.Errorf("PersistentVolumeClaims %v not all in phase %s within %v", pvcNames, phase, timeout)
}

// 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(name, metav1.GetOptions{})
if err == nil {
// Already taken
return false, nil
}
if apierrs.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.
// Please see NewFramework instead of using this directly.
func CreateTestingNS(baseName string, c clientset.Interface, labels map[string]string) (*v1.Namespace, error) {
Expand All @@ -1060,11 +1079,19 @@ func CreateTestingNS(baseName string, c clientset.Interface, labels map[string]s
}
labels["e2e-run"] = string(RunId)

// 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
// name.
name, err := findAvailableNamespaceName(baseName, c)
if err != nil {
return nil, err
}

namespaceObj := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("e2e-tests-%v-", baseName),
Namespace: "",
Labels: labels,
Name: name,
Namespace: "",
Labels: labels,
},
Status: v1.NamespaceStatus{},
}
Expand Down

0 comments on commit 8547036

Please sign in to comment.