Search in sources :

Example 1 with K8sApiConfig

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

the class K8sManagementWebResource method syncStates.

/**
 * Synchronizes the all states with kubernetes API server.
 *
 * @return 200 OK with sync result, 404 not found
 * @throws InterruptedException exception
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("sync/states")
public Response syncStates() {
    K8sApiConfig config = configService.apiConfigs().stream().findAny().orElse(null);
    if (config == null) {
        throw new ItemNotFoundException("Failed to find valid kubernetes API configuration.");
    }
    KubernetesClient client = K8sNetworkingUtil.k8sClient(config);
    if (client == null) {
        throw new ItemNotFoundException("Failed to connect to kubernetes API server.");
    }
    client.namespaces().list().getItems().forEach(ns -> {
        if (namespaceAdminService.namespace(ns.getMetadata().getUid()) != null) {
            namespaceAdminService.updateNamespace(ns);
        } else {
            namespaceAdminService.createNamespace(ns);
        }
    });
    client.services().inAnyNamespace().list().getItems().forEach(svc -> {
        if (serviceAdminService.service(svc.getMetadata().getUid()) != null) {
            serviceAdminService.updateService(svc);
        } else {
            serviceAdminService.createService(svc);
        }
    });
    client.endpoints().inAnyNamespace().list().getItems().forEach(ep -> {
        if (endpointsAdminService.endpoints(ep.getMetadata().getUid()) != null) {
            endpointsAdminService.updateEndpoints(ep);
        } else {
            endpointsAdminService.createEndpoints(ep);
        }
    });
    client.pods().inAnyNamespace().list().getItems().forEach(pod -> {
        if (podAdminService.pod(pod.getMetadata().getUid()) != null) {
            podAdminService.updatePod(pod);
        } else {
            podAdminService.createPod(pod);
        }
        syncPortFromPod(pod, networkAdminService);
    });
    client.extensions().ingresses().inAnyNamespace().list().getItems().forEach(ingress -> {
        if (ingressAdminService.ingress(ingress.getMetadata().getUid()) != null) {
            ingressAdminService.updateIngress(ingress);
        } else {
            ingressAdminService.createIngress(ingress);
        }
    });
    client.network().networkPolicies().inAnyNamespace().list().getItems().forEach(policy -> {
        if (policyAdminService.networkPolicy(policy.getMetadata().getUid()) != null) {
            policyAdminService.updateNetworkPolicy(policy);
        } else {
            policyAdminService.createNetworkPolicy(policy);
        }
    });
    return ok(mapper().createObjectNode()).build();
}
Also used : K8sApiConfig(org.onosproject.k8snode.api.K8sApiConfig) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ItemNotFoundException(org.onlab.util.ItemNotFoundException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with K8sApiConfig

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

the class K8sNodeWebResource method deleteApiConfig.

/**
 * Removes a kubernetes API config.
 *
 * @param endpoint kubernetes API endpoint
 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed
 */
@DELETE
@Path("api/{endpoint : .+}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response deleteApiConfig(@PathParam("endpoint") String endpoint) {
    log.trace(String.format(MESSAGE_NODE, REMOVE));
    K8sApiConfig existing = configAdminService.apiConfig(nullIsIllegal(endpoint, ENDPOINT + ERROR_MESSAGE));
    if (existing == null) {
        log.warn("There is no API configuration to delete : {}", endpoint);
        return Response.notModified().build();
    } else {
        configAdminService.removeApiConfig(endpoint);
    }
    return Response.noContent().build();
}
Also used : K8sApiConfig(org.onosproject.k8snode.api.K8sApiConfig) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 3 with K8sApiConfig

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

the class K8sNodeWebResource method readApiConfigConfiguration.

