use of io.fabric8.annotations.Endpoint in project fabric8 by jboss-fuse.
the class SessionServicesAreReady method isEndpointAvailable.
/**
* Checks if there is an endpoint for the service available.
* @param s The target service.
* @return Returns true if a connection to at least one of the endpoints is possible.
*/
private boolean isEndpointAvailable(Service s) {
String serviceStatus = null;
boolean result = false;
String sid = getName(s);
String namespace = session.getNamespace();
Endpoints endpoints = kubernetesClient.endpoints().inNamespace(namespace).withName(sid).get();
ServiceSpec spec = s.getSpec();
if (endpoints != null && spec != null) {
List<EndpointSubset> subsets = endpoints.getSubsets();
if (subsets != null) {
for (EndpointSubset subset : subsets) {
List<EndpointAddress> addresses = subset.getAddresses();
if (addresses != null) {
for (EndpointAddress address : addresses) {
String ip = address.getIp();
String addr = ip;
/*
TODO v1beta2...
String addr = endpoit.substring(0, endpoit.indexOf(":"));
Integer port = Integer.parseInt(endpoit.substring(endpoit.indexOf(":") + 1));
*/
List<ServicePort> ports = spec.getPorts();
for (ServicePort port : ports) {
Integer portNumber = port.getPort();
if (portNumber != null && portNumber > 0) {
if (configuration.isWaitForServiceConnectionEnabled()) {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(ip, portNumber), (int) configuration.getWaitForServiceConnectionTimeout());
serviceStatus = "Service: " + sid + " is ready. Provider:" + addr + ".";
return true;
} catch (Exception e) {
serviceStatus = "Service: " + sid + " is not ready! in namespace " + namespace + ". Error: " + e.getMessage();
} finally {
session.getLogger().warn(serviceStatus);
}
} else {
serviceStatus = "Service: " + sid + " is ready. Not testing connecting to it!. Provider:" + addr + ".";
session.getLogger().warn(serviceStatus);
return true;
}
}
}
}
}
}
}
}
session.getLogger().warn("Service: " + sid + " has no valid endpoints");
return result;
}
Aggregations