use of org.onosproject.k8snode.api.K8sNode in project onos by opennetworkinglab.
the class K8sNodeWebResource method readNodeConfiguration.
private Set<K8sNode> readNodeConfiguration(InputStream input) {
Set<K8sNode> nodeSet = Sets.newHashSet();
try {
JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
ArrayNode nodes = (ArrayNode) jsonTree.path(NODES);
nodes.forEach(node -> {
try {
ObjectNode objectNode = node.deepCopy();
K8sNode k8sNode = codec(K8sNode.class).decode(objectNode, this);
nodeSet.add(k8sNode);
} catch (Exception e) {
log.error("Exception occurred due to {}", e);
throw new IllegalArgumentException();
}
});
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
return nodeSet;
}
use of org.onosproject.k8snode.api.K8sNode in project onos by opennetworkinglab.
the class K8sSyncRulesCommand method syncRulesBaseForNode.
private void syncRulesBaseForNode(K8sNodeAdminService adminService, K8sNode k8sNode) {
K8sNode updated = k8sNode.updateState(INIT);
adminService.updateNode(updated);
boolean result = true;
long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
while (adminService.node(k8sNode.hostname()).state() != COMPLETE) {
long waitMs = timeoutExpiredMs - System.currentTimeMillis();
try {
sleep(SLEEP_MS);
} catch (InterruptedException e) {
error("Exception caused during node synchronization...");
}
if (adminService.node(k8sNode.hostname()).state() == COMPLETE) {
break;
} else {
adminService.updateNode(updated);
print("Failed to synchronize flow rules, retrying...");
}
if (waitMs <= 0) {
result = false;
break;
}
}
if (result) {
print(SUCCESS_MSG, k8sNode.hostname());
} else {
error(FAIL_MSG, k8sNode.hostname());
}
}
use of org.onosproject.k8snode.api.K8sNode in project onos by opennetworkinglab.
the class K8sRoutingArpHandler method processArpPacket.
private void processArpPacket(PacketContext context, Ethernet ethernet) {
DeviceId deviceId = context.inPacket().receivedFrom().deviceId();
if (!allK8sDevices(k8sNodeService, k8sHostService).contains(deviceId)) {
return;
}
ARP arp = (ARP) ethernet.getPayload();
if (arp.getOpCode() == ARP.OP_REPLY) {
IpAddress spa = Ip4Address.valueOf(arp.getSenderProtocolAddress());
MacAddress sha = MacAddress.valueOf(arp.getSenderHardwareAddress());
log.info("ARP reply from ip: {}, mac: {}", spa, sha);
Set<IpAddress> gatewayIps = k8sNodeService.completeNodes().stream().map(K8sNode::extGatewayIp).collect(Collectors.toSet());
if (!gatewayIps.contains(spa)) {
return;
}
log.info("ARP reply from external gateway ip: {}, mac: {}", spa, sha);
k8sNodeService.completeNodes().stream().filter(n -> n.extGatewayMac() == null).forEach(n -> {
K8sNode updated = n.updateExtGatewayMac(sha);
k8sNodeService.updateNode(updated);
});
}
}
use of org.onosproject.k8snode.api.K8sNode in project onos by opennetworkinglab.
the class K8sSwitchingArpHandler method processArpRequest.
private void processArpRequest(PacketContext context, Ethernet ethPacket) {
ARP arpPacket = (ARP) ethPacket.getPayload();
K8sPort srcK8sPort = k8sNetworkService.ports().stream().filter(p -> p.macAddress().equals(ethPacket.getSourceMAC())).findAny().orElse(null);
PortNumber srcPortNum = context.inPacket().receivedFrom().port();
DeviceId srcDeviceId = context.inPacket().receivedFrom().deviceId();
boolean isEntryPort = false;
for (K8sNode node : k8sNodeService.completeNodes()) {
if (srcDeviceId.equals(node.intgBridge()) && srcPortNum.equals(node.intgEntryPortNum())) {
isEntryPort = true;
}
}
// integration bridge entry port, we simply ignore the ARP request...
if (srcK8sPort == null && !isEntryPort) {
log.warn("Failed to find source port(MAC:{})", ethPacket.getSourceMAC());
return;
}
IpAddress targetIp = Ip4Address.valueOf(arpPacket.getTargetProtocolAddress());
// look up the MAC address from regular k8s ports
MacAddress replyMac = k8sNetworkService.ports().stream().filter(p -> p.ipAddress().equals(targetIp)).map(K8sPort::macAddress).findAny().orElse(null);
// look up the MAC address from special integration entry port (e.g., LOCAL, k8s-int-os)
for (K8sNetwork network : k8sNetworkService.networks()) {
if (network.gatewayIp().equals(targetIp)) {
K8sNode node = k8sNodeService.node(network.name());
replyMac = node.intgEntryPortMac();
}
}
if (replyMac == null) {
String cidr = k8sNetworkService.networks().stream().map(K8sNetwork::cidr).findAny().orElse(null);
if (cidr != null) {
String unshiftedIp = unshiftIpDomain(targetIp.toString(), SHIFTED_IP_PREFIX, cidr);
replyMac = k8sNetworkService.ports().stream().filter(p -> p.ipAddress().equals(IpAddress.valueOf(unshiftedIp))).map(K8sPort::macAddress).findAny().orElse(null);
}
}
if (replyMac == null) {
Set<String> serviceIps = k8sServiceService.services().stream().map(s -> s.getSpec().getClusterIP()).collect(Collectors.toSet());
if (serviceIps.contains(targetIp.toString())) {
replyMac = MacAddress.valueOf(SERVICE_FAKE_MAC_STR);
}
}
if (replyMac == null) {
if (targetIp.toString().startsWith(NODE_IP_PREFIX)) {
String targetIpPrefix = targetIp.toString().split("\\.")[1];
String nodePrefix = NODE_IP_PREFIX + "." + targetIpPrefix;
String origNodeCidr = k8sNodeService.completeNodes().stream().map(n -> n.nodeIp().toString()).findAny().orElse(null);
if (origNodeCidr != null) {
String origNodeIp = unshiftIpDomain(targetIp.toString(), nodePrefix, origNodeCidr);
IpPrefix k8sNodeIpCidr = IpPrefix.valueOf(IpAddress.valueOf(origNodeCidr), 24);
SubnetUtils k8sNodeSubnet = new SubnetUtils(k8sNodeIpCidr.toString());
String k8sNodeGateway = getGatewayIp(k8sNodeIpCidr.toString()).toString();
String seekIp = "";
if (!k8sNodeSubnet.getInfo().isInRange(origNodeIp)) {
replyMac = extHostMacStore.asJavaMap().get(IpAddress.valueOf(k8sNodeGateway));
seekIp = k8sNodeGateway;
} else {
replyMac = extHostMacStore.asJavaMap().get(IpAddress.valueOf(origNodeIp));
seekIp = origNodeIp;
}
// we need to manually learn their MAC addresses
if (replyMac == null) {
ConnectPoint cp = context.inPacket().receivedFrom();
K8sNode k8sNode = k8sNodeService.node(cp.deviceId());
if (k8sNode != null) {
// we use fake IP and MAC address as a source to
// query destination MAC address
setArpRequest(MacAddress.valueOf(NODE_FAKE_MAC_STR).toBytes(), IpAddress.valueOf(NODE_FAKE_IP_STR).toOctets(), IpAddress.valueOf(seekIp).toOctets(), k8sNode);
context.block();
return;
}
}
}
}
}
if (replyMac == null) {
replyMac = MacAddress.valueOf(gatewayMac);
}
Ethernet ethReply = ARP.buildArpReply(targetIp.getIp4Address(), replyMac, ethPacket);
TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(context.inPacket().receivedFrom().port()).build();
packetService.emit(new DefaultOutboundPacket(context.inPacket().receivedFrom().deviceId(), treatment, ByteBuffer.wrap(ethReply.serialize())));
context.block();
}
use of org.onosproject.k8snode.api.K8sNode in project onos by opennetworkinglab.
the class K8sSwitchingGatewayHandler method setGatewayRule.
private void setGatewayRule(K8sNetwork k8sNetwork, boolean install) {
for (K8sNode node : k8sNodeService.completeNodes()) {
TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).matchIPDst(IpPrefix.valueOf(k8sNetwork.gatewayIp(), HOST_PREFIX));
TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
if (node.hostname().equals(k8sNetwork.name())) {
tBuilder.setEthDst(node.intgBridgeMac()).setOutput(node.intgEntryPortNum());
} else {
K8sNode localNode = k8sNodeService.node(k8sNetwork.name());
tBuilder.setOutput(node.intgToTunPortNum());
// install flows into tunnel bridge
PortNumber portNum = tunnelPortNumByNetId(k8sNetwork.networkId(), k8sNetworkService, node);
TrafficTreatment treatmentToRemote = DefaultTrafficTreatment.builder().extension(buildExtension(deviceService, node.tunBridge(), localNode.dataIp().getIp4Address()), node.tunBridge()).setTunnelId(Long.valueOf(k8sNetwork.segmentId())).setOutput(portNum).build();
k8sFlowRuleService.setRule(appId, node.tunBridge(), sBuilder.build(), treatmentToRemote, PRIORITY_GATEWAY_RULE, TUN_ENTRY_TABLE, install);
}
k8sFlowRuleService.setRule(appId, node.intgBridge(), sBuilder.build(), tBuilder.build(), PRIORITY_GATEWAY_RULE, ROUTING_TABLE, install);
if (node.hostname().equals(k8sNetwork.name())) {
sBuilder = DefaultTrafficSelector.builder().matchInPort(node.intgEntryPortNum()).matchEthType(Ethernet.TYPE_IPV4).matchIPDst(IpPrefix.valueOf(k8sNetwork.gatewayIp(), HOST_PREFIX));
tBuilder = DefaultTrafficTreatment.builder().setOutput(node.intgToLocalPatchPortNum());
k8sFlowRuleService.setRule(appId, node.intgBridge(), sBuilder.build(), tBuilder.build(), PRIORITY_LOCAL_BRIDGE_RULE, ROUTING_TABLE, install);
}
}
}
Aggregations