Ejemplo n.º 1
0
 def _get_target_names_creating_them_if_missing(self, target_dict,
                                                api: k8s.client.CoreV1Api):
     service_names_per_host = {}
     port_dict_per_host = {}
     for host_string in target_dict.keys():
         host = Host.from_identifier(host_string)
         if isinstance(host, GenericClusterHost):
             self.logger.debug(
                 "Found GenericClusterHost %s,"
                 "Rewriting it to a ClusterHost in default namespace now.",
                 host,
             )
             host = ClusterHost("default", host.pod_labels)
         if not isinstance(host, ClusterHost):
             raise ValueError(
                 "Only ClusterHost targets are supported by this Orchestrator."
                 " Host: %s, hostString: %s" % (host, host_string))
         self.logger.debug("Searching service for host %s", host)
         services_for_host = [
             svc for svc in self._current_services if host.matches(svc)
         ]
         self.logger.debug(
             "Found services %s for host %s ",
             [svc.metadata for svc in services_for_host],
             host,
         )
         rewritten_ports = self._rewrite_ports_for_host(
             target_dict[host_string], services_for_host)
         self.logger.debug("Rewritten ports: %s", rewritten_ports)
         port_dict_per_host[host_string] = rewritten_ports
         if not services_for_host:
             gen_name = "%s-test-target-pod-" % PROJECT_PREFIX
             target_container = k8s.client.V1Container(
                 image=self.oci_images["target"], name="runner")
             pod_labels_tuple = (ROLE_LABEL, "test_target_pod")
             target_pod = create_pod_manifest(
                 host=host,
                 additional_labels={
                     pod_labels_tuple[0]: pod_labels_tuple[1],
                     CLEANUP_LABEL: CLEANUP_ALWAYS,
                 },
                 generate_name=gen_name,
                 container=target_container,
             )
             target_ports = [
                 int(port.replace("-", ""))
                 for port in port_dict_per_host[host_string].values()
             ]
             svc = create_service_manifest(
                 host,
                 {pod_labels_tuple[0]: pod_labels_tuple[1]},
                 {
                     ROLE_LABEL: "test_target_svc",
                     CLEANUP_LABEL: CLEANUP_ALWAYS
                 },
                 target_ports,
             )
             target_pod_namespace = host.namespace
             resp = api.create_namespaced_pod(
                 namespace=target_pod_namespace, body=target_pod)
             if isinstance(resp, k8s.client.V1Pod):
                 self.logger.debug("Target pod %s created succesfully",
                                   resp.metadata.name)
                 self._current_pods.append(resp)
             else:
                 self.logger.error("Failed to create pod! Resp: %s", resp)
             resp = api.create_namespaced_service(namespace=host.namespace,
                                                  body=svc)
             if isinstance(resp, k8s.client.V1Service):
                 service_names_per_host[host_string] = resp.spec.cluster_ip
                 self.logger.debug("Target svc %s created succesfully",
                                   resp.metadata.name)
                 self._current_services.append(resp)
             else:
                 self.logger.error("Failed to create target svc! Resp: %s",
                                   resp)
         else:
             service_names_per_host[host_string] = services_for_host[
                 0].spec.cluster_ip
     return service_names_per_host, port_dict_per_host
Ejemplo n.º 2
0
 def _get_target_names_creating_them_if_missing(self, target_dict,
                                                api: k8s.client.CoreV1Api):
     svc_names_per_host = {}
     port_dict_per_host = {}
     for host_string in target_dict.keys():
         host = Host.from_identifier(host_string)
         if isinstance(host, GenericClusterHost):
             logger.debug(
                 "Found GenericClusterHost " + str(host) +
                 ". Rewriting it to a ClusterHost in default namespace now."
             )
             host = ClusterHost("default", host.pod_labels)
         if not isinstance(host, ClusterHost):
             raise ValueError(
                 "Only ClusterHost targets are supported by this Orchestrator. Host: "
                 + str(host) + ", hostString: " + host_string)
         logger.debug("Searching service for host " + str(host))
         services_for_host = [
             svc for svc in self._current_services if host.matches(svc)
         ]
         logger.debug("Found services {} for host {} ".format(
             [svc.metadata for svc in services_for_host], host))
         rewritten_ports = self._rewrite_ports_for_host(
             target_dict[host_string], services_for_host)
         logger.debug("Rewritten ports: " + str(rewritten_ports))
         port_dict_per_host[host_string] = rewritten_ports
         if not services_for_host:
             gen_name = PROJECT_PREFIX + "-test-target-pod-"
             target_container = k8s.client.V1Container(
                 image=self.target_image, name="runner")
             pod_labels_tuple = (ROLE_LABEL, "test_target_pod")
             target_pod = init_pod(host=host,
                                   additional_labels={
                                       pod_labels_tuple[0]:
                                       pod_labels_tuple[1],
                                       CLEANUP_LABEL: CLEANUP_ALWAYS
                                   },
                                   generate_name=gen_name,
                                   container=target_container)
             target_ports = [
                 int(port.replace("-", ""))
                 for port in port_dict_per_host[host_string].values()
             ]
             # ToDo we should use the cluser ip instead of the DNS names
             # so we don't need the lookups
             svc_name = "svc-" + convert_to_resource_name(
                 host.to_identifier())
             svc = init_svc(host,
                            {pod_labels_tuple[0]: pod_labels_tuple[1]}, {
                                 ROLE_LABEL: "test_target_svc",
                                 CLEANUP_LABEL: CLEANUP_ALWAYS
                             }, svc_name, target_ports)
             target_pod_namespace = host.namespace
             svc_names_per_host[
                 host_string] = target_pod_namespace + ":" + svc_name
             resp = api.create_namespaced_pod(
                 namespace=target_pod_namespace, body=target_pod)
             if isinstance(resp, k8s.client.V1Pod):
                 logger.debug("Target pod " + resp.metadata.name +
                              " created succesfully")
                 self._current_pods.append(resp)
             else:
                 logger.error("Failed to create pod! Resp: " + str(resp))
             resp = api.create_namespaced_service(namespace=host.namespace,
                                                  body=svc)
             if isinstance(resp, k8s.client.V1Service):
                 logger.debug("Target svc " + resp.metadata.name +
                              " created succesfully")
                 self._current_services.append(resp)
             else:
                 logger.error("Failed to create target svc! Resp: " +
                              str(resp))
         else:
             svc_names_per_host[host_string] = services_for_host[
                 0].metadata.namespace + ":" + services_for_host[
                     0].metadata.name
     return svc_names_per_host, port_dict_per_host