blob: c8bf0a9f2cc95c4fcadc51a1a9951d88fba333b0 [file] [log] [blame]
Jian Li534f3f22021-02-22 21:48:32 +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.ObjectNode;
20import io.fabric8.kubernetes.api.model.Container;
21import io.fabric8.kubernetes.api.model.Pod;
22import io.fabric8.kubernetes.client.utils.Serialization;
23import org.apache.karaf.shell.api.action.Command;
24import org.apache.karaf.shell.api.action.Completion;
25import org.apache.karaf.shell.api.action.Option;
26import org.apache.karaf.shell.api.action.lifecycle.Service;
27import org.onosproject.cli.AbstractShellCommand;
28import org.onosproject.kubevirtnetworking.api.KubevirtPodService;
29
30import java.io.IOException;
31import java.util.List;
32
33import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson;
34
35/**
36 * Show a detailed POD info.
37 */
38@Service
39@Command(scope = "onos", name = "kubevirt-pod",
40 description = "Displays a POD details")
41public class KubevirtShowPodCommand extends AbstractShellCommand {
42
43 @Option(name = "--name",
44 description = "Filter POD by specific name", multiValued = true)
45 @Completion(KubevirtPodCompleter.class)
46 private List<String> names;
47
48 @Override
49 protected void doExecute() throws Exception {
50 KubevirtPodService service = get(KubevirtPodService.class);
51
Jian Li8c553eb2021-03-04 18:48:39 +090052 if (names == null || names.size() == 0) {
53 print("Need to specify at least one POD name using --name option.");
54 return;
55 }
56
Jian Li534f3f22021-02-22 21:48:32 +090057 for (String name : names) {
58 Pod pod = service.pods().stream().filter(p -> p.getMetadata().getName().equals(name))
59 .findAny().orElse(null);
60 if (pod == null) {
61 print("Unable to find %s", name);
62 continue;
63 }
64
65 if (outputJson()) {
66 print("%s", json(pod));
67 } else {
68 printPod(pod);
69 }
70 }
71 }
72
73 private void printPod(Pod pod) {
74 print("Name: %s", pod.getMetadata().getName());
75 print(" Status: %s", pod.getStatus().getPhase());
76 print(" Node Name: %s", pod.getSpec().getNodeName());
77 print(" Namespace: %s", pod.getMetadata().getNamespace());
78 print(" IP address: %s", pod.getStatus().getPodIP());
79
80 int counter = 1;
81 for (Container container : pod.getSpec().getContainers()) {
82 print(" Container #%d:", counter);
83 print(" Name: %s", container.getName());
84 print(" Image: %s", container.getImage());
85 print(" Pull Policy: %s", container.getImagePullPolicy());
86 print(" Commands: %s", container.getCommand());
87 print(" Args: %s", container.getArgs());
Jian Li1c10cf22021-03-05 01:32:04 +090088 counter++;
Jian Li534f3f22021-02-22 21:48:32 +090089 }
90 }
91
92 private String json(Pod pod) {
93 try {
94 ObjectNode result = (ObjectNode)
95 new ObjectMapper().readTree(Serialization.asJson(pod));
96 return prettyJson(new ObjectMapper(), result.toString());
97 } catch (IOException e) {
98 log.warn("Failed to parse POD's JSON string.");
99 return "";
100 }
101 }
102}