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 29, 2023
2 parents b812492 + 9951a65 commit 473aa52
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -51,35 +49,33 @@ public String description() {

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Service {

private String name;

private List<KubernetesServiceInstance> serviceInstances;
private List<KubernetesServiceInstance> serviceInstances = List.of();

public Service() {
}
Expand Down

0 comments on commit 473aa52

Please sign in to comment.