|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | +package io.kubernetes.client.examples; |
| 14 | + |
| 15 | +import static io.kubernetes.client.extended.kubectl.Kubectl.label; |
| 16 | +import static io.kubernetes.client.extended.kubectl.Kubectl.scale; |
| 17 | +import static io.kubernetes.client.extended.kubectl.Kubectl.version; |
| 18 | + |
| 19 | +import io.kubernetes.client.common.KubernetesObject; |
| 20 | +import io.kubernetes.client.extended.kubectl.exception.KubectlException; |
| 21 | +import io.kubernetes.client.openapi.ApiClient; |
| 22 | +import io.kubernetes.client.openapi.models.V1Deployment; |
| 23 | +import io.kubernetes.client.openapi.models.V1Node; |
| 24 | +import io.kubernetes.client.openapi.models.V1Pod; |
| 25 | +import io.kubernetes.client.openapi.models.V1ReplicationController; |
| 26 | +import io.kubernetes.client.openapi.models.V1Service; |
| 27 | +import io.kubernetes.client.util.Config; |
| 28 | +import org.apache.commons.cli.CommandLine; |
| 29 | +import org.apache.commons.cli.DefaultParser; |
| 30 | +import org.apache.commons.cli.Option; |
| 31 | +import org.apache.commons.cli.Options; |
| 32 | +import org.apache.commons.cli.ParseException; |
| 33 | + |
| 34 | +/** |
| 35 | + * A Java equivalent for the kubectl command line tool. Not nearly as complete. |
| 36 | + * |
| 37 | + * <p>Easiest way to run this: mvn exec:java |
| 38 | + * -Dexec.mainClass="io.kubernetes.client.examples.KubectlExample" |
| 39 | + * |
| 40 | + * <p>From inside $REPO_DIR/examples |
| 41 | + */ |
| 42 | +public class KubectlExample { |
| 43 | + static Class<? extends KubernetesObject> getClassForKind(String kind) { |
| 44 | + switch (kind) { |
| 45 | + case "pod": |
| 46 | + case "pods": |
| 47 | + return V1Pod.class; |
| 48 | + case "deployment": |
| 49 | + case "deployments": |
| 50 | + return V1Deployment.class; |
| 51 | + case "service": |
| 52 | + case "services": |
| 53 | + return V1Service.class; |
| 54 | + case "node": |
| 55 | + case "nodes": |
| 56 | + return V1Node.class; |
| 57 | + case "replicationcontroller": |
| 58 | + case "replicationcontrollers": |
| 59 | + return V1ReplicationController.class; |
| 60 | + } |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + public static void main(String[] args) |
| 65 | + throws java.io.IOException, KubectlException, ParseException { |
| 66 | + ApiClient client = Config.defaultClient(); |
| 67 | + |
| 68 | + Options options = new Options(); |
| 69 | + options.addOption(new Option("n", "namespace", true, "The namespace for the resource")); |
| 70 | + options.addOption(new Option("r", "replicas", true, "The number of replicas to scale to")); |
| 71 | + DefaultParser parser = new DefaultParser(); |
| 72 | + CommandLine cli = parser.parse(options, args); |
| 73 | + |
| 74 | + args = cli.getArgs(); |
| 75 | + String verb = args[0]; |
| 76 | + String ns = cli.getOptionValue("n", "default"); |
| 77 | + String kind = null; |
| 78 | + String name = null; |
| 79 | + |
| 80 | + switch (verb) { |
| 81 | + case "scale": |
| 82 | + kind = args[1]; |
| 83 | + name = args[2]; |
| 84 | + if (!cli.hasOption("r")) { |
| 85 | + System.err.println("--replicas <n> is required"); |
| 86 | + System.exit(-3); |
| 87 | + } |
| 88 | + int replicas = Integer.parseInt(cli.getOptionValue("r")); |
| 89 | + scale(client, getClassForKind(kind)).namespace(ns).name(name).replicas(replicas).execute(); |
| 90 | + System.out.println("Deployment scaled."); |
| 91 | + System.exit(0); |
| 92 | + case "version": |
| 93 | + System.out.println(version(client)); |
| 94 | + System.exit(0); |
| 95 | + case "label": |
| 96 | + kind = args[1]; |
| 97 | + name = args[2]; |
| 98 | + String labelKey = args[3]; |
| 99 | + String labelValue = args[4]; |
| 100 | + Class<? extends KubernetesObject> clazz = getClassForKind(kind); |
| 101 | + if (clazz == null) { |
| 102 | + System.err.println("Unknown kind: " + kind); |
| 103 | + System.exit(-2); |
| 104 | + } |
| 105 | + label(client, clazz).namespace(ns).name(name).addLabel(labelKey, labelValue); |
| 106 | + System.exit(0); |
| 107 | + default: |
| 108 | + System.out.println("Unknown verb: " + verb); |
| 109 | + System.exit(-1); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments