KubernetesDiscoveryClient implements DiscoveryClient { @Override public List<String> getServices() { String spelExpression = this.properties.getFilter(); Predicate<Service> filteredServices; if (spelExpression == null || spelExpression.isEmpty()) { filteredServices = (Service instance) -> true; } else { Expression filterExpr = this.parser.parseExpression(spelExpression); filteredServices = (Service instance) -> { Boolean include = filterExpr.getValue(this.evalCtxt, instance, Boolean.class); if (include == null) { return false; } return include; }; } return getServices(filteredServices); } 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 getServicesShouldReturnAllServicesWhenNoLabelsAreAppliedToTheClient() { mockServer.expect().get().withPath("/api/v1/namespaces/test/services").andReturn(200, new ServiceListBuilder() .addNewItem().withNewMetadata().withName("s1").withLabels(new HashMap<String, String>() { { put("label", "value"); } }).endMetadata().endItem().addNewItem().withNewMetadata().withName("s2") .withLabels(new HashMap<String, String>() { { put("label", "value"); put("label2", "value2"); } }).endMetadata().endItem().addNewItem().withNewMetadata().withName("s3").endMetadata().endItem() .build()).once(); final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(); final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, KubernetesClient::services, new DefaultIsServicePortSecureResolver(properties)); final List<String> services = discoveryClient.getServices(); assertThat(services).containsOnly("s1", "s2", "s3"); }
@Test public void getServicesShouldReturnOnlyMatchingServicesWhenLabelsAreAppliedToTheClient() { mockServer.expect().get().withPath("/api/v1/namespaces/test/services?labelSelector=label%3Dvalue") .andReturn(200, new ServiceListBuilder().addNewItem().withNewMetadata().withName("s1") .withLabels(new HashMap<String, String>() { { put("label", "value"); } }).endMetadata().endItem().addNewItem().withNewMetadata().withName("s2") .withLabels(new HashMap<String, String>() { { put("label", "value"); put("label2", "value2"); } }).endMetadata().endItem().build()) .once(); final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(); final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, client -> client.services().withLabels(new HashMap<String, String>() { { put("label", "value"); } }), new DefaultIsServicePortSecureResolver(properties)); final List<String> services = discoveryClient.getServices(); assertThat(services).containsOnly("s1", "s2"); }