Search in sources :

Example 1 with CLI_PORTS_LENGTH

use of org.onosproject.k8snetworking.api.Constants.CLI_PORTS_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 2 with CLI_PORTS_LENGTH

use of org.onosproject.k8snetworking.api.Constants.CLI_PORTS_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)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 ImmutableList (com.google.common.collect.ImmutableList)2 Lists (com.google.common.collect.Lists)2 Serialization (io.fabric8.kubernetes.client.utils.Serialization)2 IOException (java.io.IOException)2 Comparator (java.util.Comparator)2 List (java.util.List)2 StringUtils (org.apache.commons.lang.StringUtils)2 Command (org.apache.karaf.shell.api.action.Command)2 Service (org.apache.karaf.shell.api.action.lifecycle.Service)2 AbstractShellCommand (org.onosproject.cli.AbstractShellCommand)2 CLI_MARGIN_LENGTH (org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH)2 CLI_NAME_LENGTH (org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH)2 CLI_PORTS_LENGTH (org.onosproject.k8snetworking.api.Constants.CLI_PORTS_LENGTH)2 K8sNetworkingUtil.genFormatString (org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString)2 K8sNetworkingUtil.prettyJson (org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson)2 Endpoints (io.fabric8.kubernetes.api.model.Endpoints)1 CLI_IP_ADDRESSES_LENGTH (org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESSES_LENGTH)1