io.fabric8.kubernetes.api.model.NodeAddress Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.NodeAddress. 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: InitWriter.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * Write the external address of this node
 *
 * @return if the operation was executed successfully
 */
public boolean writeExternalAddress() {

    List<NodeAddress> addresses = client.nodes().withName(config.getNodeName()).get().getStatus().getAddresses();
    log.info("NodeLabels = {}", addresses);
    String externalAddress = NodeUtils.findAddress(addresses, config.getAddressType());

    if (externalAddress == null) {
        log.error("External address not found");
        return false;
    } else  {
        log.info("External address found {}", externalAddress);
    }

    return write(FILE_EXTERNAL_ADDRESS, externalAddress);
}
 
Example #2
Source File: InitWriterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * Mock a Kubernetes client for getting cluster node information
 *
 * @param nodeName cluster node name
 * @param labels metadata labels to be returned for the provided cluster node name
 * @return mocked Kubernetes client
 */
private KubernetesClient mockKubernetesClient(String nodeName, Map<String, String> labels, List<NodeAddress> addresses) {

    KubernetesClient client = mock(KubernetesClient.class);
    NonNamespaceOperation mockNodes = mock(NonNamespaceOperation.class);
    Resource mockResource = mock(Resource.class);
    Node mockNode = mock(Node.class);
    ObjectMeta mockNodeMetadata = mock(ObjectMeta.class);
    NodeStatus mockNodeStatus = mock(NodeStatus.class);

    when(client.nodes()).thenReturn(mockNodes);
    when(mockNodes.withName(nodeName)).thenReturn(mockResource);
    when(mockResource.get()).thenReturn(mockNode);
    when(mockNode.getMetadata()).thenReturn(mockNodeMetadata);
    when(mockNodeMetadata.getLabels()).thenReturn(labels);
    when(mockNode.getStatus()).thenReturn(mockNodeStatus);
    when(mockNodeStatus.getAddresses()).thenReturn(addresses);

    return client;
}
 
Example #3
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 #4
Source File: NodeUtils.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to find the right address of the node. The different addresses has different prioprities:
 *      1. ExternalDNS
 *      2. ExternalIP
 *      3. Hostname
 *      4. InternalDNS
 *      5. InternalIP
 *
 * @param addresses List of addresses which are assigned to our node
 * @param preferredAddressType Name of the address which is preferred by the user. Null if not specified.
 *
 * @return  Address of the node
 */
public static String findAddress(List<NodeAddress> addresses, String preferredAddressType)   {
    if (addresses == null)  {
        return null;
    }

    Map<String, String> addressMap = addresses.stream().collect(Collectors.toMap(NodeAddress::getType, NodeAddress::getAddress));

    // If user set preferred address type, we should check it first
    if (preferredAddressType != null && addressMap.containsKey(preferredAddressType)) {
        return addressMap.get(preferredAddressType);
    }

    if (addressMap.containsKey("ExternalDNS"))  {
        return addressMap.get("ExternalDNS");
    } else if (addressMap.containsKey("ExternalIP"))  {
        return addressMap.get("ExternalIP");
    } else if (addressMap.containsKey("InternalDNS"))  {
        return addressMap.get("InternalDNS");
    } else if (addressMap.containsKey("InternalIP"))  {
        return addressMap.get("InternalIP");
    } else if (addressMap.containsKey("Hostname")) {
        return addressMap.get("Hostname");
    }

    return null;
}
 
Example #5
Source File: NodeUtilsTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testFindAddressNotFound()   {
    List<NodeAddress> addresses = new ArrayList<>(3);
    addresses.add(new NodeAddressBuilder().withType("SomeAddress").withAddress("my.external.address").build());
    addresses.add(new NodeAddressBuilder().withType("SomeOtherAddress").withAddress("my.internal.address").build());
    addresses.add(new NodeAddressBuilder().withType("YetAnotherAddress").withAddress("192.168.2.94").build());
    String address = NodeUtils.findAddress(addresses, null);

    assertThat(address, is(nullValue()));
}
 
Example #6
Source File: NodeUtilsTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testFindAddressesNull()   {
    List<NodeAddress> addresses = null;

    String address = NodeUtils.findAddress(addresses, null);

    assertThat(address, is(nullValue()));
}
 
Example #7
Source File: InitWriter.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to find the right address of the node. The different addresses has different prioprities:
 *      1. ExternalDNS
 *      2. ExternalIP
 *      3. Hostname
 *      4. InternalDNS
 *      5. InternalIP
 *
 * @param addresses List of addresses which are assigned to our node
 * @return  Address of the node
 */
protected String findAddress(List<NodeAddress> addresses)   {
    if (addresses == null)  {
        return null;
    }

    Map<String, String> addressMap = addresses.stream().collect(Collectors.toMap(NodeAddress::getType, NodeAddress::getAddress));

    // If user set preferred address type, we should check it first
    if (config.getAddressType() != null && addressMap.containsKey(config.getAddressType())) {
        return addressMap.get(config.getAddressType());
    }

    if (addressMap.containsKey("ExternalDNS"))  {
        return addressMap.get("ExternalDNS");
    } else if (addressMap.containsKey("ExternalIP"))  {
        return addressMap.get("ExternalIP");
    } else if (addressMap.containsKey("InternalDNS"))  {
        return addressMap.get("InternalDNS");
    } else if (addressMap.containsKey("InternalIP"))  {
        return addressMap.get("InternalIP");
    } else if (addressMap.containsKey("Hostname")) {
        return addressMap.get("Hostname");
    }

    return null;
}
 
Example #8
Source File: InitWriterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testFindAddressNullWithInvalidAddressTypes()   {
    List<NodeAddress> addresses = new ArrayList<>(3);
    addresses.add(new NodeAddressBuilder().withType("SomeAddress").withAddress("my.external.address").build());
    addresses.add(new NodeAddressBuilder().withType("SomeOtherAddress").withAddress("my.internal.address").build());
    addresses.add(new NodeAddressBuilder().withType("YetAnotherAddress").withAddress("192.168.2.94").build());

    InitWriterConfig config = InitWriterConfig.fromMap(envVars);
    KubernetesClient client = mockKubernetesClient(config.getNodeName(), labels, addresses);
    InitWriter writer = new InitWriter(client, config);
    String address = writer.findAddress(addresses);

    assertThat(address, is(nullValue()));
}
 
Example #9
Source File: InitWriterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testFindAddressNullWhenAddressesNull()   {
    List<NodeAddress> addresses = null;

    InitWriterConfig config = InitWriterConfig.fromMap(envVars);
    KubernetesClient client = mockKubernetesClient(config.getNodeName(), labels, addresses);
    InitWriter writer = new InitWriter(client, config);
    String address = writer.findAddress(addresses);

    assertThat(address, is(nullValue()));
}
 
Example #10
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.
        }
    }

}
 
Example #11
Source File: KafkaStatusTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
private List<Node> getClusterNodes()    {
    Node node0 = new NodeBuilder()
            .withNewMetadata()
                .withName("node-0")
            .endMetadata()
            .withNewStatus()
                .withAddresses(new NodeAddress("50.35.18.119", "ExternalIP"),
                        new NodeAddress("node-0.my-kube", "InternalDNS"),
                        new NodeAddress("10.0.0.1", "InternalIP"),
                        new NodeAddress("nocde-0", "Hostname"))
            .endStatus()
            .build();

    Node node1 = new NodeBuilder()
            .withNewMetadata()
                .withName("node-1")
            .endMetadata()
            .withNewStatus()
                .withAddresses(new NodeAddress("55.36.78.115", "ExternalIP"),
                        new NodeAddress("node-1.my-kube", "InternalDNS"),
                        new NodeAddress("10.0.0.25", "InternalIP"),
                        new NodeAddress("node-1", "Hostname"))
            .endStatus()
            .build();

    Node node2 = new NodeBuilder()
            .withNewMetadata()
                .withName("node-2")
            .endMetadata()
            .withNewStatus()
                .withAddresses(new NodeAddress("35.15.152.9", "ExternalIP"),
                        new NodeAddress("node-2.my-kube", "InternalDNS"),
                        new NodeAddress("10.0.0.16", "InternalIP"),
                        new NodeAddress("node-2", "Hostname"))
            .endStatus()
            .build();

    Node node3 = new NodeBuilder()
            .withNewMetadata()
                .withName("node-3")
            .endMetadata()
            .withNewStatus()
                .withAddresses(new NodeAddress("5.124.16.8", "ExternalIP"),
                        new NodeAddress("node-3.my-kube", "InternalDNS"),
                        new NodeAddress("10.0.0.13", "InternalIP"),
                        new NodeAddress("node-3", "Hostname"))
            .endStatus()
            .build();

    List<Node> nodes = new ArrayList<>();
    nodes.add(node0);
    nodes.add(node1);
    nodes.add(node2);
    nodes.add(node3);

    return nodes;
}
 
Example #12
Source File: DefaultK8sApiConfigHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
private K8sNode buildK8sNode(Node node) {
    String hostname = node.getMetadata().getName();
    IpAddress managementIp = null;
    IpAddress dataIp = null;

    for (NodeAddress nodeAddress:node.getStatus().getAddresses()) {
        // we need to consider assigning managementIp and dataIp differently
        // FIXME: ExternalIp is not considered currently
        if (nodeAddress.getType().equals(INTERNAL_IP)) {
            managementIp = IpAddress.valueOf(nodeAddress.getAddress());
            dataIp = IpAddress.valueOf(nodeAddress.getAddress());
        }
    }

    String roleStr = node.getMetadata().getLabels().keySet().stream()
            .filter(l -> l.contains(K8S_ROLE))
            .findFirst().orElse(null);

    K8sNode.Type nodeType = MINION;

    if (roleStr != null) {
        String role = roleStr.split("/")[1];
        if (MASTER.name().equalsIgnoreCase(role)) {
            nodeType = MASTER;
        } else {
            nodeType = MINION;
        }
    }

    Map<String, String> annots = node.getMetadata().getAnnotations();

    String extIntf = annots.get(EXT_INTF_NAME);
    String extGatewayIpStr = annots.get(EXT_GATEWAY_IP);
    String extBridgeIpStr = annots.get(EXT_BRIDGE_IP);

    return DefaultK8sNode.builder()
            .hostname(hostname)
            .managementIp(managementIp)
            .dataIp(dataIp)
            .extIntf(extIntf)
            .type(nodeType)
            .state(PRE_ON_BOARD)
            .extBridgeIp(IpAddress.valueOf(extBridgeIpStr))
            .extGatewayIp(IpAddress.valueOf(extGatewayIpStr))
            .podCidr(node.getSpec().getPodCIDR())
            .build();
}