forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace.go
59 lines (52 loc) · 1.7 KB
/
namespace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package util
import (
"fmt"
"time"
kapi "k8s.io/kubernetes/pkg/api"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/watch"
"github.com/openshift/origin/pkg/cmd/util"
)
// Namespace returns the test namespace. The default namespace is set to
// 'integration-test'. You can override it by setting the 'OS_TEST_NAMESPACE'
// environment variable
func Namespace() string {
return util.Env("OS_TEST_NAMESPACE", "integration")
}
// RandomNamespace provides random Kubernetes namespace name based on the UNIX
// timestamp. Optionally you can set the prefix.
func RandomNamespace(prefix string) string {
return prefix + string([]byte(fmt.Sprintf("%d", time.Now().UnixNano()))[3:12])
}
// CreateNamespace creates a namespace with the specified name using the provided kubeconfig
// DO NOT USE, use create project instead
func CreateNamespace(clusterAdminKubeConfig, name string) (err error) {
clusterAdminKubeClient, err := GetClusterAdminKubeClient(clusterAdminKubeConfig)
if err != nil {
return err
}
_, err = clusterAdminKubeClient.Namespaces().Create(&kapi.Namespace{
ObjectMeta: kapi.ObjectMeta{Name: name},
})
return err
}
func DeleteAndWaitForNamespaceTermination(c *kclientset.Clientset, name string) error {
w, err := c.Core().Namespaces().Watch(kapi.ListOptions{})
if err != nil {
return err
}
if err := c.Core().Namespaces().Delete(name, nil); err != nil {
return err
}
_, err = watch.Until(30*time.Second, w, func(event watch.Event) (bool, error) {
if event.Type != watch.Deleted {
return false, nil
}
namespace, ok := event.Object.(*kapi.Namespace)
if !ok {
return false, nil
}
return namespace.Name == name, nil
})
return err
}