Search in sources :

Example 6 with K8sApiConfig

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

the class K8sNetworkingUtil method k8sClient.

/**
 * Obtains workable kubernetes client.
 *
 * @param service kubernetes API service
 * @return kubernetes client
 */
public static KubernetesClient k8sClient(K8sApiConfigService service) {
    K8sApiConfig config = service.apiConfigs().stream().findAny().orElse(null);
    if (config == null) {
        log.error("Failed to find valid kubernetes API configuration.");
        return null;
    }
    KubernetesClient client = k8sClient(config);
    if (client == null) {
        log.error("Failed to connect to kubernetes API server.");
        return null;
    }
    return client;
}
Also used : K8sApiConfig(org.onosproject.k8snode.api.K8sApiConfig) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient)

Example 7 with K8sApiConfig

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

the class K8sSyncStateCommand method doExecute.

@Override
protected void doExecute() {
    K8sApiConfigService configService = get(K8sApiConfigService.class);
    K8sPodAdminService podAdminService = get(K8sPodAdminService.class);
    K8sNamespaceAdminService namespaceAdminService = get(K8sNamespaceAdminService.class);
    K8sServiceAdminService serviceAdminService = get(K8sServiceAdminService.class);
    K8sIngressAdminService ingressAdminService = get(K8sIngressAdminService.class);
    K8sEndpointsAdminService endpointsAdminService = get(K8sEndpointsAdminService.class);
    K8sNetworkAdminService networkAdminService = get(K8sNetworkAdminService.class);
    K8sNetworkPolicyAdminService networkPolicyAdminService = get(K8sNetworkPolicyAdminService.class);
    K8sApiConfig config = configService.apiConfigs().stream().findAny().orElse(null);
    if (config == null) {
        log.error("Failed to find valid kubernetes API configuration.");
        return;
    }
    KubernetesClient client = K8sNetworkingUtil.k8sClient(config);
    if (client == null) {
        log.error("Failed to connect to kubernetes API server.");
        return;
    }
    print("\nSynchronizing kubernetes namespaces");
    print(NAMESPACE_FORMAT, "Name", "Phase", "Labels");
    client.namespaces().list().getItems().forEach(ns -> {
        if (namespaceAdminService.namespace(ns.getMetadata().getUid()) != null) {
            namespaceAdminService.updateNamespace(ns);
        } else {
            namespaceAdminService.createNamespace(ns);
        }
        printNamespace(ns);
    });
    print("Synchronizing kubernetes services");
    print(SERVICE_FORMAT, "Name", "Cluster IP", "Ports");
    client.services().inAnyNamespace().list().getItems().forEach(svc -> {
        if (serviceAdminService.service(svc.getMetadata().getUid()) != null) {
            serviceAdminService.updateService(svc);
        } else {
            serviceAdminService.createService(svc);
        }
        printService(svc);
    });
    print("\nSynchronizing kubernetes endpoints");
    print(ENDPOINTS_FORMAT, "Name", "IP Addresses", "Ports");
    client.endpoints().inAnyNamespace().list().getItems().forEach(ep -> {
        if (endpointsAdminService.endpoints(ep.getMetadata().getUid()) != null) {
            endpointsAdminService.updateEndpoints(ep);
        } else {
            endpointsAdminService.createEndpoints(ep);
        }
        printEndpoints(ep);
    });
    print("\nSynchronizing kubernetes pods");
    print(POD_FORMAT, "Name", "Namespace", "IP", "Containers");
    client.pods().inAnyNamespace().list().getItems().forEach(pod -> {
        if (podAdminService.pod(pod.getMetadata().getUid()) != null) {
            podAdminService.updatePod(pod);
        } else {
            podAdminService.createPod(pod);
        }
        syncPortFromPod(pod, networkAdminService);
        printPod(pod);
    });
    print("\nSynchronizing kubernetes ingresses");
    print(INGRESS_FORMAT, "Name", "Namespace", "LB Addresses");
    client.extensions().ingresses().inAnyNamespace().list().getItems().forEach(ingress -> {
        if (ingressAdminService.ingress(ingress.getMetadata().getUid()) != null) {
            ingressAdminService.updateIngress(ingress);
        } else {
            ingressAdminService.createIngress(ingress);
        }
        printIngresses(ingress);
    });
    print("\nSynchronizing kubernetes network policies");
    print(NETWORK_POLICY_FORMAT, "Name", "Namespace", "Types");
    client.network().networkPolicies().inAnyNamespace().list().getItems().forEach(policy -> {
        if (networkPolicyAdminService.networkPolicy(policy.getMetadata().getUid()) != null) {
            networkPolicyAdminService.updateNetworkPolicy(policy);
        } else {
            networkPolicyAdminService.createNetworkPolicy(policy);
        }
        printNetworkPolicy(policy);
    });
}
Also used : K8sApiConfig(org.onosproject.k8snode.api.K8sApiConfig) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) K8sEndpointsAdminService(org.onosproject.k8snetworking.api.K8sEndpointsAdminService) K8sServiceAdminService(org.onosproject.k8snetworking.api.K8sServiceAdminService) K8sIngressAdminService(org.onosproject.k8snetworking.api.K8sIngressAdminService) K8sNetworkPolicyAdminService(org.onosproject.k8snetworking.api.K8sNetworkPolicyAdminService) K8sNetworkAdminService(org.onosproject.k8snetworking.api.K8sNetworkAdminService) K8sApiConfigService(org.onosproject.k8snode.api.K8sApiConfigService) K8sNamespaceAdminService(org.onosproject.k8snetworking.api.K8sNamespaceAdminService) K8sPodAdminService(org.onosproject.k8snetworking.api.K8sPodAdminService)

