Search in sources :

Example 1 with RouteList

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);
}
Also used : Log(org.apache.maven.plugin.logging.Log) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ArrayList(java.util.ArrayList) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) Service(io.fabric8.kubernetes.api.model.Service) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) RouteList(io.fabric8.openshift.api.model.RouteList) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Route(io.fabric8.openshift.api.model.Route)

Example 2 with RouteList

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();
}
Also used : DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) RouteList(io.fabric8.openshift.api.model.RouteList) Route(io.fabric8.openshift.api.model.Route)

Example 3 with RouteList

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);
                }
            }
        }
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress) Route(io.fabric8.openshift.api.model.Route)

Example 4 with RouteList

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);
}
Also used : RouteList(io.fabric8.openshift.api.model.RouteList)

Example 5 with RouteList

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();
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) RouteList(io.fabric8.openshift.api.model.RouteList) Route(io.fabric8.openshift.api.model.Route)

Aggregations

Route (io.fabric8.openshift.api.model.Route)4 RouteList (io.fabric8.openshift.api.model.RouteList)4 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 KubernetesHelper.createIntOrString (io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString)1 DoneableService (io.fabric8.kubernetes.api.model.DoneableService)1 Service (io.fabric8.kubernetes.api.model.Service)1 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 Log (org.apache.maven.plugin.logging.Log)1