Search in sources :

Example 1 with KubevirtPhyInterface

use of org.onosproject.kubevirtnode.api.KubevirtPhyInterface in project onos by opennetworkinglab.

the class DefaultKubevirtNodeHandler method isDeviceCreatedStateDone.

private boolean isDeviceCreatedStateDone(KubevirtNode node) {
    try {
        // we need to wait a while, in case tunneling ports
        // creation requires some time
        sleep(SLEEP_MID_MS);
    } catch (InterruptedException e) {
        log.error("Exception caused during init state checking...");
    }
    if (node.dataIp() != null && !isIntfEnabled(node, VXLAN)) {
        log.warn("VXLAN interface is not enabled!");
        return false;
    }
    if (node.dataIp() != null && !isIntfEnabled(node, GRE)) {
        log.warn("GRE interface is not enabled!");
        return false;
    }
    if (node.dataIp() != null && !isIntfEnabled(node, GENEVE)) {
        log.warn("GENEVE interface is not enabled!");
        return false;
    }
    for (KubevirtPhyInterface phyIntf : node.phyIntfs()) {
        if (phyIntf == null) {
            log.warn("Physnet interface is invalid");
            return false;
        }
        try {
            // we need to wait a while, in case tunneling ports
            // creation requires some time
            sleep(SLEEP_LONG_MS);
        } catch (InterruptedException e) {
            log.error("Exception caused during init state checking...");
        }
        String bridgeName = BRIDGE_PREFIX + phyIntf.network();
        String patchPortName = structurePortName(INTEGRATION_TO_PHYSICAL_PREFIX + phyIntf.network());
        if (!(hasPhyBridge(node, bridgeName) && hasPhyPatchPort(node, patchPortName) && hasPhyIntf(node, phyIntf.intf()))) {
            log.warn("PhyBridge {}", hasPhyBridge(node, bridgeName));
            log.warn("hasPhyPatchPort {}", hasPhyPatchPort(node, patchPortName));
            log.warn("hasPhyIntf {}", hasPhyIntf(node, phyIntf.intf()));
            return false;
        }
    }
    if (node.type() == GATEWAY) {
        if (!(hasPhyIntf(node, INTEGRATION_TO_TUNNEL) && hasPhyIntf(node, TUNNEL_TO_INTEGRATION))) {
            log.warn("IntToTunPort {}", hasPhyIntf(node, INTEGRATION_TO_TUNNEL));
            log.warn("TunToIntPort {}", hasPhyIntf(node, TUNNEL_TO_INTEGRATION));
            return false;
        }
    }
    return true;
}
Also used : KubevirtPhyInterface(org.onosproject.kubevirtnode.api.KubevirtPhyInterface)

Example 2 with KubevirtPhyInterface

use of org.onosproject.kubevirtnode.api.KubevirtPhyInterface in project onos by opennetworkinglab.

the class KubevirtShowNodeCommand method printNode.

private void printNode(KubevirtNode node) {
    print("Name: %s", node.hostname());
    print("  Type: %s", node.type());
    print("  State: %s", node.state());
    print("  Management IP: %s", node.managementIp().toString());
    print("  Data IP: %s", node.dataIp().toString());
    print("  Integration Bridge: %s", node.intgBridge());
    print("  Tunneling Bridge: %s", node.tunBridge());
    int counter = 1;
    for (KubevirtPhyInterface intf : node.phyIntfs()) {
        print("  Physical interfaces #%d:", counter);
        print("    Network: %s", intf.network());
        print("    Interface: %s", intf.intf());
        counter++;
    }
}
Also used : KubevirtPhyInterface(org.onosproject.kubevirtnode.api.KubevirtPhyInterface)

Example 3 with KubevirtPhyInterface

use of org.onosproject.kubevirtnode.api.KubevirtPhyInterface in project onos by opennetworkinglab.

the class KubevirtNodeJsonMatcher method matchesSafely.

