blob: c866f044d485990a071795f1aa32e7cdb4e56865 [file] [log] [blame]
Jian Lib1cd0b02019-02-21 16:41:40 +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
Jian Lica34c672019-02-26 14:04:08 +090018import 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 Lib1cd0b02019-02-21 16:41:40 +090022import com.google.common.collect.Lists;
23import io.fabric8.kubernetes.api.model.Endpoints;
Jian Lica34c672019-02-26 14:04:08 +090024import io.fabric8.kubernetes.client.utils.Serialization;
Jian Lia6f58382019-12-16 14:22:13 +090025import org.apache.commons.lang.StringUtils;
Jian Lib1cd0b02019-02-21 16:41:40 +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.K8sEndpointsService;
30
Jian Lica34c672019-02-26 14:04:08 +090031import java.io.IOException;
Jian Lib1cd0b02019-02-21 16:41:40 +090032import java.util.Comparator;
33import java.util.List;
34
Jian Lia6f58382019-12-16 14:22:13 +090035import static org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESSES_LENGTH;
36import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH;
37import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH;
38import static org.onosproject.k8snetworking.api.Constants.CLI_PORTS_LENGTH;
39import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
Jian Lica34c672019-02-26 14:04:08 +090040import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
41
Jian Lib1cd0b02019-02-21 16:41:40 +090042/**
43 * Lists kubernetes endpoints.
44 */
45@Service
46@Command(scope = "onos", name = "k8s-endpoints",
47 description = "Lists all kubernetes endpoints")
48public class K8sEndpointsListCommand extends AbstractShellCommand {
49
Jian Li2cc2b632019-02-18 00:56:40 +090050 private static final String PORT_PROTOCOL_SEPARATOR = "/";
Jian Lib1cd0b02019-02-21 16:41:40 +090051
52 @Override
53 protected void doExecute() {
54 K8sEndpointsService service = get(K8sEndpointsService.class);
55 List<Endpoints> endpointses = Lists.newArrayList(service.endpointses());
56 endpointses.sort(Comparator.comparing(e -> e.getMetadata().getName()));
57
Jian Lia6f58382019-12-16 14:22:13 +090058 String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
59 CLI_IP_ADDRESSES_LENGTH, CLI_PORTS_LENGTH));
60
Jian Lica34c672019-02-26 14:04:08 +090061 if (outputJson()) {
62 print("%s", json(endpointses));
63 } else {
Jian Lia6f58382019-12-16 14:22:13 +090064 print(format, "Name", "IP Addresses", "Ports");
Jian Lib1cd0b02019-02-21 16:41:40 +090065
Jian Lica34c672019-02-26 14:04:08 +090066 for (Endpoints endpoints : endpointses) {
Jian Lib1cd0b02019-02-21 16:41:40 +090067
Jian Lica34c672019-02-26 14:04:08 +090068 List<String> ips = Lists.newArrayList();
69 List<String> portWithProtocol = Lists.newArrayList();
Jian Lib1cd0b02019-02-21 16:41:40 +090070
Jian Lica34c672019-02-26 14:04:08 +090071 endpoints.getSubsets().forEach(e -> {
72 e.getAddresses().forEach(a -> ips.add(a.getIp()));
73 e.getPorts().forEach(p -> portWithProtocol.add(p.getPort() +
74 PORT_PROTOCOL_SEPARATOR + p.getProtocol()));
75 });
Jian Lib1cd0b02019-02-21 16:41:40 +090076
Jian Lia6f58382019-12-16 14:22:13 +090077 print(format,
78 StringUtils.substring(endpoints.getMetadata().getName(),
79 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
80 ips.isEmpty() ? "" : StringUtils.substring(ips.toString(),
81 0, CLI_IP_ADDRESSES_LENGTH - CLI_MARGIN_LENGTH),
Jian Lica34c672019-02-26 14:04:08 +090082 portWithProtocol.isEmpty() ? "" : portWithProtocol);
83 }
84 }
85 }
86
87 private String json(List<Endpoints> endpointses) {
88 ObjectMapper mapper = new ObjectMapper();
89 ArrayNode result = mapper.createArrayNode();
90 try {
91 for (Endpoints endpoints : endpointses) {
92 ObjectNode json = (ObjectNode) new ObjectMapper()
93 .readTree(Serialization.asJson(endpoints));
94 result.add(json);
95 }
96 return prettyJson(mapper, result.toString());
97 } catch (IOException e) {
98 log.warn("Failed to parse endpoints's JSON string.");
99 return "";
Jian Lib1cd0b02019-02-21 16:41:40 +0900100 }
101 }
102}