Jian Li | c94b7ee | 2021-02-22 21:48:32 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.kubevirtnetworking.cli; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import io.fabric8.kubernetes.api.model.Container; |
| 21 | import io.fabric8.kubernetes.api.model.Pod; |
| 22 | import io.fabric8.kubernetes.client.utils.Serialization; |
| 23 | import org.apache.karaf.shell.api.action.Command; |
| 24 | import org.apache.karaf.shell.api.action.Completion; |
| 25 | import org.apache.karaf.shell.api.action.Option; |
| 26 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 27 | import org.onosproject.cli.AbstractShellCommand; |
| 28 | import org.onosproject.kubevirtnetworking.api.KubevirtPodService; |
| 29 | |
| 30 | import java.io.IOException; |
| 31 | import java.util.List; |
| 32 | |
| 33 | import 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") |
| 41 | public 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 Li | 4929c09 | 2021-03-04 18:48:39 +0900 | [diff] [blame] | 52 | if (names == null || names.size() == 0) { |
| 53 | print("Need to specify at least one POD name using --name option."); |
| 54 | return; |
| 55 | } |
| 56 | |
Jian Li | c94b7ee | 2021-02-22 21:48:32 +0900 | [diff] [blame] | 57 | 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 Li | 47e7af7 | 2021-03-05 01:32:04 +0900 | [diff] [blame] | 88 | counter++; |
Jian Li | c94b7ee | 2021-02-22 21:48:32 +0900 | [diff] [blame] | 89 | } |
| 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 | } |