Java Code Examples for io.fabric8.kubernetes.api.model.NodeAddress#getAddress()

The following examples show how to use io.fabric8.kubernetes.api.model.NodeAddress#getAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Kubernetes.java    From enmasse with Apache License 2.0 6 votes vote down vote up
/**
 * Return the external IP for the first node found in the cluster.
 */
public String getHost() {
    if (isCRC()) {
        return "api.crc.testing";
    }
    List<NodeAddress> addresses = client.nodes().list().getItems().stream()
            .peek(n -> CustomLogger.getLogger().info("Found node: {}", n))
            .flatMap(n -> n.getStatus().getAddresses().stream()
                    .peek(a -> CustomLogger.getLogger().info("Found address: {}", a))
                    .filter(a -> a.getType().equals("InternalIP") || a.getType().equals("ExternalIP")))
            .collect(Collectors.toList());
    if (addresses.isEmpty()) {
        return null;
    }

    // Return public ip if exists
    for (NodeAddress address : addresses) {
        if (address.getType().equals("ExternalIP")) {
            return address.getAddress();
        }
    }

    // Fall back to first internal ip
    return addresses.get(0).getAddress();
}
 
Example 2
Source File: KubernetesDeploymentEndpointWaiter.java    From ephemerals with MIT License 4 votes vote down vote up
@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");
            return null; // Either timeout or unreachable or failed DNS lookup.
        }
    }

}