Search in sources :

Example 26 with LoadBalancer

use of io.fabric8.gateway.loadbalancer.LoadBalancer in project ephemerals by LivePersonInc.

the class KubernetesDeploymentEndpointWaiter method getDeploymentEndpoint.

@Override
protected DeploymentEndpoints.Endpoint getDeploymentEndpoint() {
    String ip = null;
    int port = 0;
    String serviceType = kubernetesClient.services().withName(deployment.getId()).get().getSpec().getType();
    logger.debug("Kubernetes service type: " + serviceType);
    if (serviceType.equals("LoadBalancer")) {
        ip = kubernetesClient.services().withName(deployment.getId()).get().getStatus().getLoadBalancer().getIngress().get(0).getIp();
    } else {
        // nodeport
        List<ServicePort> servicePortList = kubernetesClient.services().withName(deployment.getId()).get().getSpec().getPorts();
        for (ServicePort servicePort : servicePortList) {
            if (servicePort.getPort().equals(deploymentPort.getPort())) {
                port = servicePort.getNodePort();
            }
        }
        /**
         * Fetch Node IP address:
         *  - External IP takes precedence over internal IP
         *  - If external IP isn't found, return internal IP
         *  - If both IPs not found, return null
         */
        // Since node port is shared across all nodes, use first node
        List<NodeAddress> nodeAddressList = kubernetesClient.nodes().list().getItems().get(0).getStatus().getAddresses();
        String nodeInternalIp = null, nodeExternalIp = null;
        for (NodeAddress nodeAddress : nodeAddressList) {
            if (nodeAddress.getType().equals("ExternalIP")) {
                nodeExternalIp = nodeAddress.getAddress();
            } else if (nodeAddress.getType().equals("InternalIP")) {
                nodeInternalIp = nodeAddress.getAddress();
            }
        }
        // External IP takes precedence over internal IP
        if (nodeExternalIp != null) {
            ip = nodeExternalIp;
            logger.debug("Using node ExternalIP: " + nodeExternalIp);
        } else if (nodeInternalIp != null) {
            ip = nodeInternalIp;
            logger.debug("Using node InternalIP: " + nodeInternalIp);
        }
    }
    if (ip == null) {
        logger.info("Endpoint not found");
        return null;
    } else {
        logger.info("Endpoint found...");
        logger.info(String.format("Checking connection to endpoint IP %s and port %d", ip, port));
        try (Socket socket = new Socket()) {
            socket.connect(new InetSocketAddress(ip, port), 2 * 1000);
            logger.info("Endpoint is reachable");
            endpoint = new DeploymentEndpoints.Endpoint(deploymentPort.getName(), ip, port);
            return endpoint;
        } catch (IOException e) {
            logger.warn("Endpoint is unreachable");
            // Either timeout or unreachable or failed DNS lookup.
            return null;
        }
    }
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) DeploymentEndpoints(com.liveperson.ephemerals.deploy.DeploymentEndpoints) InetSocketAddress(java.net.InetSocketAddress) NodeAddress(io.fabric8.kubernetes.api.model.NodeAddress) IOException(java.io.IOException) Socket(java.net.Socket)

Aggregations

LoadBalancer (io.fabric8.gateway.loadbalancer.LoadBalancer)14 RoundRobinLoadBalancer (io.fabric8.gateway.loadbalancer.RoundRobinLoadBalancer)8 ArrayList (java.util.ArrayList)7 InetSocketAddress (java.net.InetSocketAddress)5 AmqpProtocol (io.fabric8.gateway.handlers.detecting.protocol.amqp.AmqpProtocol)4 HttpProtocol (io.fabric8.gateway.handlers.detecting.protocol.http.HttpProtocol)4 MqttProtocol (io.fabric8.gateway.handlers.detecting.protocol.mqtt.MqttProtocol)4 OpenwireProtocol (io.fabric8.gateway.handlers.detecting.protocol.openwire.OpenwireProtocol)4 SslConfig (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslConfig)4 SslProtocol (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslProtocol)4 StompProtocol (io.fabric8.gateway.handlers.detecting.protocol.stomp.StompProtocol)4 MappedServices (io.fabric8.gateway.handlers.http.MappedServices)4 Service (io.fabric8.kubernetes.api.model.Service)4 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)4 Test (org.junit.Test)4 HttpGateway (io.fabric8.gateway.handlers.http.HttpGateway)3 HttpGatewayHandler (io.fabric8.gateway.handlers.http.HttpGatewayHandler)3 HttpGatewayServer (io.fabric8.gateway.handlers.http.HttpGatewayServer)3 HttpMappingRule (io.fabric8.gateway.handlers.http.HttpMappingRule)3 RandomLoadBalancer (io.fabric8.gateway.loadbalancer.RandomLoadBalancer)3