blob: 7845df12f631229e710af9be665d5efebf7cb712 [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
Jian Lica34c672019-02-26 14:04:08 +090018import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Lib1cd0b02019-02-21 16:41:40 +090021import com.google.common.collect.Lists;
22import io.fabric8.kubernetes.api.model.Pod;
Jian Lica34c672019-02-26 14:04:08 +090023import io.fabric8.kubernetes.client.utils.Serialization;
Jian Lib1cd0b02019-02-21 16:41:40 +090024import org.apache.karaf.shell.api.action.Command;
25import org.apache.karaf.shell.api.action.lifecycle.Service;
26import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.k8snetworking.api.K8sPodService;
28
Jian Lica34c672019-02-26 14:04:08 +090029import java.io.IOException;
Jian Lib1cd0b02019-02-21 16:41:40 +090030import java.util.Comparator;
31import java.util.List;
32
Jian Lica34c672019-02-26 14:04:08 +090033import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
34
Jian Lib1cd0b02019-02-21 16:41:40 +090035/**
36 * Lists kubernetes pods.
37 */
38@Service
39@Command(scope = "onos", name = "k8s-pods",
40 description = "Lists all kubernetes pods")
41public class K8sPodListCommand extends AbstractShellCommand {
42
43 private static final String FORMAT = "%-50s%-15s%-15s%-30s";
44
45 @Override
46 protected void doExecute() {
47 K8sPodService service = get(K8sPodService.class);
48 List<Pod> pods = Lists.newArrayList(service.pods());
49 pods.sort(Comparator.comparing(p -> p.getMetadata().getName()));
50
Jian Lica34c672019-02-26 14:04:08 +090051 if (outputJson()) {
52 print("%s", json(pods));
53 } else {
54 print(FORMAT, "Name", "Namespace", "IP Address", "Containers");
Jian Lib1cd0b02019-02-21 16:41:40 +090055
Jian Lica34c672019-02-26 14:04:08 +090056 for (Pod pod : pods) {
Jian Lib1cd0b02019-02-21 16:41:40 +090057
Jian Lica34c672019-02-26 14:04:08 +090058 List<String> containers = Lists.newArrayList();
Jian Lib1cd0b02019-02-21 16:41:40 +090059
Jian Lica34c672019-02-26 14:04:08 +090060 pod.getSpec().getContainers().forEach(c -> containers.add(c.getName()));
Jian Lib1cd0b02019-02-21 16:41:40 +090061
Jian Lica34c672019-02-26 14:04:08 +090062 print(FORMAT,
63 pod.getMetadata().getName(),
64 pod.getMetadata().getNamespace(),
65 pod.getStatus().getPodIP(),
66 containers.isEmpty() ? "" : containers);
67 }
68 }
69 }
70
71 private String json(List<Pod> pods) {
72 ObjectMapper mapper = new ObjectMapper();
73 ArrayNode result = mapper.createArrayNode();
74 try {
75 for (Pod pod : pods) {
76 ObjectNode json = (ObjectNode) new ObjectMapper()
77 .readTree(Serialization.asJson(pod));
78 result.add(json);
79 }
80 return prettyJson(mapper, result.toString());
81 } catch (IOException e) {
82 log.warn("Failed to parse POD's JSON string.");
83 return "";
Jian Lib1cd0b02019-02-21 16:41:40 +090084 }
85 }
86}