@Override
protected boolean matchesSafely(JsonNode jsonNode, Description description) {
    // check hostname
    String jsonHostname = jsonNode.get(Constants.HOST_NAME).asText();
    String hostname = node.hostname();
    if (!jsonHostname.equals(hostname)) {
        description.appendText("hostname was " + jsonHostname);
        return false;
    }
    // check type
    String jsonType = jsonNode.get(Constants.TYPE).asText();
    String type = node.type().name();
    if (!jsonType.equals(type)) {
        description.appendText("type was " + jsonType);
        return false;
    }
    // check management IP
    String jsonMgmtIp = jsonNode.get(Constants.MANAGEMENT_IP).asText();
    String mgmtIp = node.managementIp().toString();
    if (!jsonMgmtIp.equals(mgmtIp)) {
        description.appendText("management IP was " + jsonMgmtIp);
        return false;
    }
    // check integration bridge
    JsonNode jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE);
    if (jsonIntgBridge != null) {
        String intgBridge = node.intgBridge().toString();
        if (!jsonIntgBridge.asText().equals(intgBridge)) {
            description.appendText("integration bridge was " + jsonIntgBridge);
            return false;
        }
    }
    // check tunnel bridge
    JsonNode jsonTunBridge = jsonNode.get(TUNNEL_BRIDGE);
    if (jsonTunBridge != null) {
        String tunBridge = node.tunBridge().toString();
        if (!jsonTunBridge.asText().equals(tunBridge)) {
            description.appendText("tunnel bridge was " + jsonTunBridge);
            return false;
        }
    }
    // check state
    String jsonState = jsonNode.get(STATE).asText();
    String state = node.state().name();
    if (!jsonState.equals(state)) {
        description.appendText("state was " + jsonState);
        return false;
    }
    // check data IP
    JsonNode jsonDataIp = jsonNode.get(Constants.DATA_IP);
    if (jsonDataIp != null) {
        String dataIp = node.dataIp().toString();
        if (!jsonDataIp.asText().equals(dataIp)) {
            description.appendText("Data IP was " + jsonDataIp.asText());
            return false;
        }
    }
    // check physical interfaces
    JsonNode jsonPhyIntfs = jsonNode.get(PHYSICAL_INTERFACES);
    if (jsonPhyIntfs != null) {
        if (jsonPhyIntfs.size() != node.phyIntfs().size()) {
            description.appendText("physical interface size was " + jsonPhyIntfs.size());
            return false;
        }
        for (KubevirtPhyInterface phyIntf : node.phyIntfs()) {
            boolean intfFound = false;
            for (int intfIndex = 0; intfIndex < jsonPhyIntfs.size(); intfIndex++) {
                KubevirtPhyInterfaceJsonMatcher intfMatcher = KubevirtPhyInterfaceJsonMatcher.matchesKubevirtPhyInterface(phyIntf);
                if (intfMatcher.matches(jsonPhyIntfs.get(intfIndex))) {
                    intfFound = true;
                    break;
                }
            }
            if (!intfFound) {
                description.appendText("PhyIntf not found " + phyIntf.toString());
                return false;
            }
        }
    }
    return true;
}
Also used : KubevirtPhyInterface(org.onosproject.kubevirtnode.api.KubevirtPhyInterface) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 4 with KubevirtPhyInterface

use of org.onosproject.kubevirtnode.api.KubevirtPhyInterface in project onos by opennetworkinglab.

the class KubevirtNodeCodec method decode.

@Override
public KubevirtNode decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    String hostname = nullIsIllegal(json.get(HOST_NAME).asText(), HOST_NAME + MISSING_MESSAGE);
    String type = nullIsIllegal(json.get(TYPE).asText(), TYPE + MISSING_MESSAGE);
    String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(), MANAGEMENT_IP + MISSING_MESSAGE);
    KubevirtNode.Builder nodeBuilder = DefaultKubevirtNode.builder().hostname(hostname).type(KubevirtNode.Type.valueOf(type)).state(KubevirtNodeState.INIT).managementIp(IpAddress.valueOf(mIp));
    if (json.get(DATA_IP) != null) {
        nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
    }
    JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
    if (intBridgeJson != null) {
        nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
    }
    JsonNode tunBridgeJson = json.get(TUNNEL_BRIDGE);
    if (tunBridgeJson != null) {
        nodeBuilder.tunBridge(DeviceId.deviceId(tunBridgeJson.asText()));
    }
    // parse physical interfaces
    List<KubevirtPhyInterface> phyIntfs = new ArrayList<>();
    JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES);
    if (phyIntfsJson != null) {
        final JsonCodec<KubevirtPhyInterface> phyIntfCodec = context.codec(KubevirtPhyInterface.class);
        IntStream.range(0, phyIntfsJson.size()).forEach(i -> {
            ObjectNode intfJson = get(phyIntfsJson, i);
            phyIntfs.add(phyIntfCodec.decode(intfJson, context));
        });
    }
    nodeBuilder.phyIntfs(phyIntfs);
    JsonNode externalBridgeJson = json.get(GATEWAY_BRIDGE_NAME);
    if (externalBridgeJson != null) {
        nodeBuilder.gatewayBridgeName(externalBridgeJson.asText());
    }
    log.trace("node is {}", nodeBuilder.build().toString());
    return nodeBuilder.build();
}
Also used : KubevirtNode(org.onosproject.kubevirtnode.api.KubevirtNode) DefaultKubevirtNode(org.onosproject.kubevirtnode.api.DefaultKubevirtNode) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) KubevirtPhyInterface(org.onosproject.kubevirtnode.api.KubevirtPhyInterface) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 5 with KubevirtPhyInterface

