Merge branch '3.0.x'

This commit is contained in:
Ryan Baxter
2023-11-29 14:13:06 -05:00
3 changed files with 8 additions and 13 deletions

View File

@@ -17,13 +17,11 @@
package org.springframework.cloud.kubernetes.discovery;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
@@ -51,35 +49,33 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
@Override
public List<ServiceInstance> getInstances(String serviceId) {
List<ServiceInstance> response = Collections.emptyList();
KubernetesServiceInstance[] responseBody = rest.getForEntity(
properties.getDiscoveryServerUrl() + "/apps/" + serviceId, KubernetesServiceInstance[].class).getBody();
if (responseBody != null && responseBody.length > 0) {
response = Arrays.stream(responseBody).filter(this::matchNamespaces).collect(Collectors.toList());
return Arrays.stream(responseBody).filter(this::matchNamespaces).collect(Collectors.toList());
}
return response;
return List.of();
}
@Override
public List<String> getServices() {
List<String> response = Collections.emptyList();
Service[] services = rest.getForEntity(properties.getDiscoveryServerUrl() + "/apps", Service[].class).getBody();
if (services != null && services.length > 0) {
response = Arrays.stream(services).filter(this::matchNamespaces).map(Service::getName)
return Arrays.stream(services).filter(this::matchNamespaces).map(Service::getName)
.collect(Collectors.toList());
}
return response;
return List.of();
}
private boolean matchNamespaces(KubernetesServiceInstance kubernetesServiceInstance) {
if (CollectionUtils.isEmpty(properties.getNamespaces())) {
if (properties.getNamespaces().isEmpty()) {
return true;
}
return properties.getNamespaces().contains(kubernetesServiceInstance.getNamespace());
}
private boolean matchNamespaces(Service service) {
if (CollectionUtils.isEmpty(service.getServiceInstances())) {
if (service.getServiceInstances().isEmpty()) {
return true;
}
return service.getServiceInstances().stream().anyMatch(this::matchNamespaces);

View File

@@ -16,7 +16,6 @@
package org.springframework.cloud.kubernetes.discovery;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -35,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 = new ArrayList<>();
private List<String> namespaces = List.of();
public String getDiscoveryServerUrl() {
return discoveryServerUrl;

View File

@@ -25,7 +25,7 @@ public class Service {
private String name;
private List<KubernetesServiceInstance> serviceInstances;
private List<KubernetesServiceInstance> serviceInstances = List.of();
public Service() {
}