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 org.apache.karaf.shell.api.action.Command; |
| 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.k8snetworking.api.K8sServiceService; |
| 23 | |
| 24 | import java.util.Comparator; |
| 25 | import java.util.List; |
| 26 | |
| 27 | /** |
| 28 | * Lists kubernetes services. |
| 29 | */ |
| 30 | @Service |
| 31 | @Command(scope = "onos", name = "k8s-services", |
| 32 | description = "Lists all kubernetes services") |
| 33 | public class K8sServiceListCommand extends AbstractShellCommand { |
| 34 | |
| 35 | private static final String FORMAT = "%-50s%-30s%-30s"; |
Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +0900 | [diff] [blame^] | 36 | private static final String PORT_PROTOCOL_SEPARATOR = "/"; |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 37 | |
| 38 | @Override |
| 39 | protected void doExecute() { |
| 40 | K8sServiceService service = get(K8sServiceService.class); |
| 41 | List<io.fabric8.kubernetes.api.model.Service> services = |
| 42 | Lists.newArrayList(service.services()); |
| 43 | services.sort(Comparator.comparing(s -> s.getMetadata().getName())); |
| 44 | |
| 45 | print(FORMAT, "Name", "Cluster IP", "Ports"); |
| 46 | |
| 47 | for (io.fabric8.kubernetes.api.model.Service svc : services) { |
| 48 | |
Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +0900 | [diff] [blame^] | 49 | List<String> portWithProtocol = Lists.newArrayList(); |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 50 | |
Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +0900 | [diff] [blame^] | 51 | svc.getSpec().getPorts().forEach(p -> |
| 52 | portWithProtocol.add(p.getPort() + |
| 53 | PORT_PROTOCOL_SEPARATOR + p.getProtocol())); |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 54 | |
| 55 | print(FORMAT, |
| 56 | svc.getMetadata().getName(), |
| 57 | svc.getSpec().getClusterIP(), |
Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +0900 | [diff] [blame^] | 58 | portWithProtocol.isEmpty() ? "" : portWithProtocol); |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | } |