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 | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
| 22 | import io.fabric8.kubernetes.api.model.Endpoints; |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 23 | import io.fabric8.kubernetes.client.utils.Serialization; |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 24 | import org.apache.karaf.shell.api.action.Command; |
| 25 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 26 | import org.onosproject.cli.AbstractShellCommand; |
| 27 | import org.onosproject.k8snetworking.api.K8sEndpointsService; |
| 28 | |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 29 | import java.io.IOException; |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 30 | import java.util.Comparator; |
| 31 | import java.util.List; |
| 32 | |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 33 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson; |
| 34 | |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 35 | /** |
| 36 | * Lists kubernetes endpoints. |
| 37 | */ |
| 38 | @Service |
| 39 | @Command(scope = "onos", name = "k8s-endpoints", |
| 40 | description = "Lists all kubernetes endpoints") |
| 41 | public class K8sEndpointsListCommand extends AbstractShellCommand { |
| 42 | |
| 43 | private static final String FORMAT = "%-50s%-50s%-20s"; |
Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +0900 | [diff] [blame] | 44 | private static final String PORT_PROTOCOL_SEPARATOR = "/"; |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 45 | |
| 46 | @Override |
| 47 | protected void doExecute() { |
| 48 | K8sEndpointsService service = get(K8sEndpointsService.class); |
| 49 | List<Endpoints> endpointses = Lists.newArrayList(service.endpointses()); |
| 50 | endpointses.sort(Comparator.comparing(e -> e.getMetadata().getName())); |
| 51 | |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 52 | if (outputJson()) { |
| 53 | print("%s", json(endpointses)); |
| 54 | } else { |
| 55 | print(FORMAT, "Name", "IP Addresses", "Ports"); |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 56 | |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 57 | for (Endpoints endpoints : endpointses) { |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 58 | |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 59 | List<String> ips = Lists.newArrayList(); |
| 60 | List<String> portWithProtocol = Lists.newArrayList(); |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 61 | |
Jian Li | ca34c67 | 2019-02-26 14:04:08 +0900 | [diff] [blame] | 62 | endpoints.getSubsets().forEach(e -> { |
| 63 | e.getAddresses().forEach(a -> ips.add(a.getIp())); |
| 64 | e.getPorts().forEach(p -> portWithProtocol.add(p.getPort() + |
| 65 | PORT_PROTOCOL_SEPARATOR + p.getProtocol())); |
| 66 | }); |
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 | print(FORMAT, |
| 69 | endpoints.getMetadata().getName(), |
| 70 | ips.isEmpty() ? "" : ips, |
| 71 | portWithProtocol.isEmpty() ? "" : portWithProtocol); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private String json(List<Endpoints> endpointses) { |
| 77 | ObjectMapper mapper = new ObjectMapper(); |
| 78 | ArrayNode result = mapper.createArrayNode(); |
| 79 | try { |
| 80 | for (Endpoints endpoints : endpointses) { |
| 81 | ObjectNode json = (ObjectNode) new ObjectMapper() |
| 82 | .readTree(Serialization.asJson(endpoints)); |
| 83 | result.add(json); |
| 84 | } |
| 85 | return prettyJson(mapper, result.toString()); |
| 86 | } catch (IOException e) { |
| 87 | log.warn("Failed to parse endpoints's JSON string."); |
| 88 | return ""; |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |