Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.k8snetworking.cli; |
| 17 | |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 22 | import com.google.common.collect.Lists; |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 23 | import io.fabric8.kubernetes.client.utils.Serialization; |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 24 | import org.apache.commons.lang.StringUtils; |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 25 | import org.apache.karaf.shell.api.action.Command; |
| 26 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 27 | import org.onosproject.cli.AbstractShellCommand; |
| 28 | import org.onosproject.k8snetworking.api.K8sServiceService; |
| 29 | |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 30 | import java.io.IOException; |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 31 | import java.util.Comparator; |
| 32 | import java.util.List; |
| 33 | |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 34 | import static org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESS_LENGTH; |
| 35 | import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH; |
| 36 | import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH; |
| 37 | import static org.onosproject.k8snetworking.api.Constants.CLI_PORTS_LENGTH; |
| 38 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString; |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 39 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson; |
| 40 | |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 41 | /** |
| 42 | * Lists kubernetes services. |
| 43 | */ |
| 44 | @Service |
| 45 | @Command(scope = "onos", name = "k8s-services", |
| 46 | description = "Lists all kubernetes services") |
| 47 | public class K8sServiceListCommand extends AbstractShellCommand { |
| 48 | |
Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +0900 | [diff] [blame] | 49 | private static final String PORT_PROTOCOL_SEPARATOR = "/"; |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 50 | |
| 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 Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 58 | String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, |
| 59 | CLI_IP_ADDRESS_LENGTH, CLI_PORTS_LENGTH)); |
| 60 | |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 61 | if (outputJson()) { |
| 62 | print("%s", json(services)); |
| 63 | } else { |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 64 | print(format, "Name", "Cluster IP", "Ports"); |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 65 | |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 66 | for (io.fabric8.kubernetes.api.model.Service svc : services) { |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 67 | |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 68 | List<String> portWithProtocol = Lists.newArrayList(); |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 69 | |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 70 | svc.getSpec().getPorts().forEach(p -> |
| 71 | portWithProtocol.add(p.getPort() + |
| 72 | PORT_PROTOCOL_SEPARATOR + p.getProtocol())); |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 73 | |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 74 | 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 Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 79 | 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 Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | } |