blob: 5f7bc4cd78ce62485125cc0eb2d7391a81848380 [file] [log] [blame]
Jian Lic94b7ee2021-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
52 for (String name : names) {
53 Pod pod = service.pods().stream().filter(p -> p.getMetadata().getName().equals(name))
54 .findAny().orElse(null);
55 if (pod == null) {
56 print("Unable to find %s", name);
57 continue;
58 }
59
60 if (outputJson()) {
61 print("%s", json(pod));
62 } else {
63 printPod(pod);
64 }
65 }
66 }
67
68 private void printPod(Pod pod) {
69 print("Name: %s", pod.getMetadata().getName());
70 print(" Status: %s", pod.getStatus().getPhase());
71 print(" Node Name: %s", pod.getSpec().getNodeName());
72 print(" Namespace: %s", pod.getMetadata().getNamespace());
73 print(" IP address: %s", pod.getStatus().getPodIP());
74
75 int counter = 1;
76 for (Container container : pod.getSpec().getContainers()) {
77 print(" Container #%d:", counter);
78 print(" Name: %s", container.getName());
79 print(" Image: %s", container.getImage());
80 print(" Pull Policy: %s", container.getImagePullPolicy());
81 print(" Commands: %s", container.getCommand());
82 print(" Args: %s", container.getArgs());
83 }
84 }
85
86 private String json(Pod pod) {
87 try {
88 ObjectNode result = (ObjectNode)
89 new ObjectMapper().readTree(Serialization.asJson(pod));
90 return prettyJson(new ObjectMapper(), result.toString());
91 } catch (IOException e) {
92 log.warn("Failed to parse POD's JSON string.");
93 return "";
94 }
95 }
96}