Merge "Add Juju/k8s support"
authortierno <alfonso.tiernosepulveda@telefonica.com>
Wed, 4 Dec 2019 21:26:56 +0000 (22:26 +0100)
committerGerrit Code Review <root@osm.etsi.org>
Wed, 4 Dec 2019 21:26:56 +0000 (22:26 +0100)
1  2 
osm_lcm/ns.py

diff --combined osm_lcm/ns.py
@@@ -26,6 -26,7 +26,7 @@@ from jinja2 import Environment, Templat
  from osm_lcm import ROclient
  from osm_lcm.lcm_utils import LcmException, LcmExceptionNoMgmtIP, LcmBase, deep_get
  from n2vc.k8s_helm_conn import K8sHelmConnector
+ from n2vc.k8s_juju_conn import K8sJujuConnector
  
  from osm_common.dbbase import DbException
  from osm_common.fsbase import FsException
@@@ -125,6 -126,15 +126,15 @@@ class NsLcm(LcmBase)
              on_update_db=None,
          )
  
+         self.k8sclusterjuju = K8sJujuConnector(
+             kubectl_command=self.vca_config.get("kubectlpath"),
+             juju_command=self.vca_config.get("jujupath"),
+             fs=self.fs,
+             log=self.logger,
+             db=self.db,
+             on_update_db=None,
+         )
          # create RO client
          self.RO = ROclient.ROClient(self.loop, **self.ro_config)
  
          ip_address = None
          nb_tries = 0
          target_vdu_id = None
 +        ro_retries = 0
  
          while True:
  
 +            ro_retries += 1
 +            if ro_retries >= 360:  # 1 hour
 +                raise LcmException("Not found _admin.deployed.RO.nsr_id for nsr_id: {}".format(nsr_id))
 +
              await asyncio.sleep(10, loop=self.loop)
              # wait until NS is deployed at RO
              if not ro_nsr_id:
              # get ip address
              if not target_vdu_id:
                  db_vnfr = self.db.get_one("vnfrs", {"_id": vnfr_id})
 -                if not vdu_id:
 +
 +                if not vdu_id:  # for the VNF case
                      ip_address = db_vnfr.get("ip-address")
                      if not ip_address:
                          continue
 -                for vdur in get_iterable(db_vnfr, "vdur"):
 -                    if (vdur["vdu-id-ref"] == vdu_id and vdur["count-index"] == vdu_index) or \
 -                            (ip_address and vdur.get("ip-address") == ip_address):
 -                        if vdur.get("status") == "ACTIVE":
 -                            target_vdu_id = vdur["vdu-id-ref"]
 -                        elif vdur.get("status") == "ERROR":
 -                            raise LcmException("Cannot inject ssh-key because target VM is in error state")
 -                        break
 -                else:
 +                    vdur = next((x for x in get_iterable(db_vnfr, "vdur") if x.get("ip-address") == ip_address), None)
 +                else:  # VDU case
 +                    vdur = next((x for x in get_iterable(db_vnfr, "vdur")
 +                                 if x.get("vdu-id-ref") == vdu_id and x.get("count-index") == vdu_index), None)
 +
 +                if not vdur:
                      raise LcmException("Not found vnfr_id={}, vdu_index={}, vdu_index={}".format(
                          vnfr_id, vdu_id, vdu_index
                      ))
  
 +                if vdur.get("status") == "ACTIVE":
 +                    ip_address = vdur.get("ip-address")
 +                    if not ip_address:
 +                        continue
 +                    target_vdu_id = vdur["vdu-id-ref"]
 +                elif vdur.get("status") == "ERROR":
 +                    raise LcmException("Cannot inject ssh-key because target VM is in error state")
 +
              if not target_vdu_id:
                  continue
  
                                                          params=desc_params, db_dict=db_dict, timeout=3600)
                          )
                      else:
-                         # TODO juju-bundle connector in place
-                         pass
+                         task = self.k8sclusterjuju.install(cluster_uuid=cluster_uuid, kdu_model=kdumodel,
+                                                            atomic=True, params=desc_params,
+                                                            db_dict=db_dict, timeout=600)
                      pending_tasks[task] = "_admin.deployed.K8s.{}.".format(index)
                      index += 1
              if not pending_tasks:
                                  self.k8sclusterhelm.uninstall(cluster_uuid=kdu.get("k8scluster-uuid"),
                                                                kdu_instance=kdu_instance))
                          elif kdu.get("k8scluster-type") == "juju":
-                             # TODO Juju connector needed
-                             continue
+                             task_delete_kdu_instance = asyncio.ensure_future(
+                                 self.k8sclusterjuju.uninstall(cluster_uuid=kdu.get("k8scluster-uuid"),
+                                                               kdu_instance=kdu_instance))
                          else:
                              self.error(logging_text + "Unknown k8s deployment type {}".
                                         format(kdu.get("k8scluster-type")))
                                                                             params=desc_params, db_dict=db_dict,
                                                                             timeout=300)
                              elif kdu.get("k8scluster-type") == "juju":
-                                 # TODO Juju connector needed
-                                 pass
+                                 output = await self.k8sclusterjuju.upgrade(cluster_uuid=kdu.get("k8scluster-uuid"),
+                                                                            kdu_instance=kdu.get("kdu-instance"),
+                                                                            atomic=True, kdu_model=kdu_model,
+                                                                            params=desc_params, db_dict=db_dict,
+                                                                            timeout=300)
                              else:
                                  msg = "k8scluster-type not defined"
                                  raise LcmException(msg)
                                                                              kdu_instance=kdu.get("kdu-instance"),
                                                                              db_dict=db_dict)
                              elif kdu.get("k8scluster-type") == "juju":
-                                 # TODO Juju connector needed
-                                 pass
+                                 output = await self.k8sclusterjuju.rollback(cluster_uuid=kdu.get("k8scluster-uuid"),
+                                                                             kdu_instance=kdu.get("kdu-instance"),
+                                                                             db_dict=db_dict)
                              else:
                                  msg = "k8scluster-type not defined"
                                  raise LcmException(msg)
                                  output = await self.k8sclusterhelm.status_kdu(cluster_uuid=kdu.get("k8scluster-uuid"),
                                                                                kdu_instance=kdu.get("kdu-instance"))
                              elif kdu.get("k8scluster-type") == "juju":
-                                 # TODO Juju connector needed
-                                 pass
+                                 output = await self.k8sclusterjuju.status_kdu(cluster_uuid=kdu.get("k8scluster-uuid"),
+                                                                               kdu_instance=kdu.get("kdu-instance"))
                              else:
                                  msg = "k8scluster-type not defined"
                                  raise LcmException(msg)