Skip to content

Commit

Permalink
Add func for creating a namespace by passing in the entire ObjectMeta…
Browse files Browse the repository at this point in the history
… to allow more configuration (setting labels)
  • Loading branch information
cbuto committed Feb 4, 2021
1 parent 78a1263 commit d7dcf4b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ k8s.io/api v0.19.3/go.mod h1:VF+5FT1B74Pw3KxMdKyinLo+zynBaMBiAfGMuldcNDs=
k8s.io/apimachinery v0.17.0/go.mod h1:b9qmWdKlLuU9EBh+06BtLcSf/Mu89rWL33naRxs1uZg=
k8s.io/apimachinery v0.19.3 h1:bpIQXlKjB4cB/oNpnNnV+BybGPR7iP5oYpsOTEJ4hgc=
k8s.io/apimachinery v0.19.3/go.mod h1:DnPGDnARWFvYa3pMHgSxtbZb7gpzzAZ1pTfaUNDVlmA=
k8s.io/apimachinery v0.20.1 h1:LAhz8pKbgR8tUwn7boK+b2HZdt7MiTu2mkYtFMUjTRQ=
k8s.io/apiserver v0.17.0/go.mod h1:ABM+9x/prjINN6iiffRVNCBR2Wk7uY4z+EtEGZD48cg=
k8s.io/client-go v0.17.0/go.mod h1:TYgR6EUHs6k45hb6KWjVD6jFZvJV4gHDikv/It0xz+k=
k8s.io/client-go v0.19.3 h1:ctqR1nQ52NUs6LpI0w+a5U+xjYwflFwA13OJKcicMxg=
Expand Down
14 changes: 11 additions & 3 deletions modules/k8s/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ func CreateNamespace(t testing.TestingT, options *KubectlOptions, namespaceName

// CreateNamespaceE will create a new Kubernetes namespace on the cluster targeted by the provided options.
func CreateNamespaceE(t testing.TestingT, options *KubectlOptions, namespaceName string) error {
namespaceObject := metav1.ObjectMeta{
Name: namespaceName,
}
err := CreateNamespaceWithMetadataE(t, options, namespaceObject)
return err
}

// CreateNamespaceWithMetadataE will create a new Kubernetes namespace on the cluster targeted by the provided options and
// expects the entire namespace ObjectMeta to be passed in.
func CreateNamespaceWithMetadataE(t testing.TestingT, options *KubectlOptions, namespaceObjectMeta metav1.ObjectMeta) error {
clientset, err := GetKubernetesClientFromOptionsE(t, options)
if err != nil {
return err
}

namespace := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName,
},
ObjectMeta: namespaceObjectMeta,
}
_, err = clientset.CoreV1().Namespaces().Create(context.Background(), &namespace, metav1.CreateOptions{})
return err
Expand Down
25 changes: 25 additions & 0 deletions modules/k8s/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/gruntwork-io/terratest/modules/random"
)
Expand All @@ -34,3 +35,27 @@ func TestNamespaces(t *testing.T) {
namespace := GetNamespace(t, options, namespaceName)
require.Equal(t, namespace.Name, namespaceName)
}

func TestNamespaceWithLabels(t *testing.T) {
t.Parallel()

uniqueId := random.UniqueId()
namespaceName := strings.ToLower(uniqueId)
options := NewKubectlOptions("", "", namespaceName)
namespaceLabels := map[string]string{"foo": "bar"}
namespaceObjectMetaWithLabels := metav1.ObjectMeta{
Name: namespaceName,
Labels: namespaceLabels,
}
err := CreateNamespaceWithMetadataE(t, options, namespaceObjectMetaWithLabels)
require.NoError(t, err)
defer func() {
DeleteNamespace(t, options, namespaceName)
namespace := GetNamespace(t, options, namespaceName)
require.Equal(t, namespace.Status.Phase, corev1.NamespaceTerminating)
}()

namespace := GetNamespace(t, options, namespaceName)
require.Equal(t, namespace.Name, namespaceName)
require.Equal(t, namespace.Labels, namespaceLabels)
}

0 comments on commit d7dcf4b

Please sign in to comment.