KubernetesDiscoveryClient implements DiscoveryClient { @Override public List<ServiceInstance> getInstances(String serviceId) { Assert.notNull(serviceId, "[Assertion failed] - the object argument must not be null"); List<EndpointSubsetNS> subsetsNS = this.getEndPointsList(serviceId).stream() .map(endpoints -> getSubsetsFromEndpoints(endpoints)).collect(Collectors.toList()); List<ServiceInstance> instances = new ArrayList<>(); if (!subsetsNS.isEmpty()) { for (EndpointSubsetNS es : subsetsNS) { instances.addAll(this.getNamespaceServiceInstances(es, serviceId)); } } return instances; } KubernetesDiscoveryClient(KubernetesClient client,
			KubernetesDiscoveryProperties kubernetesDiscoveryProperties,
			KubernetesClientServicesFunction kubernetesClientServicesFunction);   KubernetesDiscoveryClient(KubernetesClient client, KubernetesDiscoveryProperties kubernetesDiscoveryProperties,
			KubernetesClientServicesFunction kubernetesClientServicesFunction,
			DefaultIsServicePortSecureResolver isServicePortSecureResolver); KubernetesClient getClient(); void setClient(KubernetesClient client); @Override String description(); @Override List<ServiceInstance> getInstances(String serviceId); List<Endpoints> getEndPointsList(String serviceId); @Override List<String> getServices(); List<String> getServices(Predicate<Service> filter); @Override int getOrder();  }
