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

The following examples show how to use io.fabric8.kubernetes.api.model.EndpointPort. 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: KubernetesDiscoveryClient.java    From spring-cloud-kubernetes with Apache License 2.0 6 votes vote down vote up
private EndpointPort findEndpointPort(EndpointSubset s) {
	List<EndpointPort> ports = s.getPorts();
	EndpointPort endpointPort;
	if (ports.size() == 1) {
		endpointPort = ports.get(0);
	}
	else {
		Predicate<EndpointPort> portPredicate;
		if (!StringUtils.isEmpty(properties.getPrimaryPortName())) {
			portPredicate = port -> properties.getPrimaryPortName()
					.equalsIgnoreCase(port.getName());
		}
		else {
			portPredicate = port -> true;
		}
		endpointPort = ports.stream().filter(portPredicate).findAny()
				.orElseThrow(IllegalStateException::new);
	}
	return endpointPort;
}
 
Example #2
Source File: KubernetesServiceInstanceTests.java    From spring-cloud-kubernetes with Apache License 2.0 6 votes vote down vote up
private KubernetesServiceInstance assertServiceInstance(boolean secure) {
	EndpointAddress address = new EndpointAddress();
	address.setIp("1.2.3.4");
	EndpointPort port = new EndpointPort();
	port.setPort(8080);
	KubernetesServiceInstance instance = new KubernetesServiceInstance("123",
			"myservice", address, port, Collections.emptyMap(), secure);

	assertThat(instance.getInstanceId()).isEqualTo("123");
	assertThat(instance.getServiceId()).isEqualTo("myservice");
	assertThat(instance.getHost()).isEqualTo("1.2.3.4");
	assertThat(instance.getPort()).isEqualTo(8080);
	assertThat(instance.isSecure()).isEqualTo(secure);
	assertThat(instance.getScheme()).isEqualTo(secure ? "https" : "http");
	return instance;
}
 
Example #3
Source File: KubernetesServiceInstance.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
public KubernetesServiceInstance(String instanceId, String serviceId,
		EndpointAddress endpointAddress, EndpointPort endpointPort,
		Map<String, String> metadata, Boolean secure) {
	this.instanceId = instanceId;
	this.serviceId = serviceId;
	this.endpointAddress = endpointAddress;
	this.endpointPort = endpointPort;
	this.metadata = metadata;
	this.secure = secure;
}
 
Example #4
Source File: KubernetesDiscoveryClientFilterMetadataTest.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
private List<EndpointPort> getEndpointPorts(Map<Integer, String> ports) {
	return ports.entrySet().stream().map(e -> {
		EndpointPortBuilder endpointPortBuilder = new EndpointPortBuilder();
		endpointPortBuilder.withPort(e.getKey());
		if (!Strings.isNullOrEmpty(e.getValue())) {
			endpointPortBuilder.withName(e.getValue());
		}
		return endpointPortBuilder.build();
	}).collect(toList());
}
 
