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