KubernetesDiscoveryClient now returns the default instance if endpoint is not found.

This commit is contained in:
Ioannis Canellos
2016-04-06 16:19:35 +03:00
parent 47ea55e7a2
commit b97ee26c83

View File

@@ -57,17 +57,18 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
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();
if (Utils.isNullOrEmpty(podName) || endpoints == null) {
//TODO: Fallback to something more meaningful.
return new DefaultServiceInstance(serviceName, "localhost", 8080, false);
return defaultInstance;
}
return endpoints.getSubsets()
.stream()
.filter(s -> s.getAddresses().iterator().next().getIp().equals(podName))
.map(s -> new KubernetesServiceInstance(serviceName, s.getPorts().iterator().next().getName(), s, false))
.findFirst().get();
.map(s -> (ServiceInstance) new KubernetesServiceInstance(serviceName, s.getPorts().iterator().next().getName(), s, false))
.findFirst().orElse(defaultInstance);
}
@Override