@Test public void getInstancesShouldBeAbleToHandleEndpointsSingleAddress() { Map<String, String> labels = new HashMap(); labels.put("l", "v"); Endpoints endPoint = new EndpointsBuilder().withNewMetadata().withName("endpoint").withNamespace("test") .withLabels(labels).endMetadata().addNewSubset().addNewAddress().withIp("ip1").withNewTargetRef() .withUid("10").endTargetRef().endAddress().addNewPort("http", "http_tcp", 80, "TCP").endSubset() .build(); List<Endpoints> endpointsList = new ArrayList<>(); endpointsList.add(endPoint); EndpointsList endpoints = new EndpointsList(); endpoints.setItems(endpointsList); mockServer.expect().get() .withPath( "/api/v1/namespaces/test/endpoints?labelSelector=l%3Dv&fieldSelector=metadata.name%3Dendpoint") .andReturn(200, endpoints).once(); mockServer.expect().get().withPath("/api/v1/endpoints?fieldSelector=metadata.name%3Dendpoint") .andReturn(200, endpoints).once(); mockServer.expect().get().withPath("/api/v1/namespaces/test/endpoints?fieldSelector=metadata.name%3Dendpoint") .andReturn(200, endpoints).once(); Service service = new ServiceBuilder().withNewMetadata().withName("endpoint").withNamespace("test") .withLabels(labels).endMetadata().build(); mockServer.expect().get().withPath("/api/v1/namespaces/test/services/endpoint").andReturn(200, service) .always(); final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(); properties.setServiceLabels(labels); properties.getMetadata().setAddLabels(false); properties.getMetadata().setAddAnnotations(false); final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, KubernetesClient::services, new DefaultIsServicePortSecureResolver(properties)); final List<ServiceInstance> instances = discoveryClient.getInstances("endpoint"); assertThat(instances).hasSize(1).filteredOn(s -> s.getHost().equals("ip1") && !s.isSecure()).hasSize(1) .filteredOn(s -> s.getInstanceId().equals("10")).hasSize(1); }
@Test public void getInstancesShouldBeAbleToHandleEndpointsSingleAddressAndMultiplePorts() { Map<String, String> labels = new HashMap(); labels.put("l2", "v2"); Endpoints endPoint1 = new EndpointsBuilder().withNewMetadata().withName("endpoint").withNamespace("test") .withLabels(labels).endMetadata().addNewSubset().addNewAddress().withIp("ip1").withNewTargetRef() .withUid("20").endTargetRef().endAddress().addNewPort("mgmt", "mgmt_tcp", 900, "TCP") .addNewPort("http", "http_tcp", 80, "TCP").endSubset().build(); List<Endpoints> endpointsList = new ArrayList<>(); endpointsList.add(endPoint1); EndpointsList endpoints = new EndpointsList(); endpoints.setItems(endpointsList); mockServer.expect().get().withPath( "/api/v1/namespaces/test/endpoints?labelSelector=l2%3Dv2&fieldSelector=metadata.name%3Dendpoint") .andReturn(200, endpoints).once(); mockServer.expect().get().withPath("/api/v1/namespaces/test/endpoints?fieldSelector=metadata.name%3Dendpoint") .andReturn(200, endpoints).once(); Service service = new ServiceBuilder().withNewMetadata().withName("endpoint").withNamespace("test") .withLabels(labels).withAnnotations(labels).endMetadata().build(); mockServer.expect().get().withPath("/api/v1/namespaces/test/services/endpoint").andReturn(200, service) .always(); final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(); properties.setPrimaryPortName("http_tcp"); final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, KubernetesClient::services, new DefaultIsServicePortSecureResolver(properties)); final List<ServiceInstance> instances = discoveryClient.getInstances("endpoint"); assertThat(instances).hasSize(1).filteredOn(s -> s.getHost().equals("ip1") && !s.isSecure()).hasSize(1) .filteredOn(s -> s.getInstanceId().equals("20")).hasSize(1).filteredOn(s -> 80 == s.getPort()) .hasSize(1); }
@Test public void getInstancesShouldBeAbleToHandleEndpointsMultipleAddresses() { Map<String, String> labels = new HashMap(); labels.put("l1", "v1"); Endpoints endPoint = new EndpointsBuilder().withNewMetadata().withName("endpoint").withNamespace("test") .withLabels(labels).endMetadata().addNewSubset().addNewAddress().withIp("ip1").withNewTargetRef() .withUid("40").endTargetRef().endAddress().addNewAddress().withIp("ip2").withNewTargetRef() .withUid("50").endTargetRef().endAddress().addNewPort("https", "https_tcp", 443, "TCP").endSubset() .build(); List<Endpoints> endpointsList = new ArrayList<>(); endpointsList.add(endPoint); EndpointsList endpoints = new EndpointsList(); endpoints.setItems(endpointsList); mockServer.expect().get().withPath( "/api/v1/namespaces/test/endpoints?labelSelector=l1%3Dv1&fieldSelector=metadata.name%3Dendpoint") .andReturn(200, endpoints).once(); mockServer.expect().get().withPath("/api/v1/namespaces/test/endpoints?fieldSelector=metadata.name%3Dendpoint") .andReturn(200, endpoints).once(); Service service = new ServiceBuilder().withNewMetadata().withName("endpoint").withNamespace("test") .withLabels(labels).endMetadata().build(); mockServer.expect().get().withPath("/api/v1/namespaces/test/services/endpoint").andReturn(200, service) .always(); final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(); properties.setServiceLabels(labels); properties.getMetadata().setAddAnnotations(false); properties.getMetadata().setAddLabels(false); final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, KubernetesClient::services, new DefaultIsServicePortSecureResolver(properties)); final List<ServiceInstance> instances = discoveryClient.getInstances("endpoint"); assertThat(instances).hasSize(2).filteredOn(ServiceInstance::isSecure).extracting(ServiceInstance::getHost) .containsOnly("ip1", "ip2"); }
@Test public void getInstancesShouldBeAbleToHandleEndpointsFromMultipleNamespaces() { Endpoints endPoints1 = new EndpointsBuilder().withNewMetadata().withName("endpoint").withNamespace("test") .endMetadata().addNewSubset().addNewAddress().withIp("ip1").withNewTargetRef().withUid("60") .endTargetRef().endAddress().addNewPort("http", "http_tcp", 80, "TCP").endSubset().build(); Endpoints endpoints2 = new EndpointsBuilder().withNewMetadata().withName("endpoint").withNamespace("test2") .endMetadata().addNewSubset().addNewAddress().withIp("ip2").withNewTargetRef().withUid("70") .endTargetRef().endAddress().addNewPort("http", "http_tcp", 80, "TCP").endSubset().build(); List<Endpoints> endpointsList = new ArrayList<>(); endpointsList.add(endPoints1); endpointsList.add(endpoints2); EndpointsList endpoints = new EndpointsList(); endpoints.setItems(endpointsList); mockServer.expect().get().withPath("/api/v1/endpoints?fieldSelector=metadata.name%3Dendpoint") .andReturn(200, endpoints).once(); mockServer.expect().get().withPath("/api/v1/namespaces/test/endpoints/endpoint").andReturn(200, endPoints1) .once(); mockServer.expect().get().withPath("/api/v1/namespaces/test2/endpoints/endpoint").andReturn(200, endpoints2) .once(); Service service1 = new ServiceBuilder().withNewMetadata().withName("endpoint").withNamespace("test") .withLabels(new HashMap<String, String>() { { put("l", "v"); } }).endMetadata().build(); Service service2 = new ServiceBuilder().withNewMetadata().withName("endpoint").withNamespace("test2") .withLabels(new HashMap<String, String>() { { put("l", "v"); } }).endMetadata().build(); List<Service> servicesList = new ArrayList<>(); servicesList.add(service1); servicesList.add(service2); ServiceList services = new ServiceList(); services.setItems(servicesList); mockServer.expect().get().withPath("/api/v1/services?fieldSelector=metadata.name%3Dendpoint") .andReturn(200, services).once(); mockServer.expect().get().withPath("/api/v1/namespaces/test/services/endpoint").andReturn(200, service1) .always(); mockServer.expect().get().withPath("/api/v1/namespaces/test2/services/endpoint").andReturn(200, service2) .always(); final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(); properties.setAllNamespaces(true); final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, KubernetesClient::services, new DefaultIsServicePortSecureResolver(properties)); final List<ServiceInstance> instances = discoveryClient.getInstances("endpoint"); assertThat(instances).hasSize(2); assertThat(instances).filteredOn(s -> s.getHost().equals("ip1") && !s.isSecure()).hasSize(1); assertThat(instances).filteredOn(s -> s.getHost().equals("ip2") && !s.isSecure()).hasSize(1); assertThat(instances).filteredOn(s -> s.getServiceId().contains("endpoint") && ((KubernetesServiceInstance) s).getNamespace().equals("test")).hasSize(1); assertThat(instances).filteredOn(s -> s.getServiceId().contains("endpoint") && ((KubernetesServiceInstance) s).getNamespace().equals("test2")).hasSize(1); assertThat(instances).filteredOn(s -> s.getInstanceId().equals("60")).hasSize(1); assertThat(instances).filteredOn(s -> s.getInstanceId().equals("70")).hasSize(1); }