diff --git a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClient.java b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClient.java index b22ea869a..207919519 100644 --- a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClient.java +++ b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClient.java @@ -18,6 +18,7 @@ import java.util.Arrays; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import org.springframework.cloud.client.ServiceInstance; @@ -32,17 +33,20 @@ public class KubernetesDiscoveryClient implements DiscoveryClient { private final RestTemplate rest; - private final KubernetesDiscoveryClientProperties properties; - private final boolean emptyNamespaces; + private final Set namespaces; + + private final String discoveryServerUrl; + public KubernetesDiscoveryClient(RestTemplate rest, KubernetesDiscoveryClientProperties properties) { if (!StringUtils.hasText(properties.getDiscoveryServerUrl())) { throw new DiscoveryServerUrlInvalidException(); } this.rest = rest; - this.properties = properties; this.emptyNamespaces = properties.getNamespaces().isEmpty(); + this.namespaces = properties.getNamespaces(); + this.discoveryServerUrl = properties.getDiscoveryServerUrl(); } @Override @@ -53,7 +57,7 @@ public String description() { @Override public List getInstances(String serviceId) { KubernetesServiceInstance[] responseBody = rest.getForEntity( - properties.getDiscoveryServerUrl() + "/apps/" + serviceId, KubernetesServiceInstance[].class).getBody(); + discoveryServerUrl + "/apps/" + serviceId, KubernetesServiceInstance[].class).getBody(); if (responseBody != null && responseBody.length > 0) { return Arrays.stream(responseBody).filter(this::matchNamespaces).collect(Collectors.toList()); } @@ -62,7 +66,7 @@ public List getInstances(String serviceId) { @Override public List getServices() { - Service[] services = rest.getForEntity(properties.getDiscoveryServerUrl() + "/apps", Service[].class).getBody(); + Service[] services = rest.getForEntity(discoveryServerUrl + "/apps", Service[].class).getBody(); if (services != null && services.length > 0) { return Arrays.stream(services).filter(this::matchNamespaces).map(Service::getName) .collect(Collectors.toList()); @@ -71,14 +75,12 @@ public List getServices() { } private boolean matchNamespaces(KubernetesServiceInstance kubernetesServiceInstance) { - return emptyNamespaces || properties.getNamespaces().contains(kubernetesServiceInstance.getNamespace()); + return emptyNamespaces || namespaces.contains(kubernetesServiceInstance.getNamespace()); } private boolean matchNamespaces(Service service) { - if (service.getServiceInstances().isEmpty()) { - return true; - } - return service.getServiceInstances().stream().anyMatch(this::matchNamespaces); + return service.getServiceInstances().isEmpty() || + service.getServiceInstances().stream().anyMatch(this::matchNamespaces); } } diff --git a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientProperties.java b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientProperties.java index 94261df90..91c1d9b34 100644 --- a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientProperties.java +++ b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientProperties.java @@ -16,7 +16,7 @@ package org.springframework.cloud.kubernetes.discovery; -import java.util.List; +import java.util.Set; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -34,7 +34,7 @@ public class KubernetesDiscoveryClientProperties { * If set then only the services and endpoints matching these namespaces will be * fetched from the Kubernetes API server. */ - private List namespaces = List.of(); + private Set namespaces = Set.of(); public String getDiscoveryServerUrl() { return discoveryServerUrl; @@ -52,11 +52,11 @@ public void setEnabled(boolean enabled) { this.enabled = enabled; } - List getNamespaces() { + Set getNamespaces() { return namespaces; } - void setNamespaces(List namespaces) { + void setNamespaces(Set namespaces) { this.namespaces = namespaces; } diff --git a/spring-cloud-kubernetes-discovery/src/test/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientTests.java b/spring-cloud-kubernetes-discovery/src/test/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientTests.java index 6a81b4835..c64de7ec6 100644 --- a/spring-cloud-kubernetes-discovery/src/test/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientTests.java +++ b/spring-cloud-kubernetes-discovery/src/test/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientTests.java @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.stream.Stream; import com.github.tomakehurst.wiremock.WireMockServer; @@ -137,7 +138,7 @@ void getServices() { @ParameterizedTest @MethodSource("servicesFilteredByNamespacesSource") - void getServicesFilteredByNamespaces(List namespaces, List expectedServices) { + void getServicesFilteredByNamespaces(Set namespaces, List expectedServices) { RestTemplate rest = new RestTemplateBuilder().build(); KubernetesDiscoveryClientProperties properties = new KubernetesDiscoveryClientProperties(); properties.setNamespaces(namespaces); @@ -146,16 +147,9 @@ void getServicesFilteredByNamespaces(List namespaces, List expec assertThat(discoveryClient.getServices()).containsExactlyInAnyOrderElementsOf(expectedServices); } - static Stream servicesFilteredByNamespacesSource() { - return Stream.of(Arguments.of(List.of(), List.of("test-svc-1", "test-svc-3")), - Arguments.of(List.of("namespace1", "namespace2"), List.of("test-svc-1", "test-svc-3")), - Arguments.of(List.of("namespace1"), List.of("test-svc-1")), - Arguments.of(List.of("namespace2", "does-not-exist"), List.of("test-svc-3"))); - } - @ParameterizedTest @MethodSource("instancesFilteredByNamespacesSource") - void getInstancesFilteredByNamespaces(List namespaces, String serviceId, List expectedInstances) { + void getInstancesFilteredByNamespaces(Set namespaces, String serviceId, List expectedInstances) { RestTemplate rest = new RestTemplateBuilder().build(); KubernetesDiscoveryClientProperties properties = new KubernetesDiscoveryClientProperties(); properties.setNamespaces(namespaces); @@ -165,10 +159,17 @@ void getInstancesFilteredByNamespaces(List namespaces, String serviceId, .containsExactlyInAnyOrderElementsOf(expectedInstances); } - static Stream instancesFilteredByNamespacesSource() { - return Stream.of(Arguments.of(List.of(), "test-svc-3", List.of("uid2")), - Arguments.of(List.of("namespace1"), "test-svc-3", List.of()), - Arguments.of(List.of("namespace2"), "test-svc-3", List.of("uid2"))); + private static Stream servicesFilteredByNamespacesSource() { + return Stream.of(Arguments.of(Set.of(), List.of("test-svc-1", "test-svc-3")), + Arguments.of(Set.of("namespace1", "namespace2"), List.of("test-svc-1", "test-svc-3")), + Arguments.of(Set.of("namespace1"), List.of("test-svc-1")), + Arguments.of(Set.of("namespace2", "does-not-exist"), List.of("test-svc-3"))); + } + + private static Stream instancesFilteredByNamespacesSource() { + return Stream.of(Arguments.of(Set.of(), "test-svc-3", List.of("uid2")), + Arguments.of(Set.of("namespace1"), "test-svc-3", List.of()), + Arguments.of(Set.of("namespace2"), "test-svc-3", List.of("uid2"))); } }