Search in sources :

Example 1 with K8sApiConfigService

use of org.onosproject.k8snode.api.K8sApiConfigService 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 2 with K8sApiConfigService

use of org.onosproject.k8snode.api.K8sApiConfigService 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)

Aggregations

K8sApiConfig (org.onosproject.k8snode.api.K8sApiConfig)2 K8sApiConfigService (org.onosproject.k8snode.api.K8sApiConfigService)2 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 K8sEndpointsAdminService (org.onosproject.k8snetworking.api.K8sEndpointsAdminService)1 K8sIngressAdminService (org.onosproject.k8snetworking.api.K8sIngressAdminService)1 K8sNamespaceAdminService (org.onosproject.k8snetworking.api.K8sNamespaceAdminService)1 K8sNetworkAdminService (org.onosproject.k8snetworking.api.K8sNetworkAdminService)1 K8sNetworkPolicyAdminService (org.onosproject.k8snetworking.api.K8sNetworkPolicyAdminService)1 K8sPodAdminService (org.onosproject.k8snetworking.api.K8sPodAdminService)1 K8sServiceAdminService (org.onosproject.k8snetworking.api.K8sServiceAdminService)1