Jian Li | 324d6dc | 2019-07-10 10:55:15 +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 | |
| 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 | 324d6dc | 2019-07-10 10:55:15 +0900 | [diff] [blame] | 22 | import com.google.common.collect.Lists; |
| 23 | import io.fabric8.kubernetes.api.model.Namespace; |
| 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 | 324d6dc | 2019-07-10 10:55:15 +0900 | [diff] [blame] | 26 | import org.apache.karaf.shell.api.action.Command; |
| 27 | import org.onosproject.cli.AbstractShellCommand; |
| 28 | import org.onosproject.k8snetworking.api.K8sNamespaceService; |
| 29 | |
| 30 | import java.io.IOException; |
| 31 | import java.util.Comparator; |
| 32 | import java.util.List; |
| 33 | |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 34 | import static org.onosproject.k8snetworking.api.Constants.CLI_LABELS_LENGTH; |
| 35 | import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH; |
| 36 | import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH; |
| 37 | import static org.onosproject.k8snetworking.api.Constants.CLI_PHASE_LENGTH; |
| 38 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString; |
Jian Li | 324d6dc | 2019-07-10 10:55:15 +0900 | [diff] [blame] | 39 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson; |
| 40 | |
| 41 | /** |
| 42 | * Lists kubernetes namespaces. |
| 43 | */ |
| 44 | @Command(scope = "onos", name = "k8s-namespaces", |
| 45 | description = "Lists all kubernetes namespaces") |
| 46 | public class K8sNamespaceListCommand extends AbstractShellCommand { |
| 47 | |
Jian Li | 324d6dc | 2019-07-10 10:55:15 +0900 | [diff] [blame] | 48 | @Override |
| 49 | protected void doExecute() { |
| 50 | K8sNamespaceService service = get(K8sNamespaceService.class); |
| 51 | List<Namespace> namespaces = Lists.newArrayList(service.namespaces()); |
| 52 | namespaces.sort(Comparator.comparing(n -> n.getMetadata().getName())); |
| 53 | |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 54 | String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, |
| 55 | CLI_PHASE_LENGTH, CLI_LABELS_LENGTH)); |
| 56 | |
Jian Li | 324d6dc | 2019-07-10 10:55:15 +0900 | [diff] [blame] | 57 | if (outputJson()) { |
| 58 | print("%s", json(namespaces)); |
| 59 | } else { |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 60 | print(format, "Name", "Phase", "Labels"); |
Jian Li | 324d6dc | 2019-07-10 10:55:15 +0900 | [diff] [blame] | 61 | |
| 62 | for (Namespace namespace : namespaces) { |
| 63 | |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 64 | print(format, |
| 65 | StringUtils.substring(namespace.getMetadata().getName(), |
| 66 | 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), |
Jian Li | 324d6dc | 2019-07-10 10:55:15 +0900 | [diff] [blame] | 67 | namespace.getStatus().getPhase(), |
| 68 | namespace.getMetadata() != null && |
| 69 | namespace.getMetadata().getLabels() != null && |
| 70 | !namespace.getMetadata().getLabels().isEmpty() ? |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 71 | StringUtils.substring(namespace.getMetadata() |
| 72 | .getLabels().toString(), 0, |
| 73 | CLI_LABELS_LENGTH - CLI_MARGIN_LENGTH) : ""); |
Jian Li | 324d6dc | 2019-07-10 10:55:15 +0900 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private String json(List<Namespace> namespaces) { |
| 79 | ObjectMapper mapper = new ObjectMapper(); |
| 80 | ArrayNode result = mapper.createArrayNode(); |
| 81 | |
| 82 | try { |
| 83 | for (Namespace namespace : namespaces) { |
| 84 | ObjectNode json = (ObjectNode) new ObjectMapper() |
| 85 | .readTree(Serialization.asJson(namespace)); |
| 86 | result.add(json); |
| 87 | } |
| 88 | return prettyJson(mapper, result.toString()); |
| 89 | } catch (IOException e) { |
| 90 | log.warn("Failed to parse Namespace's JSON string."); |
| 91 | return ""; |
| 92 | } |
| 93 | } |
| 94 | } |