Java Code Examples for io.fabric8.kubernetes.api.model.ServicePort#getNodePort()

The following examples show how to use io.fabric8.kubernetes.api.model.ServicePort#getNodePort() . 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: ServiceOperator.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if the Service already has assigned node ports.
 *
 * @param namespace The namespace.
 * @param name The route name.
 * @return Whether the Service already has assigned node ports.
 */
public boolean isNodePortReady(String namespace, String name) {
    ServiceResource<Service, DoneableService> resourceOp = operation().inNamespace(namespace).withName(name);
    Service resource = resourceOp.get();

    if (resource != null && resource.getSpec() != null && resource.getSpec().getPorts() != null) {
        boolean ready = true;

        for (ServicePort port : resource.getSpec().getPorts())  {
            if (port.getNodePort() == null) {
                ready = false;
            }
        }
        return ready;
    }

    return false;
}
 
Example 2
Source File: ServicesController.java    From rabbitmq-operator with Apache License 2.0 5 votes vote down vote up
private List<ServicePort> getUpdatedServicePorts(final Service current, final Service desired) {
    final List<ServicePort> finalPorts = Lists.newArrayList();
    for (final ServicePort desiredPort : desired.getSpec().getPorts())    {
        for (final ServicePort currentPort : current.getSpec().getPorts())    {
            if (desiredPort.getNodePort() == null && desiredPort.getName().equals(currentPort.getName()) && currentPort.getNodePort() != null) {
                finalPorts.add(new ServicePortBuilder(desiredPort).withNodePort(currentPort.getNodePort()).build());
            }
        }
    }

    return finalPorts;
}
 
Example 3
Source File: ServiceOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Finds out if corresponding port from desired service also exists in current service.
 * If it exists, it will copy the node port.
 * That will make sure the node port doesn't change with every reconciliation.
 *
 * @param current   Current Service
 * @param desired   Desired Service
 */
protected void patchNodePorts(Service current, Service desired) {
    for (ServicePort desiredPort : desired.getSpec().getPorts())    {
        String portName = desiredPort.getName();

        for (ServicePort currentPort : current.getSpec().getPorts())    {
            if (desiredPort.getNodePort() == null && portName.equals(currentPort.getName()) && currentPort.getNodePort() != null) {
                desiredPort.setNodePort(currentPort.getNodePort());
            }
        }
    }
}
 
Example 4
Source File: Fabric8FlinkKubeClient.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Get rest port from the external Service.
 */
private int getRestPortFromExternalService(Service externalService) {
	final List<ServicePort> servicePortCandidates = externalService.getSpec().getPorts()
		.stream()
		.filter(x -> x.getName().equals(Constants.REST_PORT_NAME))
		.collect(Collectors.toList());

	if (servicePortCandidates.isEmpty()) {
		throw new RuntimeException("Failed to find port \"" + Constants.REST_PORT_NAME + "\" in Service \"" +
			ExternalServiceDecorator.getExternalServiceName(this.clusterId) + "\"");
	}

	final ServicePort externalServicePort = servicePortCandidates.get(0);

	final KubernetesConfigOptions.ServiceExposedType externalServiceType =
		KubernetesConfigOptions.ServiceExposedType.valueOf(externalService.getSpec().getType());

	switch (externalServiceType) {
		case ClusterIP:
		case LoadBalancer:
			return externalServicePort.getPort();
		case NodePort:
			return externalServicePort.getNodePort();
		default:
			throw new RuntimeException("Unrecognized Service type: " + externalServiceType);
	}
}
 
Example 5
Source File: K8sNodePortHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
private void setExtToIngrRules(K8sNode k8sNode, ServicePort servicePort,
                                boolean install) {
    String protocol = servicePort.getProtocol();
    int nodePort = servicePort.getNodePort();
    DeviceId deviceId = k8sNode.extBridge();

    TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder()
            .matchEthType(Ethernet.TYPE_IPV4)
            .matchIPDst(IpPrefix.valueOf(k8sNode.extBridgeIp(), HOST_CIDR));

    TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder()
            .setOutput(PortNumber.LOCAL);

    if (TCP.equals(protocol)) {
        sBuilder.matchIPProtocol(IPv4.PROTOCOL_TCP)
                .matchTcpSrc(TpPort.tpPort(nodePort));
    } else if (UDP.equals(protocol)) {
        sBuilder.matchIPProtocol(IPv4.PROTOCOL_UDP)
                .matchUdpSrc(TpPort.tpPort(nodePort));
    }

    k8sFlowRuleService.setRule(
            appId,
            deviceId,
            sBuilder.build(),
            tBuilder.build(),
            PRIORITY_NODE_PORT_RULE,
            EXT_ENTRY_TABLE,
            install);
}
 
Example 6
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 7
Source File: K8sNodePortHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
private void setNodeToServiceRules(K8sNode k8sNode,
                                   String clusterIp,
                                   ServicePort servicePort,
                                   boolean install) {
    String protocol = servicePort.getProtocol();
    int nodePort = servicePort.getNodePort();
    int svcPort = servicePort.getPort();
    DeviceId deviceId = k8sNode.extBridge();

    TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder()
            .matchEthType(Ethernet.TYPE_IPV4)
            .matchIPDst(IpPrefix.valueOf(k8sNode.extBridgeIp(), HOST_CIDR));

    TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder()
            .setIpDst(IpAddress.valueOf(clusterIp));

    if (TCP.equals(protocol)) {
        sBuilder.matchIPProtocol(IPv4.PROTOCOL_TCP)
                .matchTcpDst(TpPort.tpPort(nodePort));
        tBuilder.setTcpDst(TpPort.tpPort(svcPort));
    } else if (UDP.equals(protocol)) {
        sBuilder.matchIPProtocol(IPv4.PROTOCOL_UDP)
                .matchUdpDst(TpPort.tpPort(nodePort));
        tBuilder.setUdpDst(TpPort.tpPort(svcPort));
    }

    String podCidr = k8sNetworkService.network(k8sNode.hostname()).cidr();
    String prefix = NODE_IP_PREFIX + "." + podCidr.split("\\.")[2];

    ExtensionTreatment loadTreatment = buildLoadExtension(
            deviceService.getDevice(deviceId), B_CLASS, SRC, prefix);
    tBuilder.extension(loadTreatment, deviceId)
            .setOutput(k8sNode.extToIntgPatchPortNum());

    k8sFlowRuleService.setRule(
            appId,
            k8sNode.extBridge(),
            sBuilder.build(),
            tBuilder.build(),
            PRIORITY_NODE_PORT_RULE,
            EXT_ENTRY_TABLE,
            install);
}
 