Example #5
Source File: KubernetesDiscoveryClient.java    From spring-cloud-kubernetes with Apache License 2.0 4 votes vote down vote up
private List<ServiceInstance> getNamespaceServiceInstances(EndpointSubsetNS es,
		String serviceId) {
	String namespace = es.getNamespace();
	List<EndpointSubset> subsets = es.getEndpointSubset();
	List<ServiceInstance> instances = new ArrayList<>();
	if (!subsets.isEmpty()) {
		final Service service = this.client.services().inNamespace(namespace)
				.withName(serviceId).get();
		final Map<String, String> serviceMetadata = this.getServiceMetadata(service);
		KubernetesDiscoveryProperties.Metadata metadataProps = this.properties
				.getMetadata();

		for (EndpointSubset s : subsets) {
			// Extend the service metadata map with per-endpoint port information (if
			// requested)
			Map<String, String> endpointMetadata = new HashMap<>(serviceMetadata);
			if (metadataProps.isAddPorts()) {
				Map<String, String> ports = s.getPorts().stream()
						.filter(port -> !StringUtils.isEmpty(port.getName()))
						.collect(toMap(EndpointPort::getName,
								port -> Integer.toString(port.getPort())));
				Map<String, String> portMetadata = getMapWithPrefixedKeys(ports,
						metadataProps.getPortsPrefix());
				if (log.isDebugEnabled()) {
					log.debug("Adding port metadata: " + portMetadata);
				}
				endpointMetadata.putAll(portMetadata);
			}

			List<EndpointAddress> addresses = s.getAddresses();
			for (EndpointAddress endpointAddress : addresses) {
				String instanceId = null;
				if (endpointAddress.getTargetRef() != null) {
					instanceId = endpointAddress.getTargetRef().getUid();
				}

				EndpointPort endpointPort = findEndpointPort(s);
				instances.add(new KubernetesServiceInstance(instanceId, serviceId,
						endpointAddress, endpointPort, endpointMetadata,
						this.isServicePortSecureResolver
								.resolve(new DefaultIsServicePortSecureResolver.Input(
										endpointPort.getPort(),
										service.getMetadata().getName(),
										service.getMetadata().getLabels(),
										service.getMetadata().getAnnotations()))));
			}
		}
	}

	return instances;
}
 
Example #6
Source File: KubernetesServiceInstance.java    From spring-cloud-kubernetes with Apache License 2.0 4 votes vote down vote up
public KubernetesServiceInstance(String serviceId, EndpointAddress endpointAddress, EndpointPort endpointPort, Boolean secure) {
    this.serviceId = serviceId;
    this.endpointAddress = endpointAddress;
    this.endpointPort = endpointPort;
    this.secure = secure;
}
 
Example #7
Source File: K8sServiceHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Obtains the service port to endpoint address paired map.
 *
 * @param service   kubernetes service
 * @return a map where key is kubernetes service port, and value is the
 * endpoint addresses that are associated with the service port
 */
private Map<ServicePort, Set<String>> getSportEpAddressMap(Service service) {

    Map<ServicePort, Set<String>> map = Maps.newConcurrentMap();

    String serviceName = service.getMetadata().getName();
    List<Endpoints> endpointses = k8sEndpointsService.endpointses()
            .stream()
            .filter(ep -> serviceName.equals(ep.getMetadata().getName()))
            .collect(Collectors.toList());

    service.getSpec().getPorts().stream()
            .filter(Objects::nonNull)
            .filter(sp -> sp.getTargetPort() != null)
            .filter(sp -> sp.getTargetPort().getIntVal() != null ||
                    sp.getTargetPort().getStrVal() != null)
            .forEach(sp -> {
                Integer targetPortInt = sp.getTargetPort().getIntVal() != null ?
                        sp.getTargetPort().getIntVal() : 0;
                String targetPortName = sp.getTargetPort().getStrVal() != null ?
                        sp.getTargetPort().getStrVal() : "";
                String targetProtocol = sp.getProtocol();

                for (Endpoints endpoints : endpointses) {
                    for (EndpointSubset endpointSubset : endpoints.getSubsets()) {

                        // in case service port name is specified but not port number
                        // we will lookup the container port number and use it
                        // as the target port number
                        if (!targetPortName.equals("") && targetPortInt == 0) {
                            for (EndpointAddress addr : endpointSubset.getAddresses()) {
                                Pod pod = podByIp(k8sPodService, addr.getIp());
                                targetPortInt = portNumberByName(pod, targetPortName);
                            }
                        }

                        if (targetPortInt == 0) {
                            continue;
                        }

                        for (EndpointPort endpointPort : endpointSubset.getPorts()) {
                            if (targetProtocol.equals(endpointPort.getProtocol()) &&
                                    (targetPortInt.equals(endpointPort.getPort()) ||
                                            targetPortName.equals(endpointPort.getName()))) {
                                Set<String> addresses = endpointSubset.getAddresses()
                                        .stream().map(EndpointAddress::getIp)
                                        .collect(Collectors.toSet());
                                map.put(sp, addresses);
                            }
                        }
                    }
                }
            });

    return map;
}
 
