github.com/mponton/terratest@v0.44.0/modules/k8s/namespace.go (about)

     1  package k8s
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/mponton/terratest/modules/testing"
     7  	"github.com/stretchr/testify/require"
     8  	corev1 "k8s.io/api/core/v1"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  )
    11  
    12  // CreateNamespace will create a new Kubernetes namespace on the cluster targeted by the provided options. This will
    13  // fail the test if there is an error in creating the namespace.
    14  func CreateNamespace(t testing.TestingT, options *KubectlOptions, namespaceName string) {
    15  	require.NoError(t, CreateNamespaceE(t, options, namespaceName))
    16  }
    17  
    18  // CreateNamespaceE will create a new Kubernetes namespace on the cluster targeted by the provided options.
    19  func CreateNamespaceE(t testing.TestingT, options *KubectlOptions, namespaceName string) error {
    20  	namespaceObject := metav1.ObjectMeta{
    21  		Name: namespaceName,
    22  	}
    23  	return CreateNamespaceWithMetadataE(t, options, namespaceObject)
    24  }
    25  
    26  // CreateNamespaceWithMetadataE will create a new Kubernetes namespace on the cluster targeted by the provided options and
    27  // with the provided metadata. This method expects the entire namespace ObjectMeta to be passed in, so you'll need to set the name within the ObjectMeta struct yourself.
    28  func CreateNamespaceWithMetadataE(t testing.TestingT, options *KubectlOptions, namespaceObjectMeta metav1.ObjectMeta) error {
    29  	clientset, err := GetKubernetesClientFromOptionsE(t, options)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	namespace := corev1.Namespace{
    35  		ObjectMeta: namespaceObjectMeta,
    36  	}
    37  	_, err = clientset.CoreV1().Namespaces().Create(context.Background(), &namespace, metav1.CreateOptions{})
    38  	return err
    39  }
    40  
    41  // CreateNamespaceWithMetadata will create a new Kubernetes namespace on the cluster targeted by the provided options and
    42  // with the provided metadata. This method expects the entire namespace ObjectMeta to be passed in, so you'll need to set the name within the ObjectMeta struct yourself.
    43  // This will fail the test if there is an error while creating the namespace.
    44  func CreateNamespaceWithMetadata(t testing.TestingT, options *KubectlOptions, namespaceObjectMeta metav1.ObjectMeta) {
    45  	require.NoError(t, CreateNamespaceWithMetadataE(t, options, namespaceObjectMeta))
    46  }
    47  
    48  // GetNamespace will query the Kubernetes cluster targeted by the provided options for the requested namespace. This will
    49  // fail the test if there is an error in getting the namespace or if the namespace doesn't exist.
    50  func GetNamespace(t testing.TestingT, options *KubectlOptions, namespaceName string) *corev1.Namespace {
    51  	namespace, err := GetNamespaceE(t, options, namespaceName)
    52  	require.NoError(t, err)
    53  	require.NotNil(t, namespace)
    54  	return namespace
    55  }
    56  
    57  // GetNamespaceE will query the Kubernetes cluster targeted by the provided options for the requested namespace.
    58  func GetNamespaceE(t testing.TestingT, options *KubectlOptions, namespaceName string) (*corev1.Namespace, error) {
    59  	clientset, err := GetKubernetesClientFromOptionsE(t, options)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	return clientset.CoreV1().Namespaces().Get(context.Background(), namespaceName, metav1.GetOptions{})
    65  }
    66  
    67  // DeleteNamespace will delete the requested namespace from the Kubernetes cluster targeted by the provided options. This will
    68  // fail the test if there is an error in creating the namespace.
    69  func DeleteNamespace(t testing.TestingT, options *KubectlOptions, namespaceName string) {
    70  	require.NoError(t, DeleteNamespaceE(t, options, namespaceName))
    71  }
    72  
    73  // DeleteNamespaceE will delete the requested namespace from the Kubernetes cluster targeted by the provided options.
    74  func DeleteNamespaceE(t testing.TestingT, options *KubectlOptions, namespaceName string) error {
    75  	clientset, err := GetKubernetesClientFromOptionsE(t, options)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	return clientset.CoreV1().Namespaces().Delete(context.Background(), namespaceName, metav1.DeleteOptions{})
    81  }