blob: 6b4c997fa94a94aacaca9557d01042dd5f9df22c [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";
37
38 @Override
39 protected void doExecute() {
40 K8sEndpointsService service = get(K8sEndpointsService.class);
41 List<Endpoints> endpointses = Lists.newArrayList(service.endpointses());
42 endpointses.sort(Comparator.comparing(e -> e.getMetadata().getName()));
43
44 print(FORMAT, "Name", "IP Addresses", "Ports");
45
46 for (Endpoints endpoints : endpointses) {
47
48 List<String> ips = Lists.newArrayList();
49 List<Integer> ports = Lists.newArrayList();
50
51 endpoints.getSubsets().forEach(e -> {
52 e.getAddresses().forEach(a -> ips.add(a.getIp()));
53 e.getPorts().forEach(p -> ports.add(p.getPort()));
54 });
55
56 print(FORMAT,
57 endpoints.getMetadata().getName(),
58 ips.isEmpty() ? "" : ips,
59 ports.isEmpty() ? "" : ports);
60 }
61 }
62}