Search in sources :

Example 1 with CLI_NAME_LENGTH

use of org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH in project onos by opennetworkinglab.

the class K8sNamespaceListCommand method doExecute.

@Override
protected void doExecute() {
    K8sNamespaceService service = get(K8sNamespaceService.class);
    List<Namespace> namespaces = Lists.newArrayList(service.namespaces());
    namespaces.sort(Comparator.comparing(n -> n.getMetadata().getName()));
    String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, CLI_PHASE_LENGTH, CLI_LABELS_LENGTH));
    if (outputJson()) {
        print("%s", json(namespaces));
    } else {
        print(format, "Name", "Phase", "Labels");
        for (Namespace namespace : namespaces) {
            print(format, StringUtils.substring(namespace.getMetadata().getName(), 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), namespace.getStatus().getPhase(), namespace.getMetadata() != null && namespace.getMetadata().getLabels() != null && !namespace.getMetadata().getLabels().isEmpty() ? StringUtils.substring(namespace.getMetadata().getLabels().toString(), 0, CLI_LABELS_LENGTH - CLI_MARGIN_LENGTH) : "");
        }
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) K8sNamespaceService(org.onosproject.k8snetworking.api.K8sNamespaceService) Command(org.apache.karaf.shell.api.action.Command) CLI_NAME_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH) K8sNetworkingUtil.genFormatString(org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) List(java.util.List) CLI_PHASE_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_PHASE_LENGTH) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Namespace(io.fabric8.kubernetes.api.model.Namespace) Serialization(io.fabric8.kubernetes.client.utils.Serialization) K8sNetworkingUtil.prettyJson(org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson) CLI_LABELS_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_LABELS_LENGTH) CLI_MARGIN_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH) Comparator(java.util.Comparator) K8sNamespaceService(org.onosproject.k8snetworking.api.K8sNamespaceService) K8sNetworkingUtil.genFormatString(org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString) Namespace(io.fabric8.kubernetes.api.model.Namespace)

Example 2 with CLI_NAME_LENGTH

use of org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH in project onos by opennetworkinglab.

the class K8sNetworkPolicyListCommand method doExecute.

@Override
protected void doExecute() {
    K8sNetworkPolicyService service = get(K8sNetworkPolicyService.class);
    List<NetworkPolicy> policies = Lists.newArrayList(service.networkPolicies());
    policies.sort(Comparator.comparing(p -> p.getMetadata().getName()));
    String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, CLI_NAMESPACE_LENGTH, CLI_TYPES_LENGTH));
    if (outputJson()) {
        print("%s", json(policies));
    } else {
        print(format, "Name", "Namespace", "Types");
        for (NetworkPolicy policy : policies) {
            print(format, StringUtils.substring(policy.getMetadata().getName(), 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(policy.getMetadata().getNamespace(), 0, CLI_NAMESPACE_LENGTH - CLI_MARGIN_LENGTH), policy.getSpec().getPolicyTypes().isEmpty() ? "" : policy.getSpec().getPolicyTypes());
        }
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) CLI_TYPES_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_TYPES_LENGTH) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Command(org.apache.karaf.shell.api.action.Command) CLI_NAME_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH) K8sNetworkingUtil.genFormatString(org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) CLI_NAMESPACE_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_NAMESPACE_LENGTH) List(java.util.List) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Serialization(io.fabric8.kubernetes.client.utils.Serialization) K8sNetworkPolicyService(org.onosproject.k8snetworking.api.K8sNetworkPolicyService) K8sNetworkingUtil.prettyJson(org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson) Service(org.apache.karaf.shell.api.action.lifecycle.Service) NetworkPolicy(io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicy) CLI_MARGIN_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH) Comparator(java.util.Comparator) NetworkPolicy(io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicy) K8sNetworkingUtil.genFormatString(org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString) K8sNetworkPolicyService(org.onosproject.k8snetworking.api.K8sNetworkPolicyService)

Example 3 with CLI_NAME_LENGTH