Example 8 with K8sApiConfig

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

the class K8sApiConfigListCommand method doExecute.

@Override
protected void doExecute() {
    K8sApiConfigService configService = get(K8sApiConfigService.class);
    List<K8sApiConfig> configs = Lists.newArrayList(configService.apiConfigs());
    configs.sort(Comparator.comparing(K8sApiConfig::ipAddress));
    if (outputJson()) {
        print("%s", json(configs));
    } else {
        print(FORMAT, "Scheme", "IpAddress", "Port", "State");
        for (K8sApiConfig config : configs) {
            print(FORMAT, config.scheme().name(), config.ipAddress().toString(), config.port(), config.state().name());
        }
        print("Total %s API configs", configService.apiConfigs().size());
    }
}
Also used : K8sApiConfig(org.onosproject.k8snode.api.K8sApiConfig) K8sApiConfigService(org.onosproject.k8snode.api.K8sApiConfigService)

Example 9 with K8sApiConfig

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

the class K8sApiConfigListCommand method json.

private String json(List<K8sApiConfig> configs) {
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode result = mapper.createArrayNode();
    for (K8sApiConfig config : configs) {
        result.add(jsonForEntity(config, K8sApiConfig.class));
    }
    return prettyJson(mapper, result.toString());
}
Also used : K8sApiConfig(org.onosproject.k8snode.api.K8sApiConfig) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 10 with K8sApiConfig

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

the class K8sNodeWebResource method createApiConfigs.

/**
 * Creates a set of kubernetes API config from the JSON input stream.
 *
 * @param input kubernetes API configs JSON input stream
 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
 * is malformed
 * @onos.rsModel K8sApiConfig
 */
@POST
@Path("api")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createApiConfigs(InputStream input) {
    log.trace(String.format(MESSAGE_NODE, CREATE));
    readApiConfigConfiguration(input).forEach(config -> {
        K8sApiConfig existing = configAdminService.apiConfig(endpoint(config));
        if (existing == null) {
            configAdminService.createApiConfig(config);
        }
    });
    UriBuilder locationBuilder = uriInfo.getBaseUriBuilder().path(API_CONFIGS);
    return created(locationBuilder.build()).build();
}
Also used : K8sApiConfig(org.onosproject.k8snode.api.K8sApiConfig) 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)

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