use of org.onosproject.k8snetworking.api.K8sPort in project onos by opennetworkinglab.
the class K8sNetworkingUtil method syncPortFromPod.
/**
* Synchronizes port from kubernetes POD.
*
* @param pod kubernetes POD
* @param adminService admin service
*/
public static void syncPortFromPod(Pod pod, K8sNetworkAdminService adminService) {
Map<String, String> annotations = pod.getMetadata().getAnnotations();
if (annotations != null && !annotations.isEmpty() && annotations.get(PORT_ID) != null) {
String portId = annotations.get(PORT_ID);
K8sPort oldPort = adminService.port(portId);
String networkId = annotations.get(NETWORK_ID);
DeviceId deviceId = DeviceId.deviceId(annotations.get(DEVICE_ID));
PortNumber portNumber = PortNumber.portNumber(annotations.get(PORT_NUMBER));
IpAddress ipAddress = IpAddress.valueOf(annotations.get(IP_ADDRESS));
MacAddress macAddress = MacAddress.valueOf(annotations.get(MAC_ADDRESS));
K8sPort newPort = DefaultK8sPort.builder().portId(portId).networkId(networkId).deviceId(deviceId).ipAddress(ipAddress).macAddress(macAddress).portNumber(portNumber).state(INACTIVE).build();
if (oldPort == null) {
adminService.createPort(newPort);
} else {
adminService.updatePort(newPort);
}
}
}
use of org.onosproject.k8snetworking.api.K8sPort in project onos by opennetworkinglab.
the class K8sPortWebResource method createPort.
/**
* Creates a port from the JSON input stream.
*
* @param input port JSON input stream
* @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
* is invalid or duplicated port already exists
* @onos.rsModel K8sPort
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createPort(InputStream input) {
log.trace(String.format(MESSAGE, "CREATE"));
URI location;
try {
ObjectNode jsonTree = readTreeFromStream(mapper(), input);
final K8sPort port = codec(K8sPort.class).decode(jsonTree, this);
adminService.createPort(port);
location = new URI(port.portId());
} catch (IOException | URISyntaxException e) {
throw new IllegalArgumentException(e);
}
return Response.created(location).build();
}
use of org.onosproject.k8snetworking.api.K8sPort in project onos by opennetworkinglab.
the class K8sPortListCommand method json.
private String json(List<K8sPort> ports) {
ObjectMapper mapper = new ObjectMapper();
ArrayNode result = mapper.createArrayNode();
for (K8sPort port : ports) {
result.add(jsonForEntity(port, K8sPort.class));
}
return prettyJson(mapper, result.toString());
}
use of org.onosproject.k8snetworking.api.K8sPort in project onos by opennetworkinglab.
the class K8sPortListCommand method doExecute.
@Override
protected void doExecute() {
K8sNetworkService service = get(K8sNetworkService.class);
List<K8sPort> ports = Lists.newArrayList(service.ports());
ports.sort(Comparator.comparing(K8sPort::networkId));
String format = genFormatString(ImmutableList.of(CLI_ID_LENGTH, CLI_NAME_LENGTH, CLI_MAC_ADDRESS_LENGTH, CLI_IP_ADDRESSES_LENGTH));
if (!Strings.isNullOrEmpty(networkId)) {
ports.removeIf(port -> !port.networkId().equals(networkId));
}
if (outputJson()) {
print("%s", json(ports));
} else {
print(format, "ID", "Network", "MAC Address", "Fixed IPs");
for (K8sPort port : ports) {
K8sNetwork k8sNet = service.network(port.networkId());
print(format, StringUtils.substring(port.portId(), 0, CLI_ID_LENGTH - CLI_MARGIN_LENGTH), k8sNet == null ? "" : StringUtils.substring(k8sNet.name(), 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(port.macAddress().toString(), 0, CLI_MAC_ADDRESS_LENGTH - CLI_MARGIN_LENGTH), port.ipAddress() == null ? "" : port.ipAddress());
}
}
}
use of org.onosproject.k8snetworking.api.K8sPort 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