Jian Li | 105770e | 2021-01-17 02:18:30 +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.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.Lists; |
| 23 | import io.fabric8.kubernetes.api.model.Pod; |
| 24 | import io.fabric8.kubernetes.client.utils.Serialization; |
| 25 | import org.apache.commons.lang.StringUtils; |
| 26 | import org.apache.karaf.shell.api.action.Command; |
| 27 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 28 | import org.onosproject.cli.AbstractShellCommand; |
| 29 | import org.onosproject.kubevirtnetworking.api.KubevirtPodService; |
| 30 | |
| 31 | import java.io.IOException; |
| 32 | import java.util.Comparator; |
| 33 | import java.util.List; |
| 34 | |
| 35 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_LENGTH; |
| 36 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_MARGIN_LENGTH; |
| 37 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAMESPACE_LENGTH; |
| 38 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAME_LENGTH; |
| 39 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_STATUS_LENGTH; |
| 40 | import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString; |
| 41 | import 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") |
| 49 | public 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 | } |