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.Pod; |
| 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.K8sPodService; |
| 24 | |
| 25 | import java.util.Comparator; |
| 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * Lists kubernetes pods. |
| 30 | */ |
| 31 | @Service |
| 32 | @Command(scope = "onos", name = "k8s-pods", |
| 33 | description = "Lists all kubernetes pods") |
| 34 | public class K8sPodListCommand extends AbstractShellCommand { |
| 35 | |
| 36 | private static final String FORMAT = "%-50s%-15s%-15s%-30s"; |
| 37 | |
| 38 | @Override |
| 39 | protected void doExecute() { |
| 40 | K8sPodService service = get(K8sPodService.class); |
| 41 | List<Pod> pods = Lists.newArrayList(service.pods()); |
| 42 | pods.sort(Comparator.comparing(p -> p.getMetadata().getName())); |
| 43 | |
Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +0900 | [diff] [blame^] | 44 | print(FORMAT, "Name", "Namespace", "IP Address", "Containers"); |
Jian Li | b1cd0b0 | 2019-02-21 16:41:40 +0900 | [diff] [blame] | 45 | |
| 46 | for (Pod pod : pods) { |
| 47 | |
| 48 | List<String> containers = Lists.newArrayList(); |
| 49 | |
| 50 | pod.getSpec().getContainers().forEach(c -> containers.add(c.getName())); |
| 51 | |
| 52 | print(FORMAT, |
| 53 | pod.getMetadata().getName(), |
| 54 | pod.getMetadata().getNamespace(), |
| 55 | pod.getStatus().getPodIP(), |
| 56 | containers.isEmpty() ? "" : containers); |
| 57 | } |
| 58 | } |
| 59 | } |