Search in sources :

Example 1 with K8sEndpointsService

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