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

The following examples show how to use io.fabric8.kubernetes.api.model.ServicePort#getPort() . 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: IngressEnricher.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private static Integer getServicePort(ServiceBuilder serviceBuilder) {
    ServiceSpec spec = serviceBuilder.buildSpec();
    if (spec != null) {
        List<ServicePort> ports = spec.getPorts();
        if (ports != null && !ports.isEmpty()) {
            for (ServicePort port : ports) {
                if (port.getName().equals("http") || port.getProtocol().equals("http")) {
                    return port.getPort();
                }
            }
            ServicePort servicePort = ports.get(0);
            if (servicePort != null) {
                return servicePort.getPort();
            }
        }
    }
    return 0;
}
 
Example 2
Source File: ExposeEnricher.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private boolean hasWebPort(Service service) {
    ServiceSpec spec = service.getSpec();
    if (spec != null) {
        List<ServicePort> ports = spec.getPorts();
        if (ports != null) {
            for (ServicePort port : ports) {
                Integer portNumber = port.getPort();
                if (portNumber != null && webPorts.contains(portNumber)) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 3
Source File: DefaultServiceEnricher.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private String formatPortsAsList(List<ServicePort> ports)  {
    List<String> p = new ArrayList<>();
    for (ServicePort port : ports) {
        String targetPort = getPortValue(port.getTargetPort());
        String servicePort= port.getPort() != null ? Integer.toString(port.getPort()) : targetPort;
        p.add(targetPort.equals(servicePort) ? targetPort : servicePort + ":" + targetPort);
    }
    return String.join(",", p);
}
 
Example 4
Source File: Kubernetes.java    From enmasse with Apache License 2.0 5 votes vote down vote up
private static int getPort(Service service, String portName) {
    List<ServicePort> ports = service.getSpec().getPorts();
    for (ServicePort port : ports) {
        if (port.getName().equals(portName)) {
            return port.getPort();
        }
    }
    throw new IllegalArgumentException(
            "Unable to find port " + portName + " for service " + service.getMetadata().getName());
}
 
Example 5
Source File: KubeDiscovery.java    From vxms with Apache License 2.0 5 votes vote down vote up
private static String buildServiceHostString(
    String clusterIP, ServicePort port, String protocol) {
  String hostString;
  if (StringUtil.isNullOrEmpty(protocol)) {
    hostString = clusterIP + SEPERATOR + port.getPort();
  } else {
    hostString = protocol + "://" + clusterIP + SEPERATOR + port.getPort();
  }
  return hostString;
}
 
Example 6
Source File: URLFromClusterIPImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public String getURL(Service service, String portName, String namespace, KubernetesClient client) {
  ServicePort port = URLFromServiceUtil.getServicePortByName(service, portName);
  if (port != null && service.getSpec().getType().equals("ClusterIP")) {
    return port.getProtocol().toLowerCase() + "://" + service.getSpec().getClusterIP() + ":" + port.getPort();
  }
  return null;
}
 
Example 7
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 8
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 9
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 10
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);
}