blob: 3f9dc28131c75d45c9ba9b9a1b2e85277b791a04 [file] [log] [blame]
Jian Li1cf51882019-02-26 14:41:20 +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 Li1cf51882019-02-26 14:41:20 +090022import com.google.common.collect.Lists;
23import io.fabric8.kubernetes.api.model.extensions.Ingress;
24import io.fabric8.kubernetes.client.utils.Serialization;
Jian Lia6f58382019-12-16 14:22:13 +090025import org.apache.commons.lang.StringUtils;
Jian Li1cf51882019-02-26 14:41:20 +090026import org.apache.karaf.shell.api.action.Command;
27import org.apache.karaf.shell.api.action.lifecycle.Service;
28import org.onosproject.cli.AbstractShellCommand;
29import org.onosproject.k8snetworking.api.K8sIngressService;
30
31import java.io.IOException;
32import java.util.Comparator;
33import java.util.List;
34
Jian Lia6f58382019-12-16 14:22:13 +090035import static org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
36import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH;
37import static org.onosproject.k8snetworking.api.Constants.CLI_NAMESPACE_LENGTH;
38import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH;
39import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
Jian Li1cf51882019-02-26 14:41:20 +090040import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
41
42/**
43 * Lists kubernetes ingresses.
44 */
45@Service
46@Command(scope = "onos", name = "k8s-ingresses",
47 description = "Lists all kubernetes ingresses")
48public class K8sIngressListCommand extends AbstractShellCommand {
49
Jian Li1cf51882019-02-26 14:41:20 +090050 @Override
51 protected void doExecute() {
52 K8sIngressService service = get(K8sIngressService.class);
53 List<Ingress> ingresses = Lists.newArrayList(service.ingresses());
54 ingresses.sort(Comparator.comparing(p -> p.getMetadata().getName()));
55
Jian Lia6f58382019-12-16 14:22:13 +090056 String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
57 CLI_NAMESPACE_LENGTH, CLI_IP_ADDRESS_LENGTH));
58
Jian Li1cf51882019-02-26 14:41:20 +090059 if (outputJson()) {
60 print("%s", json(ingresses));
61 } else {
Jian Lia6f58382019-12-16 14:22:13 +090062 print(format, "Name", "Namespace", "LB Addresses");
Jian Li1cf51882019-02-26 14:41:20 +090063
64 for (Ingress ingress : ingresses) {
65
66 List<String> lbIps = Lists.newArrayList();
67
68 ingress.getStatus().getLoadBalancer()
69 .getIngress().forEach(i -> lbIps.add(i.getIp()));
70
Jian Lia6f58382019-12-16 14:22:13 +090071 print(format,
72 StringUtils.substring(ingress.getMetadata().getName(),
73 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
74 StringUtils.substring(ingress.getMetadata().getNamespace(),
75 0, CLI_NAMESPACE_LENGTH - CLI_MARGIN_LENGTH),
Jian Li1cf51882019-02-26 14:41:20 +090076 lbIps.isEmpty() ? "" : lbIps);
77 }
78 }
79 }
80
81 private String json(List<Ingress> ingresses) {
82 ObjectMapper mapper = new ObjectMapper();
83 ArrayNode result = mapper.createArrayNode();
84 try {
85 for (Ingress ingress : ingresses) {
86 ObjectNode json = (ObjectNode) new ObjectMapper()
87 .readTree(Serialization.asJson(ingress));
88 result.add(json);
89 }
90 return prettyJson(mapper, result.toString());
91 } catch (IOException e) {
92 log.warn("Failed to parse ingress's JSON string.");
93 return "";
94 }
95 }
96}