use of org.onosproject.k8snetworking.api.K8sNetwork in project onos by opennetworkinglab.
the class K8sNetworkListCommand method doExecute.
@Override
protected void doExecute() {
K8sNetworkService service = get(K8sNetworkService.class);
List<K8sNetwork> networks = Lists.newArrayList(service.networks());
networks.sort(Comparator.comparing(K8sNetwork::name));
String format = genFormatString(ImmutableList.of(CLI_ID_LENGTH, CLI_NAME_LENGTH, CLI_TYPE_LENGTH, CLI_SEG_ID_LENGTH, CLI_IP_ADDRESS_LENGTH));
if (outputJson()) {
print("%s", json(networks));
} else {
print(format, "ID", "Name", "Type", "SegId", "Gateway");
for (K8sNetwork net : networks) {
print(format, StringUtils.substring(net.networkId(), 0, CLI_ID_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(net.name(), 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), net.type().toString(), net.segmentId(), net.gatewayIp() == null ? "" : net.gatewayIp().toString());
}
}
}
use of org.onosproject.k8snetworking.api.K8sNetwork in project onos by opennetworkinglab.
the class K8sIpAddressListCommand method doExecute.
@Override
protected void doExecute() {
K8sIpamService ipamService = get(K8sIpamService.class);
K8sNetworkService networkService = get(K8sNetworkService.class);
if (networkIds == null || networkIds.length == 0) {
networkIds = networkService.networks().stream().map(K8sNetwork::networkId).toArray(String[]::new);
}
Map<String, Map<IpAddress, MacAddress>> ipMacs = Maps.newConcurrentMap();
if (available && reserved) {
error("Only one of list options (available | reserved) can be specified.");
return;
}
if (!(available || reserved)) {
error("At least one of list options (available | reserved) should be specified.");
return;
}
for (String networkId : networkIds) {
Map<IpAddress, MacAddress> tmpIpMacs = Maps.newConcurrentMap();
if (available) {
ipamService.availableIps(networkId).forEach(n -> tmpIpMacs.put(n, ZERO));
}
if (reserved) {
Set<K8sPort> ports = networkService.ports(networkId);
ipamService.allocatedIps(networkId).forEach(ip -> {
MacAddress mac = ports.stream().filter(p -> p.ipAddress().equals(ip)).map(K8sPort::macAddress).findAny().orElse(ZERO);
tmpIpMacs.put(ip, mac);
});
}
ipMacs.put(networkId, tmpIpMacs);
}
if (ipMacs.size() > 0) {
print(FORMAT, "Network ID", "IP Address", "MAC Address");
ipMacs.forEach((k, v) -> v.forEach((ip, mac) -> print(FORMAT, k, ip, mac)));
} else {
print("No IP addresses are available or reserved.");
}
}
use of org.onosproject.k8snetworking.api.K8sNetwork in project onos by opennetworkinglab.
the class K8sOpenstackIntegrationHandler method setCniPtNodeRules.
private void setCniPtNodeRules(K8sNode k8sNode, boolean install) {
K8sNetwork network = k8sNetworkService.network(k8sNode.hostname());
String k8sNodeIp = k8sNode.nodeIp().toString();
String gatewayIp = network.gatewayIp().toString();
String nodePodCidr = network.cidr();
String srcPodPrefix = getBclassIpPrefixFromCidr(nodePodCidr);
String podCidr = srcPodPrefix + B_CLASS_SUFFIX;
String osK8sIntPortName = k8sNode.osToK8sIntgPatchPortName();
String k8sIntOsPortMac = k8sNode.portMacByName(k8sNode.intgBridge(), k8sNode.k8sIntgToOsPatchPortName()).toString();
String path = install ? "node/pt-install" : "node/pt-uninstall";
String jsonString = "";
try {
jsonString = new JSONObject().put(K8S_NODE_IP, k8sNodeIp).put(POD_GW_IP, gatewayIp).put(POD_CIDR, podCidr).put(SERVICE_CIDR, SERVICE_IP_CIDR_DEFAULT).put(OS_K8S_INT_PORT_NAME, osK8sIntPortName).put(K8S_INT_OS_PORT_MAC, k8sIntOsPortMac).toString();
log.info("push integration configuration {}", jsonString);
} catch (JSONException e) {
log.error("Failed to generate JSON string");
return;
}
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(ONOS_USERNAME, ONOS_PASSWORD);
final Client client = ClientBuilder.newClient();
client.register(feature);
String host = "http://" + k8sNode.managementIp().toString() + ":" + ONOS_PORT + "/";
String endpoint = host + OS_K8S_INTEGRATION_EP;
WebTarget wt = client.target(endpoint).path(path);
Response response = wt.request(MediaType.APPLICATION_JSON_TYPE).put(Entity.json(jsonString));
final int status = response.getStatus();
if (status != 200) {
log.error("Failed to install/uninstall openstack k8s CNI PT rules.");
}
}
use of org.onosproject.k8snetworking.api.K8sNetwork 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.k8snetworking.api.K8sNetwork in project onos by opennetworkinglab.
the class K8sSwitchingHandler method setLocalTunnelTagFlowRules.
private void setLocalTunnelTagFlowRules(K8sNode k8sNode, boolean install) {
TrafficSelector selector = DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).matchInPort(k8sNode.intgEntryPortNum()).build();
K8sNetwork net = k8sNetworkService.network(k8sNode.hostname());
TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder().setTunnelId(Long.valueOf(net.segmentId())).transition(JUMP_TABLE);
k8sFlowRuleService.setRule(appId, k8sNode.intgBridge(), selector, tBuilder.build(), PRIORITY_TUNNEL_TAG_RULE, VTAG_TABLE, install);
}
Aggregations