blob: 2ad937a73c64c5e35720564fab37365d01f7c86e [file] [log] [blame]
Jian Li105770e2021-01-17 02:18:30 +09001/*
2 * Copyright 2021-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.kubevirtnetworking.cli;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableList;
22import com.google.common.collect.Lists;
23import io.fabric8.kubernetes.api.model.Pod;
24import io.fabric8.kubernetes.client.utils.Serialization;
25import org.apache.commons.lang.StringUtils;
26import org.apache.karaf.shell.api.action.Command;
27import org.apache.karaf.shell.api.action.lifecycle.Service;
28import org.onosproject.cli.AbstractShellCommand;
29import org.onosproject.kubevirtnetworking.api.KubevirtPodService;
30
31import java.io.IOException;
32import java.util.Comparator;
33import java.util.List;
34
35import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
36import static org.onosproject.kubevirtnetworking.api.Constants.CLI_MARGIN_LENGTH;
37import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAMESPACE_LENGTH;
38import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAME_LENGTH;
39import static org.onosproject.kubevirtnetworking.api.Constants.CLI_STATUS_LENGTH;
40import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString;
41import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson;
42
43/**
44 * Lists kubevirt pods.
45 */
46@Service
47@Command(scope = "onos", name = "kubevirt-pods",
48 description = "Lists all kubevirt pods")
49public class KubevirtListPodCommand extends AbstractShellCommand {
50 @Override
51 protected void doExecute() throws Exception {
52 KubevirtPodService service = get(KubevirtPodService.class);
53 List<Pod> pods = Lists.newArrayList(service.pods());
54 pods.sort(Comparator.comparing(p -> p.getMetadata().getName()));
55
56 String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
57 CLI_NAMESPACE_LENGTH, CLI_STATUS_LENGTH, CLI_IP_ADDRESS_LENGTH));
58
59 if (outputJson()) {
60 print("%s", json(pods));
61 } else {
62 print(format, "Name", "Namespace", "Status", "IP Address");
63
64 for (Pod pod : pods) {
65
66 List<String> containers = Lists.newArrayList();
67
68 pod.getSpec().getContainers().forEach(c -> containers.add(c.getName()));
69
70 print(format,
71 StringUtils.substring(pod.getMetadata().getName(),
72 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
73 StringUtils.substring(pod.getMetadata().getNamespace(),
74 0, CLI_NAMESPACE_LENGTH - CLI_MARGIN_LENGTH),
75 pod.getStatus().getPhase(),
76 StringUtils.substring(pod.getStatus().getPodIP(),
77 0, CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH));
78 }
79 }
80 }
81
82 private String json(List<Pod> pods) {
83 ObjectMapper mapper = new ObjectMapper();
84 ArrayNode result = mapper.createArrayNode();
85 try {
86 for (Pod pod : pods) {
87 ObjectNode json = (ObjectNode) new ObjectMapper()
88 .readTree(Serialization.asJson(pod));
89 result.add(json);
90 }
91 return prettyJson(mapper, result.toString());
92 } catch (IOException e) {
93 log.warn("Failed to parse POD's JSON string.");
94 return "";
95 }
96 }
97}