From d21509c14b0bef94d5ace71a4b5c7592851b453d Mon Sep 17 00:00:00 2001 From: Gabriel Cuba Date: Wed, 17 May 2023 01:30:15 -0500 Subject: [PATCH] Feature 10948: Set labels to new namespaces Change-Id: I0f40d198d398c79f3e9badd8def8f884f60fb7f8 Signed-off-by: Gabriel Cuba --- n2vc/k8s_helm_base_conn.py | 5 ++++- n2vc/kubectl.py | 5 +++-- n2vc/tests/unit/test_kubectl.py | 17 ++++++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/n2vc/k8s_helm_base_conn.py b/n2vc/k8s_helm_base_conn.py index 34d3129..2a588b4 100644 --- a/n2vc/k8s_helm_base_conn.py +++ b/n2vc/k8s_helm_base_conn.py @@ -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( diff --git a/n2vc/kubectl.py b/n2vc/kubectl.py index 7cc6ac2..c16c95a 100644 --- a/n2vc/kubectl.py +++ b/n2vc/kubectl.py @@ -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, ) diff --git a/n2vc/tests/unit/test_kubectl.py b/n2vc/tests/unit/test_kubectl.py index 6ba074f..a6d02ff 100644 --- a/n2vc/tests/unit/test_kubectl.py +++ b/n2vc/tests/unit/test_kubectl.py @@ -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, ) -- 2.25.1