Jian Li | f16e885 | 2019-01-22 22:55:31 +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.k8snode.cli; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import org.apache.karaf.shell.api.action.Command; |
| 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.k8snode.api.K8sNode; |
| 25 | import org.onosproject.k8snode.api.K8sNodeService; |
| 26 | |
| 27 | import java.util.Comparator; |
| 28 | import java.util.List; |
| 29 | |
| 30 | import static org.onosproject.k8snode.util.K8sNodeUtil.prettyJson; |
| 31 | |
| 32 | /** |
| 33 | * Lists all nodes registered to the service. |
| 34 | */ |
| 35 | @Service |
| 36 | @Command(scope = "onos", name = "k8s-nodes", |
| 37 | description = "Lists all nodes registered in kubernetes node service") |
| 38 | public class K8sNodeListCommand extends AbstractShellCommand { |
| 39 | |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame^] | 40 | private static final String FORMAT = "%-28s%-15s%-24s%-20s%-15s"; |
Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 41 | |
| 42 | @Override |
| 43 | protected void doExecute() { |
| 44 | K8sNodeService nodeService = get(K8sNodeService.class); |
| 45 | List<K8sNode> nodes = Lists.newArrayList(nodeService.nodes()); |
| 46 | nodes.sort(Comparator.comparing(K8sNode::hostname)); |
| 47 | |
| 48 | if (outputJson()) { |
| 49 | print("%s", json(nodes)); |
| 50 | } else { |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame^] | 51 | print(FORMAT, "Hostname", "Type", "Management IP", "Data IP", "State"); |
Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 52 | for (K8sNode node : nodes) { |
| 53 | print(FORMAT, |
| 54 | node.hostname(), |
| 55 | node.type(), |
Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 56 | node.managementIp(), |
| 57 | node.dataIp() != null ? node.dataIp() : "", |
| 58 | node.state()); |
| 59 | } |
| 60 | print("Total %s nodes", nodeService.nodes().size()); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | private String json(List<K8sNode> nodes) { |
| 65 | ObjectMapper mapper = new ObjectMapper(); |
| 66 | ArrayNode result = mapper.createArrayNode(); |
| 67 | for (K8sNode node : nodes) { |
| 68 | result.add(jsonForEntity(node, K8sNode.class)); |
| 69 | } |
| 70 | return prettyJson(mapper, result.toString()); |
| 71 | } |
| 72 | } |