"""
+ @abc.abstractmethod
+ async def get_services(self,
+ cluster_uuid: str,
+ kdu_instance: str,
+ namespace: str) -> list:
+ """
+ Returns a list of services defined for the specified kdu instance.
+
+ :param cluster_uuid: UUID of a K8s cluster known by OSM
+ :param kdu_instance: unique name for the KDU instance
+ :param namespace: K8s namespace used by the KDU instance
+ :return: If successful, it will return a list of services, Each service
+ can have the following data:
+ - `name` of the service
+ - `type` type of service in the k8 cluster
+ - `ports` List of ports offered by the service, for each port includes at least
+ name, port, protocol
+ - `cluster_ip` Internal ip to be used inside k8s cluster
+ - `external_ip` List of external ips (in case they are available)
+ """
+
+ @abc.abstractmethod
+ async def get_service(self,
+ cluster_uuid: str,
+ service_name: str,
+ namespace: str = None) -> object:
+ """
+ Obtains the data of the specified service in the k8cluster.
+
+ :param cluster_uuid: UUID of a K8s cluster known by OSM
+ :param service_name: name of the K8s service in the specified namespace
+ :param namespace: K8s namespace used by the KDU instance
+ :return: If successful, it will return a list of services, Each service can have
+ the following data:
+ - `name` of the service
+ - `type` type of service in the k8 cluster
+ - `ports` List of ports offered by the service, for each port includes at least
+ name, port, protocol
+ - `cluster_ip` Internal ip to be used inside k8s cluster
+ - `external_ip` List of external ips (in case they are available)
+ """
+
"""
####################################################################################
################################### P R I V A T E ##################################
return_text=True,
)
+ async def get_services(self,
+ cluster_uuid: str,
+ kdu_instance: str,
+ namespace: str) -> list:
+
+ self.log.debug(
+ "get_services: cluster_uuid: {}, kdu_instance: {}".format(
+ cluster_uuid, kdu_instance
+ )
+ )
+
+ status = await self._status_kdu(
+ cluster_uuid, kdu_instance, return_text=False
+ )
+
+ service_names = self._parse_helm_status_service_info(status)
+ service_list = []
+ for service in service_names:
+ service = await self.get_service(cluster_uuid, service, namespace)
+ service_list.append(service)
+
+ return service_list
+
+ async def get_service(self,
+ cluster_uuid: str,
+ service_name: str,
+ namespace: str) -> object:
+
+ self.log.debug(
+ "get service, service_name: {}, namespace: {}, cluster_uuid: {}".format(
+ service_name, namespace, cluster_uuid)
+ )
+
+ # get paths
+ _kube_dir, helm_dir, config_filename, _cluster_dir = self._get_paths(
+ cluster_name=cluster_uuid, create_if_not_exist=True
+ )
+
+ command = "{} --kubeconfig={} --namespace={} get service {} -o=yaml".format(
+ self.kubectl_command, config_filename, namespace, service_name
+ )
+
+ output, _rc = await self._local_async_exec(
+ command=command, raise_exception_on_error=True
+ )
+
+ data = yaml.load(output, Loader=yaml.SafeLoader)
+
+ service = {
+ "name": service_name,
+ "type": self._get_deep(data, ("spec", "type")),
+ "ports": self._get_deep(data, ("spec", "ports")),
+ "cluster_ip": self._get_deep(data, ("spec", "clusterIP"))
+ }
+ if service["type"] == "LoadBalancer":
+ ip_map_list = self._get_deep(data, ("status", "loadBalancer", "ingress"))
+ ip_list = [elem["ip"] for elem in ip_map_list]
+ service["external_ip"] = ip_list
+
+ return service
+
async def synchronize_repos(self, cluster_uuid: str):
_, cluster_id = self._get_namespace_cluster_id(cluster_uuid)
self.log.debug("Task cancelled")
return
except Exception as e:
- self.log.debug("_store_status exception: {}".format(str(e)))
+ self.log.debug("_store_status exception: {}".format(str(e)), exc_info=True)
pass
finally:
if run_once:
return ready
+ def _parse_helm_status_service_info(self, status):
+
+ # extract info.status.resources-> str
+ # format:
+ # ==> v1/Deployment
+ # NAME READY UP-TO-DATE AVAILABLE AGE
+ # halting-horse-mongodb 0/1 1 0 0s
+ # halting-petit-mongodb 1/1 1 0 0s
+ # blank line
+ resources = K8sHelmConnector._get_deep(status, ("info", "status", "resources"))
+
+ service_list = []
+ first_line_skipped = service_found = False
+ for line in resources:
+ if not service_found:
+ if len(line) >= 2 and line[0] == "==>" and line[1] == "v1/Service":
+ service_found = True
+ continue
+ else:
+ if len(line) >= 2 and line[0] == "==>":
+ service_found = first_line_skipped = False
+ continue
+ if not line:
+ continue
+ if not first_line_skipped:
+ first_line_skipped = True
+ continue
+ service_list.append(line[0])
+
+ return service_list
+
@staticmethod
def _get_deep(dictionary: dict, members: tuple):
target = dictionary