Feature 10948: Set labels to new namespaces 01/13401/4
authorGabriel Cuba <gcuba@whitestack.com>
Wed, 17 May 2023 06:30:15 +0000 (01:30 -0500)
committerGabriel Cuba <gcuba@whitestack.com>
Mon, 29 May 2023 05:48:55 +0000 (00:48 -0500)
Change-Id: I0f40d198d398c79f3e9badd8def8f884f60fb7f8
Signed-off-by: Gabriel Cuba <gcuba@whitestack.com>
n2vc/k8s_helm_base_conn.py
n2vc/kubectl.py
n2vc/tests/unit/test_kubectl.py

index 34d3129..2a588b4 100644 (file)
@@ -2118,12 +2118,14 @@ class K8sHelmBaseConnector(K8sConnector):
         self,
         namespace,
         cluster_uuid,
+        labels,
     ):
         """
         Create a namespace in a specific cluster
 
-        :param namespace: namespace to be created
+        :param namespace:    Namespace to be created
         :param cluster_uuid: K8s cluster uuid used to retrieve kubeconfig
+        :param labels:       Dictionary with labels for the new namespace
         :returns: None
         """
         paths, env = self._init_paths_env(
@@ -2132,6 +2134,7 @@ class K8sHelmBaseConnector(K8sConnector):
         kubectl = Kubectl(config_file=paths["kube_config"])
         await kubectl.create_namespace(
             name=namespace,
+            labels=labels,
         )
 
     async def delete_namespace(
index 7cc6ac2..c16c95a 100644 (file)
@@ -574,15 +574,16 @@ class Kubectl:
         delay=1,
         fallback=Exception("Failed creating the namespace"),
     )
-    async def create_namespace(self, name: str):
+    async def create_namespace(self, name: str, labels: dict = None):
         """
         Create a namespace
 
         :param: name:       Name of the namespace to be created
+        :param: labels:     Dictionary with labels for the new namespace
 
         """
         v1_core = self.clients[CORE_CLIENT]
-        metadata = V1ObjectMeta(name=name)
+        metadata = V1ObjectMeta(name=name, labels=labels)
         namespace = V1Namespace(
             metadata=metadata,
         )
index 6ba074f..a6d02ff 100644 (file)
@@ -734,6 +734,7 @@ class CreateNamespaceClass(asynctest.TestCase):
     def setUp(self, mock_load_kube_config):
         super(CreateNamespaceClass, self).setUp()
         self.namespace = "osm"
+        self.labels = {"key": "value"}
         self.kubectl = Kubectl()
 
     @asynctest.fail_on(active_handles=True)
@@ -741,7 +742,21 @@ class CreateNamespaceClass(asynctest.TestCase):
         self,
         mock_create_namespace,
     ):
-        metadata = V1ObjectMeta(name=self.namespace)
+        metadata = V1ObjectMeta(name=self.namespace, labels=self.labels)
+        namespace = V1Namespace(
+            metadata=metadata,
+        )
+        await self.kubectl.create_namespace(
+            name=self.namespace,
+            labels=self.labels,
+        )
+        mock_create_namespace.assert_called_once_with(namespace)
+
+    async def test_namespace_is_created_default_labels(
+        self,
+        mock_create_namespace,
+    ):
+        metadata = V1ObjectMeta(name=self.namespace, labels=None)
         namespace = V1Namespace(
             metadata=metadata,
         )