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 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import io.fabric8.kubernetes.api.model.Endpoints; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.k8snetworking.api.K8sEndpointsService; |
| 24 | |
| 25 | import java.util.Comparator; |
| 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * Lists kubernetes endpoints. |
| 30 | */ |
| 31 | @Service |
| 32 | @Command(scope = "onos", name = "k8s-endpoints", |
| 33 | description = "Lists all kubernetes endpoints") |
| 34 | public class K8sEndpointsListCommand extends AbstractShellCommand { |
| 35 | |
| 36 | private static final String FORMAT = "%-50s%-50s%-20s"; |
| 37 | |
| 38 | @Override |
| 39 | protected void doExecute() { |
| 40 | K8sEndpointsService service = get(K8sEndpointsService.class); |
| 41 | List<Endpoints> endpointses = Lists.newArrayList(service.endpointses()); |
| 42 | endpointses.sort(Comparator.comparing(e -> e.getMetadata().getName())); |
| 43 | |
| 44 | print(FORMAT, "Name", "IP Addresses", "Ports"); |
| 45 | |
| 46 | for (Endpoints endpoints : endpointses) { |
| 47 | |
| 48 | List<String> ips = Lists.newArrayList(); |
| 49 | List<Integer> ports = Lists.newArrayList(); |
| 50 | |
| 51 | endpoints.getSubsets().forEach(e -> { |
| 52 | e.getAddresses().forEach(a -> ips.add(a.getIp())); |
| 53 | e.getPorts().forEach(p -> ports.add(p.getPort())); |
| 54 | }); |
| 55 | |
| 56 | print(FORMAT, |
| 57 | endpoints.getMetadata().getName(), |
| 58 | ips.isEmpty() ? "" : ips, |
| 59 | ports.isEmpty() ? "" : ports); |
| 60 | } |
| 61 | } |
| 62 | } |