use of org.onosproject.kubevirtnode.api.KubevirtPhyInterface in project onos by opennetworkinglab.

the class KubevirtNodeUtil method buildKubevirtNode.

/**
 * Returns the kubevirt node from the node.
 *
 * @param node a raw node object returned from a k8s client
 * @return kubevirt node
 */
public static KubevirtNode buildKubevirtNode(Node node) {
    String hostname = node.getMetadata().getName();
    IpAddress managementIp = null;
    IpAddress dataIp = null;
    for (NodeAddress nodeAddress : node.getStatus().getAddresses()) {
        if (nodeAddress.getType().equals(INTERNAL_IP)) {
            managementIp = IpAddress.valueOf(nodeAddress.getAddress());
            dataIp = IpAddress.valueOf(nodeAddress.getAddress());
        }
    }
    Set<String> rolesFull = node.getMetadata().getLabels().keySet().stream().filter(l -> l.contains(K8S_ROLE)).collect(Collectors.toSet());
    KubevirtNode.Type nodeType = WORKER;
    for (String roleStr : rolesFull) {
        String role = roleStr.split("/")[1];
        if (MASTER.name().equalsIgnoreCase(role)) {
            nodeType = MASTER;
            break;
        }
    }
    // start to parse kubernetes annotation
    Map<String, String> annots = node.getMetadata().getAnnotations();
    String physnetConfig = annots.get(PHYSNET_CONFIG_KEY);
    String gatewayConfig = annots.get(GATEWAY_CONFIG_KEY);
    String dataIpStr = annots.get(DATA_IP_KEY);
    Set<KubevirtPhyInterface> phys = new HashSet<>();
    String gatewayBridgeName = null;
    try {
        if (physnetConfig != null) {
            JSONArray configJson = new JSONArray(physnetConfig);
            for (int i = 0; i < configJson.length(); i++) {
                JSONObject object = configJson.getJSONObject(i);
                String network = object.getString(NETWORK_KEY);
                String intf = object.getString(INTERFACE_KEY);
                if (network != null && intf != null) {
                    phys.add(DefaultKubevirtPhyInterface.builder().network(network).intf(intf).build());
                }
            }
        }
        if (dataIpStr != null) {
            dataIp = IpAddress.valueOf(dataIpStr);
        }
        if (gatewayConfig != null) {
            JsonNode jsonNode = new ObjectMapper().readTree(gatewayConfig);
            nodeType = GATEWAY;
            gatewayBridgeName = jsonNode.get(GATEWAY_BRIDGE_NAME).asText();
        }
    } catch (JSONException | JsonProcessingException e) {
        log.error("Failed to parse physnet config or gateway config object", e);
    }
    // if the node is taint with kubevirt.io key configured,
    // we mark this node as OTHER type, and do not add it into the cluster
    NodeSpec spec = node.getSpec();
    if (spec.getTaints() != null) {
        for (Taint taint : spec.getTaints()) {
            String effect = taint.getEffect();
            String key = taint.getKey();
            String value = taint.getValue();
            if (StringUtils.equals(effect, NO_SCHEDULE_EFFECT) && StringUtils.equals(key, KUBEVIRT_IO_KEY) && StringUtils.equals(value, DRAINING_VALUE)) {
                nodeType = OTHER;
            }
        }
    }
    return DefaultKubevirtNode.builder().hostname(hostname).managementIp(managementIp).dataIp(dataIp).type(nodeType).state(KubevirtNodeState.ON_BOARDED).phyIntfs(phys).gatewayBridgeName(gatewayBridgeName).build();
}
Also used : Address(org.xbill.DNS.Address) StringUtils(org.apache.commons.lang.StringUtils) OvsdbNodeId(org.onosproject.ovsdb.controller.OvsdbNodeId) GATEWAY(org.onosproject.kubevirtnode.api.KubevirtNode.Type.GATEWAY) DeviceService(org.onosproject.net.device.DeviceService) LoggerFactory(org.slf4j.LoggerFactory) DefaultKubevirtNode(org.onosproject.kubevirtnode.api.DefaultKubevirtNode) KubevirtApiConfig(org.onosproject.kubevirtnode.api.KubevirtApiConfig) NodeSpec(io.fabric8.kubernetes.api.model.NodeSpec) DefaultKubevirtPhyInterface(org.onosproject.kubevirtnode.api.DefaultKubevirtPhyInterface) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) JSONException(org.json.JSONException) WORKER(org.onosproject.kubevirtnode.api.KubevirtNode.Type.WORKER) JSONObject(org.json.JSONObject) OvsdbController(org.onosproject.ovsdb.controller.OvsdbController) OTHER(org.onosproject.kubevirtnode.api.KubevirtNode.Type.OTHER) NodeAddress(io.fabric8.kubernetes.api.model.NodeAddress) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) Node(io.fabric8.kubernetes.api.model.Node) IpAddress(org.onlab.packet.IpAddress) KubevirtNode(org.onosproject.kubevirtnode.api.KubevirtNode) Tools.get(org.onlab.util.Tools.get) Logger(org.slf4j.Logger) Device(org.onosproject.net.Device) Taint(io.fabric8.kubernetes.api.model.Taint) BridgeName(org.onosproject.net.behaviour.BridgeName) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) MASTER(org.onosproject.kubevirtnode.api.KubevirtNode.Type.MASTER) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) KubevirtPhyInterface(org.onosproject.kubevirtnode.api.KubevirtPhyInterface) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) List(java.util.List) SONA_PROJECT_DOMAIN(org.onosproject.kubevirtnode.api.Constants.SONA_PROJECT_DOMAIN) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) OvsdbClientService(org.onosproject.ovsdb.controller.OvsdbClientService) ConfigBuilder(io.fabric8.kubernetes.client.ConfigBuilder) KubevirtNodeState(org.onosproject.kubevirtnode.api.KubevirtNodeState) JSONArray(org.json.JSONArray) Dictionary(java.util.Dictionary) BridgeConfig(org.onosproject.net.behaviour.BridgeConfig) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JsonNode(com.fasterxml.jackson.databind.JsonNode) Taint(io.fabric8.kubernetes.api.model.Taint) Taint(io.fabric8.kubernetes.api.model.Taint) DefaultKubevirtNode(org.onosproject.kubevirtnode.api.DefaultKubevirtNode) KubevirtNode(org.onosproject.kubevirtnode.api.KubevirtNode) JSONObject(org.json.JSONObject) DefaultKubevirtPhyInterface(org.onosproject.kubevirtnode.api.DefaultKubevirtPhyInterface) KubevirtPhyInterface(org.onosproject.kubevirtnode.api.KubevirtPhyInterface) NodeSpec(io.fabric8.kubernetes.api.model.NodeSpec) IpAddress(org.onlab.packet.IpAddress) NodeAddress(io.fabric8.kubernetes.api.model.NodeAddress) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Aggregations

KubevirtPhyInterface (org.onosproject.kubevirtnode.api.KubevirtPhyInterface)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 DefaultKubevirtNode (org.onosproject.kubevirtnode.api.DefaultKubevirtNode)3 KubevirtNode (org.onosproject.kubevirtnode.api.KubevirtNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 DefaultKubevirtPhyInterface (org.onosproject.kubevirtnode.api.DefaultKubevirtPhyInterface)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Strings (com.google.common.base.Strings)1 Node (io.fabric8.kubernetes.api.model.Node)1 NodeAddress (io.fabric8.kubernetes.api.model.NodeAddress)1 NodeSpec (io.fabric8.kubernetes.api.model.NodeSpec)1 Taint (io.fabric8.kubernetes.api.model.Taint)1 ConfigBuilder (io.fabric8.kubernetes.client.ConfigBuilder)1 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1