Example #8
Source File: K8sServiceHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
private void setStatefulGroupFlowRules(DeviceId deviceId, long ctState,
                                       long ctMask, Service service,
                                       boolean install) {
    List<GroupBucket> buckets = Lists.newArrayList();

    String serviceName = service.getMetadata().getName();
    String serviceIp = service.getSpec().getClusterIP();

    // TODO: multi-ports case should be addressed
    Integer servicePort = service.getSpec().getPorts().get(0).getPort();
    String serviceProtocol = service.getSpec().getPorts().get(0).getProtocol();

    String svcStr = servicePortStr(serviceIp, servicePort, serviceProtocol);
    int groupId = svcStr.hashCode();

    List<Endpoints> endpointses = k8sEndpointsService.endpointses()
            .stream()
            .filter(ep -> serviceName.equals(ep.getMetadata().getName()))
            .collect(Collectors.toList());

    Map<String, String> nodeIpGatewayIpMap =
            nodeIpGatewayIpMap(k8sNodeService, k8sNetworkService);

    for (Endpoints endpoints : endpointses) {
        for (EndpointSubset endpointSubset : endpoints.getSubsets()) {
            List<EndpointPort> ports = endpointSubset.getPorts()
                    .stream()
                    .filter(p -> p.getProtocol().equals(TCP))
                    .collect(Collectors.toList());

            for (EndpointAddress address : endpointSubset.getAddresses()) {
                String podIp = nodeIpGatewayIpMap.containsKey(address.getIp()) ?
                        nodeIpGatewayIpMap.get(address.getIp()) : address.getIp();

                NiciraConnTrackTreatmentBuilder connTreatmentBuilder =
                        niciraConnTrackTreatmentBuilder(driverService, deviceId)
                                .commit(true)
                                .natAction(true)
                                .natIp(IpAddress.valueOf(podIp))
                                .natFlag(CT_NAT_DST_FLAG);

                ports.forEach(p -> {
                    ExtensionTreatment ctNatTreatment = connTreatmentBuilder
                            .natPortMin(TpPort.tpPort(p.getPort()))
                            .natPortMax(TpPort.tpPort(p.getPort()))
                            .build();
                    ExtensionTreatment resubmitTreatment = buildResubmitExtension(
                            deviceService.getDevice(deviceId), ACL_TABLE);
                    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
                            .extension(ctNatTreatment, deviceId)
                            .extension(resubmitTreatment, deviceId)
                            .build();
                    buckets.add(buildGroupBucket(treatment, SELECT, (short) -1));
                });
            }
        }
    }

    if (!buckets.isEmpty()) {
        k8sGroupRuleService.setRule(appId, deviceId, groupId, SELECT, buckets, install);

        setTrackNew(deviceId, ctState, ctMask, IpAddress.valueOf(serviceIp),
                TpPort.tpPort(servicePort), NAT_TABLE, groupId,
                PRIORITY_CT_RULE, install);
    }
}
 
Example #9
Source File: KubernetesServiceInstance.java    From spring-cloud-kubernetes with Apache License 2.0 2 votes vote down vote up
/**
 * @param serviceId the id of the service.
 * @param endpointAddress the address where the service instance can be found.
 * @param endpointPort the port on which the service is running.
 * @param metadata a map containing metadata.
 * @param secure indicates whether or not the connection needs to be secure.
 * @deprecated - use other constructor
 */
@Deprecated
public KubernetesServiceInstance(String serviceId, EndpointAddress endpointAddress,
		EndpointPort endpointPort, Map<String, String> metadata, Boolean secure) {
	this(null, serviceId, endpointAddress, endpointPort, metadata, secure);
}