use of io.fabric8.openshift.api.model.RouteList in project fabric8-maven-plugin by fabric8io.
the class ApplyMojo method createRoutes.
protected void createRoutes(Controller controller, Collection<HasMetadata> collection) {
String routeDomainPostfix = this.routeDomain;
Log log = getLog();
String namespace = clusterAccess.getNamespace();
// lets get the routes first to see if we should bother
try {
OpenShiftClient openshiftClient = controller.getOpenShiftClientOrNull();
if (openshiftClient == null) {
return;
}
RouteList routes = openshiftClient.routes().inNamespace(namespace).list();
if (routes != null) {
routes.getItems();
}
} catch (Exception e) {
log.warn("Cannot load OpenShift Routes; maybe not connected to an OpenShift platform? " + e, e);
return;
}
List<Route> routes = new ArrayList<>();
for (Object object : collection) {
if (object instanceof Service) {
Service service = (Service) object;
Route route = createRouteForService(routeDomainPostfix, namespace, service);
if (route != null) {
routes.add(route);
}
}
}
collection.addAll(routes);
}
use of io.fabric8.openshift.api.model.RouteList in project fabric8 by fabric8io.
the class KubernetesHelper method getServiceURLInCurrentNamespace.
/**
* Returns the URL to access the service; using the environment variables, routes
* or service clusterIP address
*
* @throws IllegalArgumentException if the URL cannot be found for the serviceName and namespace
*/
public static String getServiceURLInCurrentNamespace(KubernetesClient client, String serviceName, String serviceProtocol, String servicePortName, boolean serviceExternal) {
Service srv = null;
String serviceHost = KubernetesServices.serviceToHostOrBlank(serviceName);
String servicePort = KubernetesServices.serviceToPortOrBlank(serviceName, servicePortName);
String serviceProto = serviceProtocol != null ? serviceProtocol : KubernetesServices.serviceToProtocol(serviceName, servicePort);
// 1. Inside Kubernetes: Services as ENV vars
if (!serviceExternal && Strings.isNotBlank(serviceHost) && Strings.isNotBlank(servicePort) && Strings.isNotBlank(serviceProtocol)) {
return serviceProtocol + "://" + serviceHost + ":" + servicePort;
// 2. Anywhere: When namespace is passed System / Env var. Mostly needed for integration tests.
} else {
srv = client.services().withName(serviceName).get();
}
if (srv == null) {
throw new IllegalArgumentException("No kubernetes service could be found for name: " + serviceName);
}
if (Strings.isNullOrBlank(servicePortName) && isOpenShift(client)) {
OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
RouteList routeList = openShiftClient.routes().list();
for (Route route : routeList.getItems()) {
if (route.getSpec().getTo().getName().equals(serviceName)) {
return (serviceProto + "://" + route.getSpec().getHost()).toLowerCase();
}
}
}
ServicePort port = findServicePortByName(srv, servicePortName);
if (port == null) {
throw new RuntimeException("Couldn't find port: " + servicePortName + " for service:" + serviceName);
}
String clusterIP = srv.getSpec().getClusterIP();
if ("None".equals(clusterIP)) {
throw new IllegalStateException("Service: " + serviceName + " in current namespace is head-less. Search for endpoints instead.");
}
return (serviceProto + "://" + clusterIP + ":" + port.getPort()).toLowerCase();
}
use of io.fabric8.openshift.api.model.RouteList in project kie-wb-common by kiegroup.
the class OpenShiftNameService method setRoutes.
public static void setRoutes(RouteList routeList, String routerHost) {
InetAddress routerAddr;
try {
routerAddr = routerHost == null ? null : InetAddress.getByName(routerHost);
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Invalid IP for router host", e);
}
if (routeList != null) {
synchronized (ROUTING) {
for (Route route : routeList.getItems()) {
String host = route.getSpec().getHost();
if (routerAddr != null) {
System.out.println(String.format("Adding route (router -> host): %s -> %s", routerHost, host));
ROUTING.put(host, routerAddr);
} else {
ROUTING.remove(host);
}
}
}
}
}
use of io.fabric8.openshift.api.model.RouteList in project kie-wb-common by kiegroup.
the class OpenShiftNameServiceClientListener method trigger.
@Override
public void trigger(OpenShiftClient client, OpenShiftRuntimeConfig runtimeConfig) {
String prjName = runtimeConfig.getProjectName();
RouteList routeList = client.getDelegate().routes().inNamespace(prjName).list();
String routerHost = System.getProperty(OpenShiftNameServiceClientListener.class.getName() + ".routerHost", "10.34.75.115");
OpenShiftNameService.setRoutes(routeList, routerHost);
}
use of io.fabric8.openshift.api.model.RouteList in project fabric8 by fabric8io.
the class ServiceConverter method getServiceURL.
public String getServiceURL(KubernetesClient client, Service srv, String serviceProtocol, String servicePortName) {
String serviceName = KubernetesHelper.getName(srv);
String serviceProto = serviceProtocol != null ? serviceProtocol : KubernetesServices.serviceToProtocol(serviceName, servicePortName);
if (Strings.isNullOrBlank(servicePortName) && KubernetesHelper.isOpenShift(client)) {
OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
RouteList routeList = openShiftClient.routes().list();
for (Route route : routeList.getItems()) {
if (route.getSpec().getTo().getName().equals(serviceName)) {
return (serviceProto + "://" + route.getSpec().getHost()).toLowerCase();
}
}
}
ServicePort port = KubernetesHelper.findServicePortByName(srv, servicePortName);
if (port == null) {
throw new RuntimeException("Couldn't find port: " + servicePortName + " for service:" + serviceName);
}
String clusterIP = srv.getSpec().getClusterIP();
if ("None".equals(clusterIP)) {
throw new IllegalStateException("Service " + serviceName + " is head-less. Search for endpoints instead.");
}
return (serviceProto + "://" + clusterIP + ":" + port.getPort()).toLowerCase();
}
Aggregations