use of org.onosproject.k8snetworking.api.K8sNetworkService 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);
}
}));
}
}
use of org.onosproject.k8snetworking.api.K8sNetworkService in project onos by opennetworkinglab.
the class K8sSwitchingHandler method setForwardingRulesForTunnel.
/**
* Configures the flow rules which are used for L2 packet switching.
* Note that these rules will be inserted in switching table (table 80).
*
* @param port kubernetes port object
* @param install install flag, add the rule if true, remove it otherwise
*/
private void setForwardingRulesForTunnel(K8sPort port, boolean install) {
// switching rules for the instPorts in the same node
TrafficSelector selector = DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).matchIPDst(port.ipAddress().toIpPrefix()).build();
TrafficTreatment treatment = DefaultTrafficTreatment.builder().setEthDst(port.macAddress()).setOutput(port.portNumber()).build();
k8sFlowRuleService.setRule(appId, port.deviceId(), selector, treatment, PRIORITY_SWITCHING_RULE, FORWARDING_TABLE, install);
// switching rules for the node in the remote node
K8sNode localNode = k8sNodeService.node(port.deviceId());
if (localNode == null) {
final String error = String.format("Cannot find kubernetes node for %s", port.deviceId());
throw new IllegalStateException(error);
}
k8sNodeService.completeNodes().stream().filter(remoteNode -> !remoteNode.intgBridge().equals(localNode.intgBridge())).forEach(remoteNode -> {
TrafficTreatment treatmentToTunnel = DefaultTrafficTreatment.builder().setOutput(remoteNode.intgToTunPortNum()).build();
k8sFlowRuleService.setRule(appId, remoteNode.intgBridge(), selector, treatmentToTunnel, PRIORITY_SWITCHING_RULE, FORWARDING_TABLE, install);
PortNumber portNum = tunnelPortNumByNetId(port.networkId(), k8sNetworkService, remoteNode);
TrafficTreatment treatmentToRemote = DefaultTrafficTreatment.builder().extension(buildExtension(deviceService, remoteNode.tunBridge(), localNode.dataIp().getIp4Address()), remoteNode.tunBridge()).setTunnelId(getVni(port)).setOutput(portNum).build();
k8sFlowRuleService.setRule(appId, remoteNode.tunBridge(), selector, treatmentToRemote, PRIORITY_DEFAULT_RULE, TUN_ENTRY_TABLE, install);
});
}
Aggregations