kubernetes discovery minor changes part 3 (#1523)
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.cloud.kubernetes.discovery;
|
||||
|
||||
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<String> 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 class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
@Override
|
||||
public List<ServiceInstance> 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 class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
@Override
|
||||
public List<String> 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 class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<String> namespaces = List.of();
|
||||
private Set<String> namespaces = Set.of();
|
||||
|
||||
public String getDiscoveryServerUrl() {
|
||||
return discoveryServerUrl;
|
||||
@@ -52,11 +52,11 @@ public class KubernetesDiscoveryClientProperties {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
List<String> getNamespaces() {
|
||||
Set<String> getNamespaces() {
|
||||
return namespaces;
|
||||
}
|
||||
|
||||
void setNamespaces(List<String> namespaces) {
|
||||
void setNamespaces(Set<String> namespaces) {
|
||||
this.namespaces = namespaces;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.net.URI;
|
||||
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 @@ class KubernetesDiscoveryClientTests {
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("servicesFilteredByNamespacesSource")
|
||||
void getServicesFilteredByNamespaces(List<String> namespaces, List<String> expectedServices) {
|
||||
void getServicesFilteredByNamespaces(Set<String> namespaces, List<String> expectedServices) {
|
||||
RestTemplate rest = new RestTemplateBuilder().build();
|
||||
KubernetesDiscoveryClientProperties properties = new KubernetesDiscoveryClientProperties();
|
||||
properties.setNamespaces(namespaces);
|
||||
@@ -146,16 +147,9 @@ class KubernetesDiscoveryClientTests {
|
||||
assertThat(discoveryClient.getServices()).containsExactlyInAnyOrderElementsOf(expectedServices);
|
||||
}
|
||||
|
||||
static Stream<Arguments> 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<String> namespaces, String serviceId, List<String> expectedInstances) {
|
||||
void getInstancesFilteredByNamespaces(Set<String> namespaces, String serviceId, List<String> expectedInstances) {
|
||||
RestTemplate rest = new RestTemplateBuilder().build();
|
||||
KubernetesDiscoveryClientProperties properties = new KubernetesDiscoveryClientProperties();
|
||||
properties.setNamespaces(namespaces);
|
||||
@@ -165,10 +159,17 @@ class KubernetesDiscoveryClientTests {
|
||||
.containsExactlyInAnyOrderElementsOf(expectedInstances);
|
||||
}
|
||||
|
||||
static Stream<Arguments> 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<Arguments> 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<Arguments> 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")));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user