Search in sources :

Example 11 with K8sHost

use of org.onosproject.k8snode.api.K8sHost in project onos by opennetworkinglab.

the class K8sNetworkingUtil method tunnelPortNumByNetType.

/**
 * Returns the tunnel port number with specified net type and kubernetes node.
 *
 * @param netType       network type
 * @param node          kubernetes node
 * @return tunnel port number
 */
public static PortNumber tunnelPortNumByNetType(K8sNetwork.Type netType, K8sNode node) {
    if (node.mode() == PASSTHROUGH) {
        K8sHostService hostService = DefaultServiceDirectory.getService(K8sHostService.class);
        Port port = null;
        for (K8sHost host : hostService.hosts()) {
            if (host.nodeNames().contains(node.hostname())) {
                for (K8sTunnelBridge bridge : host.tunBridges()) {
                    if (bridge.tunnelId() == node.segmentId()) {
                        String portName = netType.name().toLowerCase() + "-" + node.segmentId();
                        port = port(bridge.deviceId(), portName);
                    }
                }
            }
        }
        if (port == null) {
            return null;
        } else {
            return port.number();
        }
    } else {
        switch(netType) {
            case VXLAN:
                return node.vxlanPortNum();
            case GRE:
                return node.grePortNum();
            case GENEVE:
                return node.genevePortNum();
            default:
                return null;
        }
    }
}
Also used : Port(org.onosproject.net.Port) DefaultK8sPort(org.onosproject.k8snetworking.api.DefaultK8sPort) K8sPort(org.onosproject.k8snetworking.api.K8sPort) TpPort(org.onlab.packet.TpPort) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) K8sHost(org.onosproject.k8snode.api.K8sHost) K8sHostService(org.onosproject.k8snode.api.K8sHostService) K8sTunnelBridge(org.onosproject.k8snode.api.K8sTunnelBridge)

Example 12 with K8sHost

use of org.onosproject.k8snode.api.K8sHost in project onos by opennetworkinglab.

the class K8sNetworkingUtil method allK8sDevices.

/**
 * Returns all device identifiers belong to kubernetes nodes and hosts.
 *
 * @param nodeService   node service
 * @param hostService   host service
 * @return all device identifiers belong to kubernetes nodes and hosts
 */
public static Set<DeviceId> allK8sDevices(K8sNodeService nodeService, K8sHostService hostService) {
    Set<DeviceId> allDevIds = new HashSet<>();
    Set<DeviceId> intgDevIds = nodeService.completeNodes().stream().map(K8sNode::intgBridge).collect(Collectors.toSet());
    Set<DeviceId> extDevIds = nodeService.completeNodes().stream().map(K8sNode::extBridge).collect(Collectors.toSet());
    Set<DeviceId> tunDevIds = nodeService.completeNodes().stream().map(K8sNode::tunBridge).collect(Collectors.toSet());
    Set<DeviceId> localDevIds = nodeService.completeNodes().stream().map(K8sNode::localBridge).collect(Collectors.toSet());
    Set<DeviceId> hostTunDevIds = new HashSet<>();
    Set<DeviceId> hostRouterDevIds = new HashSet<>();
    for (K8sHost host : hostService.completeHosts()) {
        Set<K8sTunnelBridge> hostTunBrs = host.tunBridges();
        Set<K8sRouterBridge> hostRouterBrs = host.routerBridges();
        hostTunDevIds.addAll(hostTunBrs.stream().map(K8sTunnelBridge::deviceId).collect(Collectors.toSet()));
        hostRouterDevIds.addAll(hostRouterBrs.stream().map(K8sRouterBridge::deviceId).collect(Collectors.toSet()));
    }
    allDevIds.addAll(intgDevIds);
    allDevIds.addAll(extDevIds);
    allDevIds.addAll(tunDevIds);
    allDevIds.addAll(localDevIds);
    allDevIds.addAll(hostTunDevIds);
    allDevIds.addAll(hostRouterDevIds);
    return allDevIds;
}
Also used : DeviceId(org.onosproject.net.DeviceId) K8sHost(org.onosproject.k8snode.api.K8sHost) K8sRouterBridge(org.onosproject.k8snode.api.K8sRouterBridge) K8sTunnelBridge(org.onosproject.k8snode.api.K8sTunnelBridge) HashSet(java.util.HashSet)

