blob: a1443979a8bc439913b07cec028658864d6ea043 [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.Pod;
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.K8sPodService;
24
25import java.util.Comparator;
26import java.util.List;
27
28/**
29 * Lists kubernetes pods.
30 */
31@Service
32@Command(scope = "onos", name = "k8s-pods",
33 description = "Lists all kubernetes pods")
34public class K8sPodListCommand extends AbstractShellCommand {
35
36 private static final String FORMAT = "%-50s%-15s%-15s%-30s";
37
38 @Override
39 protected void doExecute() {
40 K8sPodService service = get(K8sPodService.class);
41 List<Pod> pods = Lists.newArrayList(service.pods());
42 pods.sort(Comparator.comparing(p -> p.getMetadata().getName()));
43
Jian Li2cc2b632019-02-18 00:56:40 +090044 print(FORMAT, "Name", "Namespace", "IP Address", "Containers");
Jian Lib1cd0b02019-02-21 16:41:40 +090045
46 for (Pod pod : pods) {
47
48 List<String> containers = Lists.newArrayList();
49
50 pod.getSpec().getContainers().forEach(c -> containers.add(c.getName()));
51
52 print(FORMAT,
53 pod.getMetadata().getName(),
54 pod.getMetadata().getNamespace(),
55 pod.getStatus().getPodIP(),
56 containers.isEmpty() ? "" : containers);
57 }
58 }
59}