blob: 018cfb2b99626a3c80ea598ae991f3a9927bccdc [file] [log] [blame]
Jian Lib1cd0b02019-02-21 16:41:40 +09001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.k8snetworking.cli;
17
18import com.google.common.collect.Lists;
19import io.fabric8.kubernetes.api.model.Endpoints;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.k8snetworking.api.K8sEndpointsService;
24
25import java.util.Comparator;
26import java.util.List;
27
28/**
29 * Lists kubernetes endpoints.
30 */
31@Service
32@Command(scope = "onos", name = "k8s-endpoints",
33 description = "Lists all kubernetes endpoints")
34public class K8sEndpointsListCommand extends AbstractShellCommand {
35
36 private static final String FORMAT = "%-50s%-50s%-20s";
Jian Li2cc2b632019-02-18 00:56:40 +090037 private static final String PORT_PROTOCOL_SEPARATOR = "/";
Jian Lib1cd0b02019-02-21 16:41:40 +090038
39 @Override
40 protected void doExecute() {
41 K8sEndpointsService service = get(K8sEndpointsService.class);
42 List<Endpoints> endpointses = Lists.newArrayList(service.endpointses());
43 endpointses.sort(Comparator.comparing(e -> e.getMetadata().getName()));
44
45 print(FORMAT, "Name", "IP Addresses", "Ports");
46
47 for (Endpoints endpoints : endpointses) {
48
49 List<String> ips = Lists.newArrayList();
Jian Li2cc2b632019-02-18 00:56:40 +090050 List<String> portWithProtocol = Lists.newArrayList();
Jian Lib1cd0b02019-02-21 16:41:40 +090051
52 endpoints.getSubsets().forEach(e -> {
53 e.getAddresses().forEach(a -> ips.add(a.getIp()));
Jian Li2cc2b632019-02-18 00:56:40 +090054 e.getPorts().forEach(p -> portWithProtocol.add(p.getPort() +
55 PORT_PROTOCOL_SEPARATOR + p.getProtocol()));
Jian Lib1cd0b02019-02-21 16:41:40 +090056 });
57
58 print(FORMAT,
59 endpoints.getMetadata().getName(),
60 ips.isEmpty() ? "" : ips,
Jian Li2cc2b632019-02-18 00:56:40 +090061 portWithProtocol.isEmpty() ? "" : portWithProtocol);
Jian Lib1cd0b02019-02-21 16:41:40 +090062 }
63 }
64}