Polish discovery client
This commit is contained in:
committed by
Ioannis Canellos
parent
42999e439c
commit
90d3850177
@@ -16,23 +16,19 @@
|
||||
*/
|
||||
package org.springframework.cloud.kubernetes.discovery;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.fabric8.kubernetes.api.model.EndpointAddress;
|
||||
import io.fabric8.kubernetes.api.model.EndpointSubset;
|
||||
import io.fabric8.kubernetes.api.model.Endpoints;
|
||||
import io.fabric8.kubernetes.api.model.Service;
|
||||
import io.fabric8.kubernetes.client.KubernetesClient;
|
||||
import io.fabric8.kubernetes.client.utils.Utils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.expression.Expression;
|
||||
@@ -43,7 +39,6 @@ import org.springframework.util.Assert;
|
||||
public class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
private static final Log log = LogFactory.getLog(KubernetesDiscoveryClient.class);
|
||||
private static final String HOSTNAME = "HOSTNAME";
|
||||
|
||||
private KubernetesClient client;
|
||||
private final KubernetesDiscoveryProperties properties;
|
||||
@@ -73,63 +68,16 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
return "Kubernetes Discovery Client";
|
||||
}
|
||||
|
||||
public ServiceInstance getLocalServiceInstance() {
|
||||
String serviceName = properties.getServiceName();
|
||||
String podName = System.getenv(HOSTNAME);
|
||||
ServiceInstance defaultInstance = new DefaultServiceInstance(serviceName,
|
||||
"localhost",
|
||||
8080,
|
||||
false);
|
||||
|
||||
Endpoints endpoints = client.endpoints().withName(serviceName).get();
|
||||
Optional<Service> service = Optional.ofNullable(client.services().withName(serviceName).get());
|
||||
final Map<String, String> labels;
|
||||
if (service.isPresent()) {
|
||||
labels = service.get().getMetadata().getLabels();
|
||||
} else {
|
||||
labels = null;
|
||||
}
|
||||
if (Utils.isNullOrEmpty(podName) || endpoints == null) {
|
||||
return defaultInstance;
|
||||
}
|
||||
try {
|
||||
List<EndpointSubset> subsets = endpoints.getSubsets();
|
||||
|
||||
if (subsets != null) {
|
||||
for (EndpointSubset s : subsets) {
|
||||
List<EndpointAddress> addresses = s.getAddresses();
|
||||
for (EndpointAddress a : addresses) {
|
||||
return new KubernetesServiceInstance(serviceName,
|
||||
a,
|
||||
s.getPorts().stream().findFirst().orElseThrow(IllegalStateException::new),
|
||||
labels,
|
||||
false);
|
||||
}
|
||||
}
|
||||
}
|
||||
return defaultInstance;
|
||||
|
||||
} catch (Throwable t) {
|
||||
return defaultInstance;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ServiceInstance> getInstances(String serviceId) {
|
||||
Assert.notNull(serviceId,
|
||||
"[Assertion failed] - the object argument must be null");
|
||||
Optional<Service> service = Optional.ofNullable(client.services().withName(serviceId).get());
|
||||
final Map<String, String> labels;
|
||||
if (service.isPresent()) {
|
||||
labels = service.get().getMetadata().getLabels();
|
||||
} else {
|
||||
labels = null;
|
||||
}
|
||||
final Map<String, String> labels = getLabels(serviceId);
|
||||
|
||||
Optional<Endpoints> endpoints = Optional.ofNullable(client.endpoints().withName(serviceId).get());
|
||||
List<EndpointSubset> subsets = endpoints.get().getSubsets();
|
||||
Endpoints endpoints = client.endpoints().withName(serviceId).get();
|
||||
List<EndpointSubset> subsets = null != endpoints ? endpoints.getSubsets() : new ArrayList<>();
|
||||
List<ServiceInstance> instances = new ArrayList<>();
|
||||
if (subsets != null) {
|
||||
if (!subsets.isEmpty()) {
|
||||
for (EndpointSubset s : subsets) {
|
||||
List<EndpointAddress> addresses = s.getAddresses();
|
||||
for (EndpointAddress a : addresses) {
|
||||
@@ -145,6 +93,14 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
return instances;
|
||||
}
|
||||
|
||||
private Map<String, String> getLabels(String serviceName) {
|
||||
final Service service = client.services().withName(serviceName).get();
|
||||
if (service != null) {
|
||||
return service.getMetadata().getLabels();
|
||||
}
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getServices() {
|
||||
String spelExpression = properties.getFilter();
|
||||
|
||||
Reference in New Issue
Block a user