private Set<K8sApiConfig> readApiConfigConfiguration(InputStream input) {
    Set<K8sApiConfig> configSet = Sets.newHashSet();
    try {
        JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
        ArrayNode configs = (ArrayNode) jsonTree.path(API_CONFIGS);
        configs.forEach(config -> {
            try {
                ObjectNode objectNode = config.deepCopy();
                K8sApiConfig k8sApiConfig = codec(K8sApiConfig.class).decode(objectNode, this);
                configSet.add(k8sApiConfig);
            } catch (Exception e) {
                log.error("Exception occurred due to {}", e);
                throw new IllegalArgumentException();
            }
        });
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
    return configSet;
}
Also used : K8sApiConfig(org.onosproject.k8snode.api.K8sApiConfig) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 4 with K8sApiConfig

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

the class K8sApiConfigCodec method decode.

@Override
public K8sApiConfig decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    JsonNode clusterNameJson = json.get(CLUSTER_NAME);
    String clusterNameStr = "";
    if (clusterNameJson == null) {
        clusterNameStr = DEFAULT_CLUSTER_NAME;
    } else {
        clusterNameStr = clusterNameJson.asText();
    }
    JsonNode segmentIdJson = json.get(SEGMENT_ID);
    int segmentId = DEFAULT_SEGMENT_ID;
    if (segmentIdJson != null) {
        segmentId = segmentIdJson.asInt();
    }
    JsonNode modeJson = json.get(MODE);
    String modeStr = "";
    if (modeJson == null) {
        modeStr = DEFAULT_CONFIG_MODE;
    } else {
        modeStr = modeJson.asText();
    }
    Mode mode = Mode.valueOf(modeStr);
    Scheme scheme = Scheme.valueOf(nullIsIllegal(json.get(SCHEME).asText(), SCHEME + MISSING_MESSAGE));
    IpAddress ipAddress = IpAddress.valueOf(nullIsIllegal(json.get(IP_ADDRESS).asText(), IP_ADDRESS + MISSING_MESSAGE));
    int port = json.get(PORT).asInt();
    K8sApiConfig.Builder builder = DefaultK8sApiConfig.builder().clusterName(clusterNameStr).segmentId(segmentId).mode(mode).scheme(scheme).ipAddress(ipAddress).port(port).state(DISCONNECTED);
    JsonNode dvrJson = json.get(DVR);
    if (dvrJson != null) {
        builder.dvr(dvrJson.asBoolean());
    }
    JsonNode tokenJson = json.get(TOKEN);
    JsonNode caCertDataJson = json.get(CA_CERT_DATA);
    JsonNode clientCertDataJson = json.get(CLIENT_CERT_DATA);
    JsonNode clientKeyDataJson = json.get(CLIENT_KEY_DATA);
    String token = "";
    String caCertData = "";
    String clientCertData = "";
    String clientKeyData = "";
    if (scheme == HTTPS) {
        caCertData = nullIsIllegal(caCertDataJson.asText(), CA_CERT_DATA + MISSING_MESSAGE);
        clientCertData = nullIsIllegal(clientCertDataJson.asText(), CLIENT_CERT_DATA + MISSING_MESSAGE);
        clientKeyData = nullIsIllegal(clientKeyDataJson.asText(), CLIENT_KEY_DATA + MISSING_MESSAGE);
        if (tokenJson != null) {
            token = tokenJson.asText();
        }
    } else {
        if (tokenJson != null) {
            token = tokenJson.asText();
        }
        if (caCertDataJson != null) {
            caCertData = caCertDataJson.asText();
        }
        if (clientCertDataJson != null) {
            clientCertData = clientCertDataJson.asText();
        }
        if (clientKeyDataJson != null) {
            clientKeyData = clientKeyDataJson.asText();
        }
    }
    if (StringUtils.isNotEmpty(token)) {
        builder.token(token);
    }
    if (StringUtils.isNotEmpty(caCertData)) {
        builder.caCertData(caCertData);
    }
    if (StringUtils.isNotEmpty(clientCertData)) {
        builder.clientCertData(clientCertData);
    }
    if (StringUtils.isNotEmpty(clientKeyData)) {
        builder.clientKeyData(clientKeyData);
    }
    JsonNode extNetworkCidrJson = json.get(EXT_NETWORK_CIDR);
    if (extNetworkCidrJson != null) {
        builder.extNetworkCidr(IpPrefix.valueOf(extNetworkCidrJson.asText()));
    }
    Set<HostNodesInfo> infos = new HashSet<>();
    ArrayNode infosJson = (ArrayNode) json.get(HOST_NODES_INFO);
    if (infosJson != null) {
        for (JsonNode infoJson : infosJson) {
            HostNodesInfo info = context.codec(HostNodesInfo.class).decode((ObjectNode) infoJson, context);
            infos.add(info);
        }
        builder.infos(infos);
    }
    return builder.build();
}
Also used : K8sApiConfig(org.onosproject.k8snode.api.K8sApiConfig) DefaultK8sApiConfig(org.onosproject.k8snode.api.DefaultK8sApiConfig) Scheme(org.onosproject.k8snode.api.K8sApiConfig.Scheme) Mode(org.onosproject.k8snode.api.K8sApiConfig.Mode) HostNodesInfo(org.onosproject.k8snode.api.HostNodesInfo) JsonNode(com.fasterxml.jackson.databind.JsonNode) IpAddress(org.onlab.packet.IpAddress) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HashSet(java.util.HashSet)

Example 5 with K8sApiConfig

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

the class K8sApiConfigCodecTest method getK8sApiConfig.

private K8sApiConfig getK8sApiConfig(String resourceName) throws IOException {
    InputStream jsonStream = K8sNodeCodecTest.class.getResourceAsStream(resourceName);
    JsonNode json = context.mapper().readTree(jsonStream);
    assertThat(json, notNullValue());
    K8sApiConfig config = k8sApiConfigCodec.decode((ObjectNode) json, context);
    assertThat(config, notNullValue());
    return config;
}
Also used : K8sApiConfigJsonMatcher.matchesK8sApiConfig(org.onosproject.k8snode.codec.K8sApiConfigJsonMatcher.matchesK8sApiConfig) K8sApiConfig(org.onosproject.k8snode.api.K8sApiConfig) DefaultK8sApiConfig(org.onosproject.k8snode.api.DefaultK8sApiConfig) InputStream(java.io.InputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

K8sApiConfig (org.onosproject.k8snode.api.K8sApiConfig)14 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 DefaultK8sApiConfig (org.onosproject.k8snode.api.DefaultK8sApiConfig)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)3 Consumes (javax.ws.rs.Consumes)3 HostNodesInfo (org.onosproject.k8snode.api.HostNodesInfo)3 K8sApiConfigJsonMatcher.matchesK8sApiConfig (org.onosproject.k8snode.codec.K8sApiConfigJsonMatcher.matchesK8sApiConfig)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Test (org.junit.Test)2 DefaultHostNodesInfo (org.onosproject.k8snode.api.DefaultHostNodesInfo)2 K8sApiConfigService (org.onosproject.k8snode.api.K8sApiConfigService)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 INDENT_OUTPUT (com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT)1 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 DELETE (javax.ws.rs.DELETE)1