Pin pylint version to 3.1.1 in tox.ini
[osm/LCM.git] / osm_lcm / lcm_helm_conn.py
index d1ad4c5..930ec60 100644 (file)
@@ -34,7 +34,6 @@ from osm_lcm.data_utils.database.database import Database
 from osm_lcm.data_utils.filesystem.filesystem import Filesystem
 
 from n2vc.n2vc_conn import N2VCConnector
-from n2vc.k8s_helm_conn import K8sHelmConnector
 from n2vc.k8s_helm3_conn import K8sHelm3Connector
 from n2vc.exceptions import (
     N2VCBadArgumentsException,
@@ -79,21 +78,16 @@ def retryer(max_wait_time_var="_initial_retry_time", delay_time_var="_retry_dela
 
 
 def create_secure_context(
-    trusted: str,
+    trusted: str, client_cert_path: str, client_key_path: str
 ) -> ssl.SSLContext:
     ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
     ctx.verify_mode = ssl.CERT_REQUIRED
     ctx.check_hostname = True
     ctx.minimum_version = ssl.TLSVersion.TLSv1_2
-    # TODO: client TLS
-    # ctx.load_cert_chain(str(client_cert), str(client_key))
+    ctx.load_cert_chain(client_cert_path, client_key_path)
     ctx.load_verify_locations(trusted)
     ctx.set_ciphers("ECDHE+AESGCM:ECDHE+CHACHA20:DHE+AESGCM:DHE+CHACHA20")
     ctx.set_alpn_protocols(["h2"])
-    try:
-        ctx.set_npn_protocols(["h2"])
-    except NotImplementedError:
-        pass
     return ctx
 
 
@@ -101,7 +95,6 @@ class LCMHelmConn(N2VCConnector, LcmBase):
     def __init__(
         self,
         log: object = None,
-        loop: object = None,
         vca_config: VcaConfig = None,
         on_update_db=None,
     ):
@@ -114,7 +107,7 @@ class LCMHelmConn(N2VCConnector, LcmBase):
 
         # parent class constructor
         N2VCConnector.__init__(
-            self, log=log, loop=loop, on_update_db=on_update_db, db=self.db, fs=self.fs
+            self, log=log, on_update_db=on_update_db, db=self.db, fs=self.fs
         )
 
         self.vca_config = vca_config
@@ -129,16 +122,7 @@ class LCMHelmConn(N2VCConnector, LcmBase):
         self._max_retry_time = self.vca_config.helm_max_retry_time
         self.log.debug("Retry time: {}".format(self._max_retry_time))
 
-        # initialize helm connector for helmv2 and helmv3
-        self._k8sclusterhelm2 = K8sHelmConnector(
-            kubectl_command=self.vca_config.kubectlpath,
-            helm_command=self.vca_config.helmpath,
-            fs=self.fs,
-            db=self.db,
-            log=self.log,
-            on_update_db=None,
-        )
-
+        # initialize helm connector for helmv3
         self._k8sclusterhelm3 = K8sHelm3Connector(
             kubectl_command=self.vca_config.kubectlpath,
             helm_command=self.vca_config.helm3path,
@@ -186,11 +170,14 @@ class LCMHelmConn(N2VCConnector, LcmBase):
               (e.g. stable/openldap, stable/openldap:1.2.4)
             - a path to a packaged chart (e.g. mychart.tgz)
             - a path to an unpacked chart directory or a URL (e.g. mychart)
-        :param str vca_type:  Type of vca, must be type helm or helm-v3
+        :param str vca_type:  Type of vca, must be type helm-v3
         :returns str, dict: id of the new execution environment including namespace.helm_id
         and credentials object set to None as all credentials should be osm kubernetes .kubeconfig
         """
 
+        if not namespace:
+            namespace = self.vca_config.kubectl_osm_namespace
+
         self.log.info(
             "create_execution_environment: namespace: {}, artifact_path: {}, "
             "chart_model: {}, db_dict: {}, reuse_ee_id: {}".format(
@@ -245,38 +232,21 @@ class LCMHelmConn(N2VCConnector, LcmBase):
                 config["global"]["osm"] = config.get("osm")
 
             self.log.debug("install helm chart: {}".format(full_path))
-            if vca_type == "helm":
-                helm_id = self._k8sclusterhelm2.generate_kdu_instance_name(
-                    db_dict=db_dict,
-                    kdu_model=kdu_model,
-                )
-                await self._k8sclusterhelm2.install(
-                    system_cluster_uuid,
-                    kdu_model=kdu_model,
-                    kdu_instance=helm_id,
-                    namespace=self.vca_config.kubectl_osm_namespace,
-                    params=config,
-                    db_dict=db_dict,
-                    timeout=progress_timeout,
-                )
-            else:
-                helm_id = self._k8sclusterhelm2.generate_kdu_instance_name(
-                    db_dict=db_dict,
-                    kdu_model=kdu_model,
-                )
-                await self._k8sclusterhelm3.install(
-                    system_cluster_uuid,
-                    kdu_model=kdu_model,
-                    kdu_instance=helm_id,
-                    namespace=self.vca_config.kubectl_osm_namespace,
-                    params=config,
-                    db_dict=db_dict,
-                    timeout=progress_timeout,
-                )
-
-            ee_id = "{}:{}.{}".format(
-                vca_type, self.vca_config.kubectl_osm_namespace, helm_id
+            helm_id = self._k8sclusterhelm3.generate_kdu_instance_name(
+                db_dict=db_dict,
+                kdu_model=kdu_model,
             )
+            await self._k8sclusterhelm3.install(
+                system_cluster_uuid,
+                kdu_model=kdu_model,
+                kdu_instance=helm_id,
+                namespace=namespace,
+                params=config,
+                db_dict=db_dict,
+                timeout=progress_timeout,
+            )
+
+            ee_id = "{}:{}.{}".format(vca_type, namespace, helm_id)
             return ee_id, None
         except N2VCException:
             raise
@@ -311,7 +281,7 @@ class LCMHelmConn(N2VCConnector, LcmBase):
         :param float total_timeout:
         :param dict config:  General variables to instantiate KDU
         :param str artifact_path:  path of package content
-        :param str vca_type:  Type of vca, must be type helm or helm-v3
+        :param str vca_type:  Type of vca, must be type helm-v3
         :returns str, dict: id of the new execution environment including namespace.helm_id
         and credentials object set to None as all credentials should be osm kubernetes .kubeconfig
         """
@@ -366,28 +336,16 @@ class LCMHelmConn(N2VCConnector, LcmBase):
                 config["global"]["osm"] = config.get("osm")
 
             self.log.debug("Ugrade helm chart: {}".format(full_path))
-            if vca_type == "helm":
-                await self._k8sclusterhelm2.upgrade(
-                    system_cluster_uuid,
-                    kdu_model=full_path,
-                    kdu_instance=helm_id,
-                    namespace=namespace,
-                    params=config,
-                    db_dict=db_dict,
-                    timeout=progress_timeout,
-                    force=True,
-                )
-            else:
-                await self._k8sclusterhelm3.upgrade(
-                    system_cluster_uuid,
-                    kdu_model=full_path,
-                    kdu_instance=helm_id,
-                    namespace=namespace,
-                    params=config,
-                    db_dict=db_dict,
-                    timeout=progress_timeout,
-                    force=True,
-                )
+            await self._k8sclusterhelm3.upgrade(
+                system_cluster_uuid,
+                kdu_model=full_path,
+                kdu_instance=helm_id,
+                namespace=namespace,
+                params=config,
+                db_dict=db_dict,
+                timeout=progress_timeout,
+                force=True,
+            )
 
         except N2VCException:
             raise
@@ -428,6 +386,37 @@ class LCMHelmConn(N2VCConnector, LcmBase):
             certificate_name=certificate_name,
         )
 
+    async def setup_ns_namespace(
+        self,
+        name: str,
+    ):
+        # Obtain system cluster id from database
+        system_cluster_uuid = await self._get_system_cluster_id()
+        await self._k8sclusterhelm3.create_namespace(
+            namespace=name,
+            cluster_uuid=system_cluster_uuid,
+            labels={
+                "pod-security.kubernetes.io/enforce": self.vca_config.eegrpc_pod_admission_policy
+            },
+        )
+        await self._k8sclusterhelm3.setup_default_rbac(
+            name="ee-role",
+            namespace=name,
+            api_groups=[""],
+            resources=["secrets"],
+            verbs=["get"],
+            service_account="default",
+            cluster_uuid=system_cluster_uuid,
+        )
+        await self._k8sclusterhelm3.copy_secret_data(
+            src_secret="osm-ca",
+            dst_secret="osm-ca",
+            src_namespace=self.vca_config.kubectl_osm_namespace,
+            dst_namespace=name,
+            cluster_uuid=system_cluster_uuid,
+            data_key="ca.crt",
+        )
+
     async def register_execution_environment(
         self,
         namespace: str,
@@ -680,12 +669,7 @@ class LCMHelmConn(N2VCConnector, LcmBase):
             # Get helm_id
             version, namespace, helm_id = get_ee_id_parts(ee_id)
 
-            # Uninstall chart, for backward compatibility we must assume that if there is no
-            # version it is helm-v2
-            if version == "helm-v3":
-                await self._k8sclusterhelm3.uninstall(system_cluster_uuid, helm_id)
-            else:
-                await self._k8sclusterhelm2.uninstall(system_cluster_uuid, helm_id)
+            await self._k8sclusterhelm3.uninstall(system_cluster_uuid, helm_id)
             self.log.info("ee_id: {} deleted".format(ee_id))
         except N2VCException:
             raise
@@ -698,8 +682,12 @@ class LCMHelmConn(N2VCConnector, LcmBase):
     async def delete_namespace(
         self, namespace: str, db_dict: dict = None, total_timeout: float = None
     ):
-        # method not implemented for this connector, execution environments must be deleted individually
-        pass
+        # Obtain system cluster id from database
+        system_cluster_uuid = await self._get_system_cluster_id()
+        await self._k8sclusterhelm3.delete_namespace(
+            namespace=namespace,
+            cluster_uuid=system_cluster_uuid,
+        )
 
     async def install_k8s_proxy_charm(
         self,
@@ -772,7 +760,11 @@ class LCMHelmConn(N2VCConnector, LcmBase):
                 else:
                     return "ERROR", "No result received"
 
-        ssl_context = create_secure_context(self.vca_config.ca_store)
+        ssl_context = create_secure_context(
+            self.vca_config.ca_store,
+            self.vca_config.client_cert_path,
+            self.vca_config.client_key_path,
+        )
         channel = Channel(
             ip_addr, self.vca_config.helm_ee_service_port, ssl=ssl_context
         )