Example 13 with K8sHost

use of org.onosproject.k8snode.api.K8sHost in project onos by opennetworkinglab.

the class K8sNodeWebResource method removeNodesFromHost.

/**
 * Remove a set of new nodes from the existing host.
 *
 * @param hostIp host IP address
 * @param input kubernetes node names JSON input stream
 * @return 200 UPDATED if the JSON is correct, 400 BAD_REQUEST if the JSON
 * is malformed
 * @onos.rsModel K8sNodeNames
 */
@PUT
@Path("host/delete/nodes/{hostIp}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response removeNodesFromHost(@PathParam("hostIp") String hostIp, InputStream input) {
    log.trace(String.format(MESSAGE_HOST, UPDATE));
    Set<String> newNodeNames = readNodeNamesConfiguration(input);
    K8sHost host = hostAdminService.host(IpAddress.valueOf(hostIp));
    Set<String> existNodeNames = host.nodeNames();
    existNodeNames.removeAll(newNodeNames);
    K8sHost updated = host.updateNodeNames(existNodeNames);
    hostAdminService.updateHost(updated);
    return Response.ok().build();
}
Also used : K8sHost(org.onosproject.k8snode.api.K8sHost) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) INDENT_OUTPUT(com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT) PUT(javax.ws.rs.PUT)

Example 14 with K8sHost

use of org.onosproject.k8snode.api.K8sHost in project onos by opennetworkinglab.

the class K8sNodeWebResource method createHosts.

/**
 * Creates a set of kubernetes hosts' config from the JSON input stream.
 *
 * @param input kubernetes hosts JSON input stream
 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
 * is malformed
 * @onos.rsModel K8sHosts
 */
@POST
@Path("host")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createHosts(InputStream input) {
    log.trace(String.format(MESSAGE_NODE, CREATE));
    readHostsConfiguration(input).forEach(host -> {
        K8sHost existing = hostAdminService.host(host.hostIp());
        if (existing == null) {
            hostAdminService.createHost(host);
        }
    });
    UriBuilder locationBuilder = uriInfo.getBaseUriBuilder().path(HOSTS).path(HOST_IP);
    return created(locationBuilder.build()).build();
}
Also used : K8sHost(org.onosproject.k8snode.api.K8sHost) UriBuilder(javax.ws.rs.core.UriBuilder) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 15 with K8sHost

use of org.onosproject.k8snode.api.K8sHost in project onos by opennetworkinglab.

the class K8sNodeWebResource method deleteHost.

/**
 * Removes a kubernetes host' config.
 *
 * @param hostIp host IP contained in kubernetes nodes configuration
 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and
 * 304 NOT_MODIFIED without the updated config
 */
@DELETE
@Path("host/{hostIp}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response deleteHost(@PathParam("hostIp") String hostIp) {
    log.trace(String.format(MESSAGE_HOST, REMOVE));
    K8sHost existing = hostAdminService.host(IpAddress.valueOf(nullIsIllegal(hostIp, HOST_IP + ERROR_MESSAGE)));
    if (existing == null) {
        log.warn("There is no host configuration to delete : {}", hostIp);
        return Response.notModified().build();
    } else {
        hostAdminService.removeHost(IpAddress.valueOf(nullIsIllegal(hostIp, HOST_IP + ERROR_MESSAGE)));
    }
    return Response.noContent().build();
}
Also used : K8sHost(org.onosproject.k8snode.api.K8sHost) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

K8sHost (org.onosproject.k8snode.api.K8sHost)19 DefaultK8sHost (org.onosproject.k8snode.api.DefaultK8sHost)6 Test (org.junit.Test)5 Consumes (javax.ws.rs.Consumes)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 K8sHostJsonMatcher.matchesK8sHost (org.onosproject.k8snode.codec.K8sHostJsonMatcher.matchesK8sHost)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 INDENT_OUTPUT (com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 HashSet (java.util.HashSet)2 PUT (javax.ws.rs.PUT)2 K8sHostService (org.onosproject.k8snode.api.K8sHostService)2 K8sRouterBridge (org.onosproject.k8snode.api.K8sRouterBridge)2 K8sTunnelBridge (org.onosproject.k8snode.api.K8sTunnelBridge)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)1 InputStream (java.io.InputStream)1 DELETE (javax.ws.rs.DELETE)1