blob: 4356bc7507f795f564bb0d163f2408b8268e6e83 [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;
Jian Lica34c672019-02-26 14:04:08 +090023import io.fabric8.kubernetes.client.utils.Serialization;
Jian Lia6f58382019-12-16 14:22:13 +090024import org.apache.commons.lang.StringUtils;
Jian Lib1cd0b02019-02-21 16:41:40 +090025import org.apache.karaf.shell.api.action.Command;
26import org.apache.karaf.shell.api.action.lifecycle.Service;
27import org.onosproject.cli.AbstractShellCommand;
28import org.onosproject.k8snetworking.api.K8sServiceService;
29
Jian Lica34c672019-02-26 14:04:08 +090030import java.io.IOException;
Jian Lib1cd0b02019-02-21 16:41:40 +090031import java.util.Comparator;
32import java.util.List;
33
Jian Lia6f58382019-12-16 14:22:13 +090034import static org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESS_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_PORTS_LENGTH;
38import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
Jian Lica34c672019-02-26 14:04:08 +090039import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
40
Jian Lib1cd0b02019-02-21 16:41:40 +090041/**
42 * Lists kubernetes services.
43 */
44@Service
45@Command(scope = "onos", name = "k8s-services",
46 description = "Lists all kubernetes services")
47public class K8sServiceListCommand extends AbstractShellCommand {
48
Jian Li2cc2b632019-02-18 00:56:40 +090049 private static final String PORT_PROTOCOL_SEPARATOR = "/";
Jian Lib1cd0b02019-02-21 16:41:40 +090050
51 @Override
52 protected void doExecute() {
53 K8sServiceService service = get(K8sServiceService.class);
54 List<io.fabric8.kubernetes.api.model.Service> services =
55 Lists.newArrayList(service.services());
56 services.sort(Comparator.comparing(s -> s.getMetadata().getName()));
57
Jian Lia6f58382019-12-16 14:22:13 +090058 String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
59 CLI_IP_ADDRESS_LENGTH, CLI_PORTS_LENGTH));
60
Jian Lica34c672019-02-26 14:04:08 +090061 if (outputJson()) {
62 print("%s", json(services));
63 } else {
Jian Lia6f58382019-12-16 14:22:13 +090064 print(format, "Name", "Cluster IP", "Ports");
Jian Lib1cd0b02019-02-21 16:41:40 +090065
Jian Lica34c672019-02-26 14:04:08 +090066 for (io.fabric8.kubernetes.api.model.Service svc : services) {
Jian Lib1cd0b02019-02-21 16:41:40 +090067
Jian Lica34c672019-02-26 14:04:08 +090068 List<String> portWithProtocol = Lists.newArrayList();
Jian Lib1cd0b02019-02-21 16:41:40 +090069
Jian Lica34c672019-02-26 14:04:08 +090070 svc.getSpec().getPorts().forEach(p ->
71 portWithProtocol.add(p.getPort() +
72 PORT_PROTOCOL_SEPARATOR + p.getProtocol()));
Jian Lib1cd0b02019-02-21 16:41:40 +090073
Jian Lia6f58382019-12-16 14:22:13 +090074 print(format,
75 StringUtils.substring(svc.getMetadata().getName(),
76 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
77 StringUtils.substring(svc.getSpec().getClusterIP(),
78 0, CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH),
Jian Lica34c672019-02-26 14:04:08 +090079 portWithProtocol.isEmpty() ? "" : portWithProtocol);
80 }
81 }
82 }
83
84 private String json(List<io.fabric8.kubernetes.api.model.Service> services) {
85 ObjectMapper mapper = new ObjectMapper();
86 ArrayNode result = mapper.createArrayNode();
87 try {
88 for (io.fabric8.kubernetes.api.model.Service service : services) {
89 ObjectNode json = (ObjectNode) new ObjectMapper()
90 .readTree(Serialization.asJson(service));
91 result.add(json);
92 }
93 return prettyJson(mapper, result.toString());
94 } catch (IOException e) {
95 log.warn("Failed to parse service's JSON string.");
96 return "";
Jian Lib1cd0b02019-02-21 16:41:40 +090097 }
98 }
99}