use of org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH in project onos by opennetworkinglab.

the class K8sPodListCommand method doExecute.

@Override
protected void doExecute() {
    K8sPodService service = get(K8sPodService.class);
    List<Pod> pods = Lists.newArrayList(service.pods());
    pods.sort(Comparator.comparing(p -> p.getMetadata().getName()));
    String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, CLI_NAMESPACE_LENGTH, CLI_IP_ADDRESS_LENGTH, CLI_CONTAINERS_LENGTH));
    if (outputJson()) {
        print("%s", json(pods));
    } else {
        print(format, "Name", "Namespace", "IP Address", "Containers");
        for (Pod pod : pods) {
            List<String> containers = Lists.newArrayList();
            pod.getSpec().getContainers().forEach(c -> containers.add(c.getName()));
            print(format, StringUtils.substring(pod.getMetadata().getName(), 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(pod.getMetadata().getNamespace(), 0, CLI_NAMESPACE_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(pod.getStatus().getPodIP(), 0, CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH), containers.isEmpty() ? "" : containers);
        }
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Pod(io.fabric8.kubernetes.api.model.Pod) CLI_CONTAINERS_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_CONTAINERS_LENGTH) IOException(java.io.IOException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Command(org.apache.karaf.shell.api.action.Command) CLI_NAME_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH) K8sNetworkingUtil.genFormatString(org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) CLI_NAMESPACE_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_NAMESPACE_LENGTH) List(java.util.List) CLI_IP_ADDRESS_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESS_LENGTH) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Serialization(io.fabric8.kubernetes.client.utils.Serialization) K8sNetworkingUtil.prettyJson(org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson) Service(org.apache.karaf.shell.api.action.lifecycle.Service) K8sPodService(org.onosproject.k8snetworking.api.K8sPodService) CLI_MARGIN_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH) Comparator(java.util.Comparator) Pod(io.fabric8.kubernetes.api.model.Pod) K8sPodService(org.onosproject.k8snetworking.api.K8sPodService) K8sNetworkingUtil.genFormatString(org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString)

Example 4 with CLI_NAME_LENGTH

use of org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH in project onos by opennetworkinglab.

the class K8sServiceListCommand method doExecute.

@Override
protected void doExecute() {
    K8sServiceService service = get(K8sServiceService.class);
    List<io.fabric8.kubernetes.api.model.Service> services = Lists.newArrayList(service.services());
    services.sort(Comparator.comparing(s -> s.getMetadata().getName()));
    String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, CLI_IP_ADDRESS_LENGTH, CLI_PORTS_LENGTH));
    if (outputJson()) {
        print("%s", json(services));
    } else {
        print(format, "Name", "Cluster IP", "Ports");
        for (io.fabric8.kubernetes.api.model.Service svc : services) {
            List<String> portWithProtocol = Lists.newArrayList();
            svc.getSpec().getPorts().forEach(p -> portWithProtocol.add(p.getPort() + PORT_PROTOCOL_SEPARATOR + p.getProtocol()));
            print(format, StringUtils.substring(svc.getMetadata().getName(), 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(svc.getSpec().getClusterIP(), 0, CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH), portWithProtocol.isEmpty() ? "" : portWithProtocol);
        }
    }
}
Also used : K8sServiceService(org.onosproject.k8snetworking.api.K8sServiceService) StringUtils(org.apache.commons.lang.StringUtils) K8sServiceService(org.onosproject.k8snetworking.api.K8sServiceService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) CLI_PORTS_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_PORTS_LENGTH) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Command(org.apache.karaf.shell.api.action.Command) CLI_NAME_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH) K8sNetworkingUtil.genFormatString(org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) List(java.util.List) CLI_IP_ADDRESS_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESS_LENGTH) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Serialization(io.fabric8.kubernetes.client.utils.Serialization) K8sNetworkingUtil.prettyJson(org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson) Service(org.apache.karaf.shell.api.action.lifecycle.Service) CLI_MARGIN_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH) Comparator(java.util.Comparator) K8sServiceService(org.onosproject.k8snetworking.api.K8sServiceService) Service(org.apache.karaf.shell.api.action.lifecycle.Service) K8sNetworkingUtil.genFormatString(org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString)

Example 5 with CLI_NAME_LENGTH

use of org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH in project onos by opennetworkinglab.

the class K8sEndpointsListCommand method doExecute.

@Override
protected void doExecute() {
    K8sEndpointsService service = get(K8sEndpointsService.class);
    List<Endpoints> endpointses = Lists.newArrayList(service.endpointses());
    endpointses.sort(Comparator.comparing(e -> e.getMetadata().getName()));
    String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, CLI_IP_ADDRESSES_LENGTH, CLI_PORTS_LENGTH));
    if (outputJson()) {
        print("%s", json(endpointses));
    } else {
        print(format, "Name", "IP Addresses", "Ports");
        for (Endpoints endpoints : endpointses) {
            List<String> ips = Lists.newArrayList();
            List<String> portWithProtocol = Lists.newArrayList();
            endpoints.getSubsets().forEach(e -> {
                e.getAddresses().forEach(a -> ips.add(a.getIp()));
                e.getPorts().forEach(p -> portWithProtocol.add(p.getPort() + PORT_PROTOCOL_SEPARATOR + p.getProtocol()));
            });
            print(format, StringUtils.substring(endpoints.getMetadata().getName(), 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), ips.isEmpty() ? "" : StringUtils.substring(ips.toString(), 0, CLI_IP_ADDRESSES_LENGTH - CLI_MARGIN_LENGTH), portWithProtocol.isEmpty() ? "" : portWithProtocol);
        }
    }
}
Also used : Endpoints(io.fabric8.kubernetes.api.model.Endpoints) Endpoints(io.fabric8.kubernetes.api.model.Endpoints) StringUtils(org.apache.commons.lang.StringUtils) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) CLI_IP_ADDRESSES_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESSES_LENGTH) CLI_PORTS_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_PORTS_LENGTH) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Command(org.apache.karaf.shell.api.action.Command) CLI_NAME_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH) K8sNetworkingUtil.genFormatString(org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) List(java.util.List) Lists(com.google.common.collect.Lists) K8sEndpointsService(org.onosproject.k8snetworking.api.K8sEndpointsService) ImmutableList(com.google.common.collect.ImmutableList) Serialization(io.fabric8.kubernetes.client.utils.Serialization) K8sNetworkingUtil.prettyJson(org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson) Service(org.apache.karaf.shell.api.action.lifecycle.Service) CLI_MARGIN_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH) Comparator(java.util.Comparator) K8sEndpointsService(org.onosproject.k8snetworking.api.K8sEndpointsService) K8sNetworkingUtil.genFormatString(org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)6 ImmutableList (com.google.common.collect.ImmutableList)6 Lists (com.google.common.collect.Lists)6 Serialization (io.fabric8.kubernetes.client.utils.Serialization)6 IOException (java.io.IOException)6 Comparator (java.util.Comparator)6 List (java.util.List)6 StringUtils (org.apache.commons.lang.StringUtils)6 Command (org.apache.karaf.shell.api.action.Command)6 AbstractShellCommand (org.onosproject.cli.AbstractShellCommand)6 CLI_MARGIN_LENGTH (org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH)6 CLI_NAME_LENGTH (org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH)6 K8sNetworkingUtil.genFormatString (org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString)6 K8sNetworkingUtil.prettyJson (org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson)6 Service (org.apache.karaf.shell.api.action.lifecycle.Service)5 CLI_IP_ADDRESS_LENGTH (org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESS_LENGTH)3 CLI_NAMESPACE_LENGTH (org.onosproject.k8snetworking.api.Constants.CLI_NAMESPACE_LENGTH)3 CLI_PORTS_LENGTH (org.onosproject.k8snetworking.api.Constants.CLI_PORTS_LENGTH)2