use of org.onosproject.k8snetworking.api.K8sPodService in project onos by opennetworkinglab.
the class K8sServiceHandler method getSportEpAddressMap.
/**
* 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()) {
// 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;
}
use of org.onosproject.k8snetworking.api.K8sPodService in project onos by opennetworkinglab.
the class K8sPodListCommand method doExecute.
@Override
protected void doExecute() {
K8sPodService service = get(K8sPodService.class);
List<Pod> pods = Lists.newArrayList(service.pods());
pods.sort(Comparator.comparing(p -> p.getMetadata().getName()));
String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, CLI_NAMESPACE_LENGTH, CLI_IP_ADDRESS_LENGTH, CLI_CONTAINERS_LENGTH));
if (outputJson()) {
print("%s", json(pods));
} else {
print(format, "Name", "Namespace", "IP Address", "Containers");
for (Pod pod : pods) {
List<String> containers = Lists.newArrayList();
pod.getSpec().getContainers().forEach(c -> containers.add(c.getName()));
print(format, StringUtils.substring(pod.getMetadata().getName(), 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(pod.getMetadata().getNamespace(), 0, CLI_NAMESPACE_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(pod.getStatus().getPodIP(), 0, CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH), containers.isEmpty() ? "" : containers);
}
}
}
use of org.onosproject.k8snetworking.api.K8sPodService in project onos by opennetworkinglab.
the class K8sServiceHandler method setGroupBuckets.
private void setGroupBuckets(Service service, boolean install) {
Map<ServicePort, Set<String>> spEpasMap = getSportEpAddressMap(service);
Map<ServicePort, List<GroupBucket>> spGrpBkts = Maps.newConcurrentMap();
Map<String, String> nodeIpGatewayIpMap = nodeIpGatewayIpMap(k8sNodeService, k8sNetworkService);
for (K8sNode node : k8sNodeService.completeNodes()) {
spEpasMap.forEach((sp, epas) -> {
List<GroupBucket> bkts = Lists.newArrayList();
for (String ip : epas) {
GroupBucket bkt = buildBuckets(node.intgBridge(), nodeIpGatewayIpMap.getOrDefault(ip, ip), sp);
if (bkt == null) {
continue;
}
if (install) {
bkts.add(bkt);
} else {
bkts.remove(bkt);
}
}
spGrpBkts.put(sp, bkts);
});
String serviceIp = service.getSpec().getClusterIP();
spGrpBkts.forEach((sp, bkts) -> {
String svcStr = servicePortStr(serviceIp, sp.getPort(), sp.getProtocol());
int groupId = svcStr.hashCode();
if (bkts.size() > 0) {
k8sGroupRuleService.setBuckets(appId, node.intgBridge(), groupId, bkts);
}
});
spEpasMap.forEach((sp, epas) -> epas.forEach(epa -> {
String podIp = nodeIpGatewayIpMap.getOrDefault(epa, epa);
int targetPort;
if (sp.getTargetPort().getIntVal() == null) {
Pod pod = podByIp(k8sPodService, podIp);
targetPort = portNumberByName(pod, sp.getTargetPort().getStrVal());
} else {
targetPort = sp.getTargetPort().getIntVal();
}
if (targetPort != 0) {
setUnshiftDomainRules(node.intgBridge(), POD_TABLE, PRIORITY_NAT_RULE, serviceIp, sp.getPort(), sp.getProtocol(), podIp, targetPort, install);
}
}));
}
}
Aggregations