Search in sources :

Example 6 with K8sPort

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);
        }
    }
}
Also used : DeviceId(org.onosproject.net.DeviceId) DefaultK8sPort(org.onosproject.k8snetworking.api.DefaultK8sPort) K8sPort(org.onosproject.k8snetworking.api.K8sPort) IpAddress(org.onlab.packet.IpAddress) PortNumber(org.onosproject.net.PortNumber) MacAddress(org.onlab.packet.MacAddress)

Example 7 with K8sPort

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();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) K8sPort(org.onosproject.k8snetworking.api.K8sPort) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 8 with K8sPort

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());
}
Also used : K8sPort(org.onosproject.k8snetworking.api.K8sPort) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 9 with K8sPort

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());
        }
    }
}
Also used : K8sNetwork(org.onosproject.k8snetworking.api.K8sNetwork) K8sNetworkService(org.onosproject.k8snetworking.api.K8sNetworkService) K8sPort(org.onosproject.k8snetworking.api.K8sPort) K8sNetworkingUtil.genFormatString(org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString)

Example 10 with K8sPort

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);
    });
}
Also used : VXLAN(org.onosproject.k8snetworking.api.K8sNetwork.Type.VXLAN) ARP_TABLE(org.onosproject.k8snetworking.api.Constants.ARP_TABLE) CoreService(org.onosproject.core.CoreService) PortNumber(org.onosproject.net.PortNumber) DeviceService(org.onosproject.net.device.DeviceService) Tools.groupedThreads(org.onlab.util.Tools.groupedThreads) K8sNodeListener(org.onosproject.k8snode.api.K8sNodeListener) K8sNetworkListener(org.onosproject.k8snetworking.api.K8sNetworkListener) DriverService(org.onosproject.net.driver.DriverService) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) Ethernet(org.onlab.packet.Ethernet) ApplicationId(org.onosproject.core.ApplicationId) MastershipService(org.onosproject.mastership.MastershipService) NodeId(org.onosproject.cluster.NodeId) K8sNetworkingUtil.tunnelPortNumByNetId(org.onosproject.k8snetworking.util.K8sNetworkingUtil.tunnelPortNumByNetId) Deactivate(org.osgi.service.component.annotations.Deactivate) Set(java.util.Set) K8sFlowRuleService(org.onosproject.k8snetworking.api.K8sFlowRuleService) Executors.newSingleThreadExecutor(java.util.concurrent.Executors.newSingleThreadExecutor) GRE(org.onosproject.k8snetworking.api.K8sNetwork.Type.GRE) Objects(java.util.Objects) GENEVE(org.onosproject.k8snetworking.api.K8sNetwork.Type.GENEVE) ARP_BROADCAST_MODE(org.onosproject.k8snetworking.api.Constants.ARP_BROADCAST_MODE) TUN_ENTRY_TABLE(org.onosproject.k8snetworking.api.Constants.TUN_ENTRY_TABLE) ClusterService(org.onosproject.cluster.ClusterService) DeviceId(org.onosproject.net.DeviceId) IpPrefix(org.onlab.packet.IpPrefix) PRIORITY_DEFAULT_RULE(org.onosproject.k8snetworking.api.Constants.PRIORITY_DEFAULT_RULE) PRIORITY_TUNNEL_TAG_RULE(org.onosproject.k8snetworking.api.Constants.PRIORITY_TUNNEL_TAG_RULE) K8sNetworkService(org.onosproject.k8snetworking.api.K8sNetworkService) Strings(com.google.common.base.Strings) Component(org.osgi.service.component.annotations.Component) TrafficSelector(org.onosproject.net.flow.TrafficSelector) FORWARDING_TABLE(org.onosproject.k8snetworking.api.Constants.FORWARDING_TABLE) K8sNetworkEvent(org.onosproject.k8snetworking.api.K8sNetworkEvent) K8sPort(org.onosproject.k8snetworking.api.K8sPort) JUMP_TABLE(org.onosproject.k8snetworking.api.Constants.JUMP_TABLE) K8sNodeEvent(org.onosproject.k8snode.api.K8sNodeEvent) Activate(org.osgi.service.component.annotations.Activate) VTAG_TABLE(org.onosproject.k8snetworking.api.Constants.VTAG_TABLE) K8sNetworkingUtil.getPropertyValue(org.onosproject.k8snetworking.util.K8sNetworkingUtil.getPropertyValue) K8sNode(org.onosproject.k8snode.api.K8sNode) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) ExecutorService(java.util.concurrent.ExecutorService) K8S_NETWORKING_APP_ID(org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID) ComponentConfigService(org.onosproject.cfg.ComponentConfigService) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) Logger(org.slf4j.Logger) K8sNetworkingUtil.tunnelPortNumByNetType(org.onosproject.k8snetworking.util.K8sNetworkingUtil.tunnelPortNumByNetType) RulePopulatorUtil.buildExtension(org.onosproject.k8snetworking.util.RulePopulatorUtil.buildExtension) PRIORITY_SWITCHING_RULE(org.onosproject.k8snetworking.api.Constants.PRIORITY_SWITCHING_RULE) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) ConfigProperty(org.onosproject.cfg.ConfigProperty) K8sNetwork(org.onosproject.k8snetworking.api.K8sNetwork) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) K8sNodeService(org.onosproject.k8snode.api.K8sNodeService) Reference(org.osgi.service.component.annotations.Reference) LeadershipService(org.onosproject.cluster.LeadershipService) K8sNode(org.onosproject.k8snode.api.K8sNode) TrafficSelector(org.onosproject.net.flow.TrafficSelector) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) PortNumber(org.onosproject.net.PortNumber)

Aggregations

K8sPort (org.onosproject.k8snetworking.api.K8sPort)15 DefaultK8sPort (org.onosproject.k8snetworking.api.DefaultK8sPort)5 K8sNetwork (org.onosproject.k8snetworking.api.K8sNetwork)5 Set (java.util.Set)4 MacAddress (org.onlab.packet.MacAddress)4 K8sNetworkService (org.onosproject.k8snetworking.api.K8sNetworkService)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 ExecutorService (java.util.concurrent.ExecutorService)3 IpAddress (org.onlab.packet.IpAddress)3 Tools.groupedThreads (org.onlab.util.Tools.groupedThreads)3 CoreService (org.onosproject.core.CoreService)3 K8S_NETWORKING_APP_ID (org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID)3 K8sPortJsonMatcher.matchesK8sPort (org.onosproject.k8snetworking.codec.K8sPortJsonMatcher.matchesK8sPort)3 DeviceId (org.onosproject.net.DeviceId)3 Strings (com.google.common.base.Strings)2 IOException (java.io.IOException)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.Test)2 VlanId (org.onlab.packet.VlanId)2