blob: 1d6cf3d902be3d94d84acf3bb42886e7e57cf559 [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 Lib1cd0b02019-02-21 16:41:40 +090021import com.google.common.collect.Lists;
Jian Lica34c672019-02-26 14:04:08 +090022import io.fabric8.kubernetes.client.utils.Serialization;
Jian Lib1cd0b02019-02-21 16:41:40 +090023import org.apache.karaf.shell.api.action.Command;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.k8snetworking.api.K8sServiceService;
27
Jian Lica34c672019-02-26 14:04:08 +090028import java.io.IOException;
Jian Lib1cd0b02019-02-21 16:41:40 +090029import java.util.Comparator;
30import java.util.List;
31
Jian Lica34c672019-02-26 14:04:08 +090032import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
33
Jian Lib1cd0b02019-02-21 16:41:40 +090034/**
35 * Lists kubernetes services.
36 */
37@Service
38@Command(scope = "onos", name = "k8s-services",
39 description = "Lists all kubernetes services")
40public class K8sServiceListCommand extends AbstractShellCommand {
41
42 private static final String FORMAT = "%-50s%-30s%-30s";
Jian Li2cc2b632019-02-18 00:56:40 +090043 private static final String PORT_PROTOCOL_SEPARATOR = "/";
Jian Lib1cd0b02019-02-21 16:41:40 +090044
45 @Override
46 protected void doExecute() {
47 K8sServiceService service = get(K8sServiceService.class);
48 List<io.fabric8.kubernetes.api.model.Service> services =
49 Lists.newArrayList(service.services());
50 services.sort(Comparator.comparing(s -> s.getMetadata().getName()));
51
Jian Lica34c672019-02-26 14:04:08 +090052 if (outputJson()) {
53 print("%s", json(services));
54 } else {
55 print(FORMAT, "Name", "Cluster IP", "Ports");
Jian Lib1cd0b02019-02-21 16:41:40 +090056
Jian Lica34c672019-02-26 14:04:08 +090057 for (io.fabric8.kubernetes.api.model.Service svc : services) {
Jian Lib1cd0b02019-02-21 16:41:40 +090058
Jian Lica34c672019-02-26 14:04:08 +090059 List<String> portWithProtocol = Lists.newArrayList();
Jian Lib1cd0b02019-02-21 16:41:40 +090060
Jian Lica34c672019-02-26 14:04:08 +090061 svc.getSpec().getPorts().forEach(p ->
62 portWithProtocol.add(p.getPort() +
63 PORT_PROTOCOL_SEPARATOR + p.getProtocol()));
Jian Lib1cd0b02019-02-21 16:41:40 +090064
Jian Lica34c672019-02-26 14:04:08 +090065 print(FORMAT,
66 svc.getMetadata().getName(),
67 svc.getSpec().getClusterIP(),
68 portWithProtocol.isEmpty() ? "" : portWithProtocol);
69 }
70 }
71 }
72
73 private String json(List<io.fabric8.kubernetes.api.model.Service> services) {
74 ObjectMapper mapper = new ObjectMapper();
75 ArrayNode result = mapper.createArrayNode();
76 try {
77 for (io.fabric8.kubernetes.api.model.Service service : services) {
78 ObjectNode json = (ObjectNode) new ObjectMapper()
79 .readTree(Serialization.asJson(service));
80 result.add(json);
81 }
82 return prettyJson(mapper, result.toString());
83 } catch (IOException e) {
84 log.warn("Failed to parse service's JSON string.");
85 return "";
Jian Lib1cd0b02019-02-21 16:41:40 +090086 }
87 }
88}