blob: ade0b01c88d33b885c65e8f83a2886b286deab4c [file] [log] [blame]
Jian Lib1cd0b02019-02-21 16:41:40 +09001/*
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 */
16package org.onosproject.k8snetworking.cli;
17
18import com.google.common.collect.Lists;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.k8snetworking.api.K8sServiceService;
23
24import java.util.Comparator;
25import 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")
33public class K8sServiceListCommand extends AbstractShellCommand {
34
35 private static final String FORMAT = "%-50s%-30s%-30s";
Jian Li2cc2b632019-02-18 00:56:40 +090036 private static final String PORT_PROTOCOL_SEPARATOR = "/";
Jian Lib1cd0b02019-02-21 16:41:40 +090037
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 Li2cc2b632019-02-18 00:56:40 +090049 List<String> portWithProtocol = Lists.newArrayList();
Jian Lib1cd0b02019-02-21 16:41:40 +090050
Jian Li2cc2b632019-02-18 00:56:40 +090051 svc.getSpec().getPorts().forEach(p ->
52 portWithProtocol.add(p.getPort() +
53 PORT_PROTOCOL_SEPARATOR + p.getProtocol()));
Jian Lib1cd0b02019-02-21 16:41:40 +090054
55 print(FORMAT,
56 svc.getMetadata().getName(),
57 svc.getSpec().getClusterIP(),
Jian Li2cc2b632019-02-18 00:56:40 +090058 portWithProtocol.isEmpty() ? "" : portWithProtocol);
Jian Lib1cd0b02019-02-21 16:41:40 +090059 }
60 }
61}