forked from gruntwork-io/terratest
/
namespace.go
66 lines (55 loc) · 2.5 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
60
61
62
63
64
65
66
package k8s
import (
"testing"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// CreateNamespace will create a new Kubernetes namespace on the cluster targeted by the provided options. This will
// fail the test if there is an error in creating the namespace.
func CreateNamespace(t *testing.T, options *KubectlOptions, namespaceName string) {
require.NoError(t, CreateNamespaceE(t, options, namespaceName))
}
// CreateNamespaceE will create a new Kubernetes namespace on the cluster targeted by the provided options.
func CreateNamespaceE(t *testing.T, options *KubectlOptions, namespaceName string) error {
clientset, err := GetKubernetesClientFromOptionsE(t, options)
if err != nil {
return err
}
namespace := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName,
},
}
_, err = clientset.CoreV1().Namespaces().Create(&namespace)
return err
}
// GetNamespace will query the Kubernetes cluster targeted by the provided options for the requested namespace. This will
// fail the test if there is an error in getting the namespace or if the namespace doesn't exist.
func GetNamespace(t *testing.T, options *KubectlOptions, namespaceName string) *corev1.Namespace {
namespace, err := GetNamespaceE(t, options, namespaceName)
require.NoError(t, err)
require.NotNil(t, namespace)
return namespace
}
// GetNamespaceE will query the Kubernetes cluster targeted by the provided options for the requested namespace.
func GetNamespaceE(t *testing.T, options *KubectlOptions, namespaceName string) (*corev1.Namespace, error) {
clientset, err := GetKubernetesClientFromOptionsE(t, options)
if err != nil {
return nil, err
}
return clientset.CoreV1().Namespaces().Get(namespaceName, metav1.GetOptions{})
}
// DeleteNamespace will delete the requested namespace from the Kubernetes cluster targeted by the provided options. This will
// fail the test if there is an error in creating the namespace.
func DeleteNamespace(t *testing.T, options *KubectlOptions, namespaceName string) {
require.NoError(t, DeleteNamespaceE(t, options, namespaceName))
}
// DeleteNamespaceE will delete the requested namespace from the Kubernetes cluster targeted by the provided options.
func DeleteNamespaceE(t *testing.T, options *KubectlOptions, namespaceName string) error {
clientset, err := GetKubernetesClientFromOptionsE(t, options)
if err != nil {
return err
}
return clientset.CoreV1().Namespaces().Delete(namespaceName, &metav1.DeleteOptions{})
}