Search in sources :

Example 1 with KubectlPortForward

use of io.kubernetes.client.extended.kubectl.KubectlPortForward in project java by kubernetes-client.

the class KubectlExample method main.

public static void main(String[] args) throws java.io.IOException, KubectlException, ParseException {
    ApiClient client = Config.defaultClient();
    Options options = new Options();
    options.addOption(new Option("n", "namespace", true, "The namespace for the resource"));
    options.addOption(new Option("r", "replicas", true, "The number of replicas to scale to"));
    options.addOption(new Option("c", "container", true, "The container in a pod to connect to"));
    DefaultParser parser = new DefaultParser();
    CommandLine cli = parser.parse(options, args);
    args = cli.getArgs();
    String verb = args[0];
    String ns = cli.getOptionValue("n", "default");
    String kind = null;
    String name = null;
    switch(verb) {
        case "delete":
            kind = args[1];
            name = args[2];
            delete(getClassForKind(kind)).namespace(ns).name(name).execute();
        case "drain":
            name = args[1];
            drain().apiClient(client).name(name).execute();
            System.out.println("Node drained");
            System.exit(0);
        case "cordon":
            name = args[1];
            cordon().apiClient(client).name(name).execute();
            System.out.println("Node cordoned");
            System.exit(0);
        case "uncordon":
            name = args[1];
            uncordon().apiClient(client).name(name).execute();
            System.out.println("Node uncordoned");
            System.exit(0);
        case "top":
            String what = args[1];
            switch(what) {
                case "nodes":
                case "node":
                    List<Pair<V1Node, NodeMetrics>> nodes = top(V1Node.class, NodeMetrics.class).apiClient(client).metric("cpu").execute();
                    System.out.println(pad("Node") + "\tCPU\t\tMemory");
                    for (Pair<V1Node, NodeMetrics> node : nodes) {
                        System.out.println(pad(node.getLeft().getMetadata().getName()) + "\t" + node.getRight().getUsage().get("cpu").getNumber() + "\t" + node.getRight().getUsage().get("memory").getNumber());
                    }
                    System.exit(0);
                case "pods":
                case "pod":
                    List<Pair<V1Pod, PodMetrics>> pods = top(V1Pod.class, PodMetrics.class).apiClient(client).namespace(ns).metric("cpu").execute();
                    System.out.println(pad("Pod") + "\tCPU\t\tMemory");
                    for (Pair<V1Pod, PodMetrics> pod : pods) {
                        System.out.println(pad(pod.getLeft().getMetadata().getName()) + "\t" + podMetricSum(pod.getRight(), "cpu") + "\t" + podMetricSum(pod.getRight(), "memory"));
                    }
                    System.exit(0);
            }
            System.err.println("Unknown top argument: " + what);
            System.exit(-1);
        case "cp":
            String from = args[1];
            String to = args[2];
            if (from.indexOf(":") != -1) {
                String[] parts = from.split(":");
                name = parts[0];
                from = parts[1];
                copy().apiClient(client).namespace(ns).name(name).container(cli.getOptionValue("c", "")).fromPod(from).to(to).execute();
            } else if (to.indexOf(":") != -1) {
                String[] parts = to.split(":");
                name = parts[0];
                to = parts[1];
                copy().apiClient(client).namespace(ns).name(name).container(cli.getOptionValue("c", "")).from(from).toPod(to).execute();
            } else {
                System.err.println("Missing pod name for copy.");
                System.exit(-1);
            }
            System.out.println("Copied " + from + " -> " + to);
            System.exit(0);
        case "taint":
            name = args[1];
            String taintSpec = args[2];
            boolean remove = taintSpec.endsWith("-");
            int ix = taintSpec.indexOf("=");
            int ix2 = taintSpec.indexOf(":");
            if (remove) {
                taintSpec = taintSpec.substring(0, taintSpec.length() - 2);
                String key = ix == -1 ? taintSpec : taintSpec.substring(0, ix);
                String effect = ix == -1 ? null : taintSpec.substring(ix + 1);
                if (effect == null) {
                    taint().apiClient(client).name(name).removeTaint(key).execute();
                } else {
                    taint().apiClient(client).name(name).removeTaint(key, effect).execute();
                }
                System.exit(0);
            }
            if (ix2 == -1) {
                System.err.println("key:effect or key=value:effect is required.");
                System.exit(-1);
            }
            String key = taintSpec.substring(0, ix == -1 ? ix2 : ix);
            String value = ix == -1 ? null : taintSpec.substring(ix + 1, ix2);
            String effect = taintSpec.substring(ix2 + 1);
            if (value == null) {
                taint().apiClient(client).name(name).addTaint(key, effect).execute();
            } else {
                taint().apiClient(client).name(name).addTaint(key, value, effect).execute();
            }
            System.exit(0);
        case "portforward":
            name = args[1];
            KubectlPortForward forward = portforward().apiClient(client).name(name).namespace(ns);
            for (int i = 2; i < args.length; i++) {
                String port = args[i];
                String[] ports = port.split(":");
                System.out.println("Forwarding " + ns + "/" + name + " " + ports[0] + "->" + ports[1]);
                forward.ports(Integer.parseInt(ports[0]), Integer.parseInt(ports[1]));
            }
            forward.execute();
            System.exit(0);
        case "log":
            name = args[1];
            Streams.copy(log().apiClient(client).name(name).namespace(ns).container(cli.getOptionValue("c", "")).execute(), System.out);
            System.exit(0);
        case "scale":
            kind = args[1];
            name = args[2];
            if (!cli.hasOption("r")) {
                System.err.println("--replicas <n> is required");
                System.exit(-3);
            }
            int replicas = Integer.parseInt(cli.getOptionValue("r"));
            scale(getClassForKind(kind)).apiClient(client).namespace(ns).name(name).replicas(replicas).execute();
            System.out.println("Deployment scaled.");
            System.exit(0);
        case "version":
            System.out.println(version().apiClient(client));
            System.exit(0);
        case "label":
            kind = args[1];
            name = args[2];
            String labelKey = args[3];
            String labelValue = args[4];
            Class<? extends KubernetesObject> clazz = getClassForKind(kind);
            if (clazz == null) {
                System.err.println("Unknown kind: " + kind);
                System.exit(-2);
            }
            label(clazz).apiClient(client).namespace(ns).name(name).addLabel(labelKey, labelValue);
            System.exit(0);
        case "exec":
            name = args[1];
            String[] command = Arrays.copyOfRange(args, 2, args.length);
            KubectlExec e = exec().apiClient(client).namespace(ns).name(name).command(command).container(cli.getOptionValue("c", ""));
            System.exit(e.execute());
        case "api-resources":
            apiResources().apiClient(client).execute().stream().forEach(r -> System.out.printf("%s\t\t%s\t\t%s\t\t%s\n", r.getResourcePlural(), r.getGroup(), r.getKind(), r.getNamespaced()));
            System.exit(0);
        default:
            System.out.println("Unknown verb: " + verb);
            System.exit(-1);
    }
}
Also used : Options(org.apache.commons.cli.Options) V1Node(io.kubernetes.client.openapi.models.V1Node) NodeMetrics(io.kubernetes.client.custom.NodeMetrics) ApiClient(io.kubernetes.client.openapi.ApiClient) Kubectl.taint(io.kubernetes.client.extended.kubectl.Kubectl.taint) CommandLine(org.apache.commons.cli.CommandLine) KubectlExec(io.kubernetes.client.extended.kubectl.KubectlExec) PodMetrics(io.kubernetes.client.custom.PodMetrics) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Option(org.apache.commons.cli.Option) KubectlPortForward(io.kubernetes.client.extended.kubectl.KubectlPortForward) DefaultParser(org.apache.commons.cli.DefaultParser) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

NodeMetrics (io.kubernetes.client.custom.NodeMetrics)1 PodMetrics (io.kubernetes.client.custom.PodMetrics)1 Kubectl.taint (io.kubernetes.client.extended.kubectl.Kubectl.taint)1 KubectlExec (io.kubernetes.client.extended.kubectl.KubectlExec)1 KubectlPortForward (io.kubernetes.client.extended.kubectl.KubectlPortForward)1 ApiClient (io.kubernetes.client.openapi.ApiClient)1 V1Node (io.kubernetes.client.openapi.models.V1Node)1 V1Pod (io.kubernetes.client.openapi.models.V1Pod)1 CommandLine (org.apache.commons.cli.CommandLine)1 DefaultParser (org.apache.commons.cli.DefaultParser)1 Option (org.apache.commons.cli.Option)1 Options (org.apache.commons.cli.Options)1 Pair (org.apache.commons.lang3.tuple.Pair)1