Example 8
Source File: K8sNodePortHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
private void setServiceToNodeLocalRules(K8sNode k8sNode,
                                        String clusterIp,
                                        ServicePort servicePort,
                                        boolean install) {
    String protocol = servicePort.getProtocol();
    int nodePort = servicePort.getNodePort();
    int svcPort = servicePort.getPort();
    DeviceId deviceId = k8sNode.extBridge();

    String extBridgeIp = k8sNode.extBridgeIp().toString();
    String extBridgePrefix = getBclassIpPrefixFromCidr(extBridgeIp);

    String podCidr = k8sNetworkService.network(k8sNode.hostname()).cidr();
    String nodePrefix = NODE_IP_PREFIX + "." + podCidr.split("\\.")[2];

    if (extBridgePrefix == null) {
        return;
    }

    String shiftedIp = unshiftIpDomain(extBridgeIp, extBridgePrefix, nodePrefix);

    TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder()
            .matchEthType(Ethernet.TYPE_IPV4)
            .matchInPort(k8sNode.extToIntgPatchPortNum())
            .matchIPSrc(IpPrefix.valueOf(IpAddress.valueOf(clusterIp), HOST_CIDR))
            .matchIPDst(IpPrefix.valueOf(IpAddress.valueOf(shiftedIp), HOST_CIDR));

    TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder()
            .setIpSrc(k8sNode.extBridgeIp())
            .setEthSrc(k8sNode.extBridgeMac());

    if (TCP.equals(protocol)) {
        sBuilder.matchIPProtocol(IPv4.PROTOCOL_TCP)
                .matchTcpSrc(TpPort.tpPort(svcPort));
        tBuilder.setTcpSrc(TpPort.tpPort(nodePort));
    } else if (UDP.equals(protocol)) {
        sBuilder.matchIPProtocol(IPv4.PROTOCOL_UDP)
                .matchUdpSrc(TpPort.tpPort(svcPort));
        tBuilder.setUdpSrc(TpPort.tpPort(nodePort));
    }

    String gatewayIp = k8sNode.extGatewayIp().toString();
    String gatewayPrefix = getBclassIpPrefixFromCidr(gatewayIp);

    if (gatewayPrefix == null) {
        return;
    }

    ExtensionTreatment loadTreatment = buildLoadExtension(
            deviceService.getDevice(deviceId), B_CLASS, DST, gatewayPrefix);
    tBuilder.extension(loadTreatment, deviceId)
            .setOutput(PortNumber.LOCAL);

    k8sFlowRuleService.setRule(
            appId,
            deviceId,
            sBuilder.build(),
            tBuilder.build(),
            PRIORITY_NODE_PORT_RULE,
            EXT_ENTRY_TABLE,
            install);
}
 
Example 9
Source File: K8sNodePortHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
private void setServiceToNodeRemoteRules(K8sNode k8sNode,
                                         String clusterIp,
                                         ServicePort servicePort,
                                         boolean install) {
    String protocol = servicePort.getProtocol();
    int nodePort = servicePort.getNodePort();
    int svcPort = servicePort.getPort();
    DeviceId deviceId = k8sNode.extBridge();

    TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder()
            .matchEthType(Ethernet.TYPE_IPV4)
            .matchInPort(k8sNode.extToIntgPatchPortNum())
            .matchIPSrc(IpPrefix.valueOf(IpAddress.valueOf(clusterIp), HOST_CIDR));

    TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder()
            .setIpSrc(k8sNode.extBridgeIp())
            .setEthSrc(k8sNode.extBridgeMac());

    if (TCP.equals(protocol)) {
        sBuilder.matchIPProtocol(IPv4.PROTOCOL_TCP)
                .matchTcpSrc(TpPort.tpPort(svcPort));
        tBuilder.setTcpSrc(TpPort.tpPort(nodePort));
    } else if (UDP.equals(protocol)) {
        sBuilder.matchIPProtocol(IPv4.PROTOCOL_UDP)
                .matchUdpSrc(TpPort.tpPort(svcPort));
        tBuilder.setUdpSrc(TpPort.tpPort(nodePort));
    }

    String gatewayIp = k8sNode.extGatewayIp().toString();
    String prefix = getBclassIpPrefixFromCidr(gatewayIp);

    if (prefix == null) {
        return;
    }

    ExtensionTreatment loadTreatment = buildLoadExtension(
            deviceService.getDevice(deviceId), B_CLASS, DST, prefix);
    tBuilder.extension(loadTreatment, deviceId)
            .setOutput(k8sNode.extBridgePortNum());

    k8sFlowRuleService.setRule(
            appId,
            deviceId,
            sBuilder.build(),
            tBuilder.build(),
            PRIORITY_NODE_PORT_REMOTE_RULE,
            EXT_ENTRY_TABLE,
            install);
}