Skip to content

Commit

Permalink
Merge branch '3.0.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjbaxter committed Nov 30, 2023
2 parents 1f88d53 + 0e7215e commit 0862fe2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -53,7 +57,7 @@ public String description() {
@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());
}
Expand All @@ -62,7 +66,7 @@ public List<ServiceInstance> getInstances(String serviceId) {

@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());
Expand All @@ -71,14 +75,12 @@ public List<String> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -52,11 +52,11 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

List<String> getNamespaces() {
Set<String> getNamespaces() {
return namespaces;
}

void setNamespaces(List<String> namespaces) {
void setNamespaces(Set<String> namespaces) {
this.namespaces = namespaces;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -137,7 +138,7 @@ void getServices() {

@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);
Expand All @@ -146,16 +147,9 @@ void getServicesFilteredByNamespaces(List<String> namespaces, List<String> expec
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);
Expand All @@ -165,10 +159,17 @@ void getInstancesFilteredByNamespaces(List<String> namespaces, String serviceId,
.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")));
}

}

0 comments on commit 0862fe2

Please sign in to comment.