Exemplo n.º 1
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
Exemplo n.º 2
0
    meta = k8s.client.V1ObjectMeta(namespace=test_host1.namespace)
    matching_svc = k8s.client.V1Service(metadata=meta)
    matching_svc.spec = k8s.client.V1ServiceSpec()
    assert test_host1.matches(matching_svc) is False


# Below: mergeInDict


def test_mergeInDict_emptyList_returnsEmptyDict():
    assert merge_in_dict([]) == {}


def test_mergeInDict_oneCase_returnsDict():
    case = NetworkTestCase(test_host1, test_host2, 80, False)
    expected = {test_host1.to_identifier(): {test_host2.to_identifier(): ["-80"]}}
    assert merge_in_dict([case]) == expected


def test_mergeInDict_twoCasesNoConflicts_returnsDict():
    case1 = NetworkTestCase(test_host1, test_host2, 80, False)
    case2 = NetworkTestCase(test_host2, test_host1, 80, False)
    expected = {
        test_host1.to_identifier(): {test_host2.to_identifier(): ["-80"]},
        test_host2.to_identifier(): {test_host1.to_identifier(): ["-80"]},
    }
    assert merge_in_dict([case1, case2]) == expected


def test_mergeInDict_twoCasesSameFromHostDifferentToHost_returnsDict():
    case1 = NetworkTestCase(test_host1, test_host2, 80, False)
Exemplo n.º 3
0
    matching_svc = k8s.client.V1Service(metadata=meta)
    matching_svc.spec = k8s.client.V1ServiceSpec()
    assert test_host1.matches(matching_svc) is False


# Below: mergeInDict


def test_mergeInDict_emptyList_returnsEmptyDict():
    assert merge_in_dict([]) == {}


def test_mergeInDict_oneCase_returnsDict():
    case = NetworkTestCase(test_host1, test_host2, 80, False)
    expected = {
        test_host1.to_identifier(): {
            test_host2.to_identifier(): ["-80"]
        }
    }
    assert merge_in_dict([case]) == expected


def test_mergeInDict_twoCasesNoConflicts_returnsDict():
    case1 = NetworkTestCase(test_host1, test_host2, 80, False)
    case2 = NetworkTestCase(test_host2, test_host1, 80, False)
    expected = {
        test_host1.to_identifier(): {
            test_host2.to_identifier(): ["-80"]
        },
        test_host2.to_identifier(): {
            test_host1.to_identifier(): ["-80"]
Exemplo n.º 4
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()
             ]
             # ToDo we should use the cluser ip instead of the DNS names
             # so we don't need the lookups
             service_name = "svc-%s" % convert_to_resource_name(
                 host.to_identifier())
             svc = create_service_manifest(
                 host,
                 {pod_labels_tuple[0]: pod_labels_tuple[1]},
                 {
                     ROLE_LABEL: "test_target_svc",
                     CLEANUP_LABEL: CLEANUP_ALWAYS
                 },
                 service_name,
                 target_ports,
             )
             target_pod_namespace = host.namespace
             service_names_per_host[host_string] = "%s:%s" % (
                 target_pod_namespace,
                 service_name,
             )
             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):
                 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] = "%s:%s" % (
                 services_for_host[0].metadata.namespace,
                 services_for_host[0].metadata.name,
             )
     return service_names_per_host, port_dict_per_host