blob: db649aa315eeb96ec00bb0ccfb871e789eca2b6f [file] [log] [blame]
Jian Li324d6dc2019-07-10 10:55:15 +09001/*
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 */
16package org.onosproject.k8snetworking.cli;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Lia6f58382019-12-16 14:22:13 +090021import com.google.common.collect.ImmutableList;
Jian Li324d6dc2019-07-10 10:55:15 +090022import com.google.common.collect.Lists;
23import io.fabric8.kubernetes.api.model.Namespace;
24import io.fabric8.kubernetes.client.utils.Serialization;
Jian Lia6f58382019-12-16 14:22:13 +090025import org.apache.commons.lang.StringUtils;
Jian Li324d6dc2019-07-10 10:55:15 +090026import org.apache.karaf.shell.api.action.Command;
27import org.onosproject.cli.AbstractShellCommand;
28import org.onosproject.k8snetworking.api.K8sNamespaceService;
29
30import java.io.IOException;
31import java.util.Comparator;
32import java.util.List;
33
Jian Lia6f58382019-12-16 14:22:13 +090034import static org.onosproject.k8snetworking.api.Constants.CLI_LABELS_LENGTH;
35import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH;
36import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH;
37import static org.onosproject.k8snetworking.api.Constants.CLI_PHASE_LENGTH;
38import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
Jian Li324d6dc2019-07-10 10:55:15 +090039import 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")
46public class K8sNamespaceListCommand extends AbstractShellCommand {
47
Jian Li324d6dc2019-07-10 10:55:15 +090048 @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 Lia6f58382019-12-16 14:22:13 +090054 String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
55 CLI_PHASE_LENGTH, CLI_LABELS_LENGTH));
56
Jian Li324d6dc2019-07-10 10:55:15 +090057 if (outputJson()) {
58 print("%s", json(namespaces));
59 } else {
Jian Lia6f58382019-12-16 14:22:13 +090060 print(format, "Name", "Phase", "Labels");
Jian Li324d6dc2019-07-10 10:55:15 +090061
62 for (Namespace namespace : namespaces) {
63
Jian Lia6f58382019-12-16 14:22:13 +090064 print(format,
65 StringUtils.substring(namespace.getMetadata().getName(),
66 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
Jian Li324d6dc2019-07-10 10:55:15 +090067 namespace.getStatus().getPhase(),
68 namespace.getMetadata() != null &&
69 namespace.getMetadata().getLabels() != null &&
70 !namespace.getMetadata().getLabels().isEmpty() ?
Jian Lia6f58382019-12-16 14:22:13 +090071 StringUtils.substring(namespace.getMetadata()
72 .getLabels().toString(), 0,
73 CLI_LABELS_LENGTH - CLI_MARGIN_LENGTH) : "");
Jian Li324d6dc2019-07-10 10:55:15 +090074 }
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}