diff --git a/examples/examples-release-10/pom.xml b/examples/examples-release-10/pom.xml index d3572f5e83..3c4d7e6db0 100644 --- a/examples/examples-release-10/pom.xml +++ b/examples/examples-release-10/pom.xml @@ -1,19 +1,17 @@ 4.0.0 - - io.kubernetes - client-java-examples-parent - 1.0.0-SNAPSHOT - ../pom.xml - - + io.kubernetes client-java-examples-release-10 bundle client-java-examples-release-10 - + 1.0.0 10.0.0 + 1.8 + ${java.version} + ${java.version} + 4.13 @@ -54,6 +52,7 @@ commons-cli commons-cli + 1.4 io.kubernetes @@ -69,11 +68,13 @@ junit junit + ${junit.version} test com.github.tomakehurst wiremock + 2.27.2 test diff --git a/examples/examples-release-11/pom.xml b/examples/examples-release-11/pom.xml index b88565c91b..a41c7fb72f 100644 --- a/examples/examples-release-11/pom.xml +++ b/examples/examples-release-11/pom.xml @@ -1,16 +1,17 @@ 4.0.0 - - io.kubernetes - client-java-examples-parent - 1.0.0-SNAPSHOT - ../pom.xml - - client-java-examples-release-11 bundle client-java-examples-release-11 + 1.0.0 + + 10.0.0 + 1.8 + ${java.version} + ${java.version} + 4.13 + @@ -51,6 +52,7 @@ commons-cli commons-cli + 1.4 io.kubernetes @@ -66,11 +68,13 @@ junit junit + ${junit.version} test com.github.tomakehurst wiremock + 2.27.2 test diff --git a/examples/examples-release-12/.google b/examples/examples-release-12/.google new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/examples-release-12/Dockerfile b/examples/examples-release-12/Dockerfile new file mode 100644 index 0000000000..ac90eb1d67 --- /dev/null +++ b/examples/examples-release-12/Dockerfile @@ -0,0 +1,7 @@ +FROM openjdk:8-jre + +COPY target/client-java-examples-*-SNAPSHOT-jar-with-dependencies.jar /examples.jar + +CMD ["java", "-jar", "/examples.jar"] + + diff --git a/examples/examples-release-12/README.md b/examples/examples-release-12/README.md new file mode 100644 index 0000000000..19ac61196f --- /dev/null +++ b/examples/examples-release-12/README.md @@ -0,0 +1,13 @@ +# Running examples + +```sh +export REPO_ROOT=/path/to/client-java/repo + +cd ${REPO_ROOT}/kubernetes +mvn install + +cd ${REPO_ROOT}/examples +mvn package +mvn exec:java -Dexec.mainClass="io.kubernetes.client.examples.Example" +``` + diff --git a/examples/examples-release-12/createPod.sh b/examples/examples-release-12/createPod.sh new file mode 100755 index 0000000000..18a9841317 --- /dev/null +++ b/examples/examples-release-12/createPod.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# creates a pod and runs +# Example.java(list pods for all namespaces) on starting of pod + +# Exit on any error. +set -e + +if ! which minikube > /dev/null; then + echo "This script requires minikube installed." + exit 100 +fi + +dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +export REPO_ROOT=${dir}/.. + +cd ${REPO_ROOT} +mvn install + +cd ${REPO_ROOT}/examples +mvn package + +eval $(minikube docker-env) +docker build -t test/examples:1.0 . +kubectl apply -f test.yaml diff --git a/examples/examples-release-12/pom.xml b/examples/examples-release-12/pom.xml new file mode 100644 index 0000000000..a29b1d8038 --- /dev/null +++ b/examples/examples-release-12/pom.xml @@ -0,0 +1,91 @@ + + 4.0.0 + + + io.kubernetes + client-java-examples-parent + 1.0.0-SNAPSHOT + ../pom.xml + + + client-java-examples-release-12 + bundle + client-java-examples-release-12 + + + + io.prometheus + simpleclient + 0.9.0 + + + io.prometheus + simpleclient_httpserver + 0.9.0 + + + io.kubernetes + client-java-api + ${kubernetes.client.version} + + + io.kubernetes + client-java + ${kubernetes.client.version} + + + io.kubernetes + client-java-extended + ${kubernetes.client.version} + + + io.kubernetes + client-java-spring-integration + ${kubernetes.client.version} + + + io.kubernetes + client-java-proto + ${kubernetes.client.version} + + + commons-cli + commons-cli + + + io.kubernetes + client-java-cert-manager-models + 0.16.1-SNAPSHOT + + + io.kubernetes + client-java-prometheus-operator-models + 0.38.1-SNAPSHOT + + + + junit + junit + test + + + com.github.tomakehurst + wiremock + test + + + + org.springframework.boot + spring-boot-starter-web + ${spring.boot.version} + + + org.springframework.boot + spring-boot-starter-actuator + ${spring.boot.version} + + + + + + diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/AttachExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/AttachExample.java new file mode 100644 index 0000000000..7f753b8d8f --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/AttachExample.java @@ -0,0 +1,77 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.Attach; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.util.Config; +import io.kubernetes.client.util.Streams; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; + +/** + * A simple example of how to use the Java API + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.AttachExample" + * + *

From inside $REPO_DIR/examples + */ +public class AttachExample { + public static void main(String[] args) throws IOException, ApiException, InterruptedException { + ApiClient client = Config.defaultClient(); + Configuration.setDefaultApiClient(client); + + Attach attach = new Attach(); + final Attach.AttachResult result = attach.attach("default", "nginx-4217019353-k5sn9", true); + + new Thread( + new Runnable() { + public void run() { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + OutputStream output = result.getStandardInputStream(); + try { + while (true) { + String line = in.readLine(); + output.write(line.getBytes()); + output.write('\n'); + output.flush(); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + } + }) + .start(); + + new Thread( + new Runnable() { + public void run() { + try { + Streams.copy(result.getStandardOutputStream(), System.out); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + }) + .start(); + + Thread.sleep(10 * 1000); + result.close(); + System.exit(0); + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/CertManagerExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/CertManagerExample.java new file mode 100644 index 0000000000..b616a18ee9 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/CertManagerExample.java @@ -0,0 +1,45 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.cert.manager.models.V1alpha2IssuerSpecSelfSigned; +import io.cert.manager.models.V1beta1Issuer; +import io.cert.manager.models.V1beta1IssuerList; +import io.cert.manager.models.V1beta1IssuerSpec; +import io.kubernetes.client.openapi.models.V1ObjectMeta; +import io.kubernetes.client.util.ClientBuilder; +import io.kubernetes.client.util.generic.GenericKubernetesApi; +import java.io.IOException; + +/** + * This sample creates a self signed issuer "self-signed-issuer" in default namespace on a + * Kubernetes cluster where cert-manager is installed + */ +public class CertManagerExample { + public static void main(String[] args) throws IOException { + GenericKubernetesApi issuerApi = + new GenericKubernetesApi<>( + V1beta1Issuer.class, + V1beta1IssuerList.class, + "cert-manager.io", + "v1beta1", + "issuers", + ClientBuilder.defaultClient()); + issuerApi.create( + new V1beta1Issuer() + .metadata(new V1ObjectMeta().namespace("default").name("self-signed-issuer")) + .kind("Issuer") + .apiVersion("cert-manager.io/v1beta1") + .spec(new V1beta1IssuerSpec().selfSigned(new V1alpha2IssuerSpecSelfSigned()))); + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ControllerExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ControllerExample.java new file mode 100644 index 0000000000..fed0e544d3 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ControllerExample.java @@ -0,0 +1,163 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.extended.controller.Controller; +import io.kubernetes.client.extended.controller.ControllerManager; +import io.kubernetes.client.extended.controller.LeaderElectingController; +import io.kubernetes.client.extended.controller.builder.ControllerBuilder; +import io.kubernetes.client.extended.controller.reconciler.Reconciler; +import io.kubernetes.client.extended.controller.reconciler.Request; +import io.kubernetes.client.extended.controller.reconciler.Result; +import io.kubernetes.client.extended.event.EventType; +import io.kubernetes.client.extended.event.legacy.EventBroadcaster; +import io.kubernetes.client.extended.event.legacy.EventRecorder; +import io.kubernetes.client.extended.event.legacy.LegacyEventBroadcaster; +import io.kubernetes.client.extended.leaderelection.LeaderElectionConfig; +import io.kubernetes.client.extended.leaderelection.LeaderElector; +import io.kubernetes.client.extended.leaderelection.resourcelock.EndpointsLock; +import io.kubernetes.client.informer.SharedIndexInformer; +import io.kubernetes.client.informer.SharedInformerFactory; +import io.kubernetes.client.informer.cache.Lister; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.models.V1EventSource; +import io.kubernetes.client.openapi.models.V1Node; +import io.kubernetes.client.openapi.models.V1NodeList; +import io.kubernetes.client.util.CallGeneratorParams; +import java.io.IOException; +import java.time.Duration; +import java.util.concurrent.TimeUnit; +import okhttp3.OkHttpClient; + +public class ControllerExample { + public static void main(String[] args) throws IOException { + + CoreV1Api coreV1Api = new CoreV1Api(); + ApiClient apiClient = coreV1Api.getApiClient(); + OkHttpClient httpClient = + apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build(); + apiClient.setHttpClient(httpClient); + + // instantiating an informer-factory, and there should be only one informer-factory + // globally. + SharedInformerFactory informerFactory = new SharedInformerFactory(); + // registering node-informer into the informer-factory. + SharedIndexInformer nodeInformer = + informerFactory.sharedIndexInformerFor( + (CallGeneratorParams params) -> { + return coreV1Api.listNodeCall( + null, + null, + null, + null, + null, + null, + params.resourceVersion, + null, + params.timeoutSeconds, + params.watch, + null); + }, + V1Node.class, + V1NodeList.class); + informerFactory.startAllRegisteredInformers(); + + EventBroadcaster eventBroadcaster = new LegacyEventBroadcaster(coreV1Api); + + // nodeReconciler prints node information on events + NodePrintingReconciler nodeReconciler = + new NodePrintingReconciler( + nodeInformer, + eventBroadcaster.newRecorder( + new V1EventSource().host("localhost").component("node-printer"))); + + // Use builder library to construct a default controller. + Controller controller = + ControllerBuilder.defaultBuilder(informerFactory) + .watch( + (workQueue) -> + ControllerBuilder.controllerWatchBuilder(V1Node.class, workQueue) + .withWorkQueueKeyFunc( + (V1Node node) -> + new Request(node.getMetadata().getName())) // optional, default to + .withOnAddFilter( + (V1Node createdNode) -> + createdNode + .getMetadata() + .getName() + .startsWith("docker-")) // optional, set onAdd filter + .withOnUpdateFilter( + (V1Node oldNode, V1Node newNode) -> + newNode + .getMetadata() + .getName() + .startsWith("docker-")) // optional, set onUpdate filter + .withOnDeleteFilter( + (V1Node deletedNode, Boolean stateUnknown) -> + deletedNode + .getMetadata() + .getName() + .startsWith("docker-")) // optional, set onDelete filter + .build()) + .withReconciler(nodeReconciler) // required, set the actual reconciler + .withName("node-printing-controller") // optional, set name for controller + .withWorkerCount(4) // optional, set worker thread count + .withReadyFunc(nodeInformer::hasSynced) // optional, only starts controller when the + // cache has synced up + .build(); + + // Use builder library to manage one or multiple controllers. + ControllerManager controllerManager = + ControllerBuilder.controllerManagerBuilder(informerFactory) + .addController(controller) + .build(); + + LeaderElectingController leaderElectingController = + new LeaderElectingController( + new LeaderElector( + new LeaderElectionConfig( + new EndpointsLock("kube-system", "leader-election", "foo"), + Duration.ofMillis(10000), + Duration.ofMillis(8000), + Duration.ofMillis(5000))), + controllerManager); + + leaderElectingController.run(); + } + + static class NodePrintingReconciler implements Reconciler { + + private Lister nodeLister; + private EventRecorder eventRecorder; + + public NodePrintingReconciler( + SharedIndexInformer nodeInformer, EventRecorder recorder) { + this.nodeLister = new Lister<>(nodeInformer.getIndexer()); + this.eventRecorder = recorder; + } + + @Override + public Result reconcile(Request request) { + V1Node node = this.nodeLister.get(request.getName()); + System.out.println("triggered reconciling " + node.getMetadata().getName()); + this.eventRecorder.event( + node, + EventType.Normal, + "Print Node", + "Successfully printed %s", + node.getMetadata().getName()); + return new Result(false); + } + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/CopyExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/CopyExample.java new file mode 100644 index 0000000000..bbb6575676 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/CopyExample.java @@ -0,0 +1,51 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.Copy; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.util.Config; +import io.kubernetes.client.util.Streams; +import io.kubernetes.client.util.exception.CopyNotSupportedException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Paths; + +/** + * A simple example of how to use the Java API + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.CopyExample" + * + *

From inside $REPO_DIR/examples + */ +public class CopyExample { + public static void main(String[] args) + throws IOException, ApiException, InterruptedException, CopyNotSupportedException { + String podName = "kube-addon-manager-minikube"; + String namespace = "kube-system"; + + ApiClient client = Config.defaultClient(); + Configuration.setDefaultApiClient(client); + + Copy copy = new Copy(); + InputStream dataStream = copy.copyFileFromPod(namespace, podName, "/etc/motd"); + Streams.copy(dataStream, System.out); + + copy.copyDirectoryFromPod(namespace, podName, null, "/etc", Paths.get("/tmp/etc")); + + System.out.println("Done!"); + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/Example.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/Example.java new file mode 100644 index 0000000000..95d7fa51bc --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/Example.java @@ -0,0 +1,44 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.models.V1Pod; +import io.kubernetes.client.openapi.models.V1PodList; +import io.kubernetes.client.util.Config; +import java.io.IOException; + +/** + * A simple example of how to use the Java API + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.Example" + * + *

From inside $REPO_DIR/examples + */ +public class Example { + public static void main(String[] args) throws IOException, ApiException { + ApiClient client = Config.defaultClient(); + Configuration.setDefaultApiClient(client); + + CoreV1Api api = new CoreV1Api(); + V1PodList list = + api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null); + for (V1Pod item : list.getItems()) { + System.out.println(item.getMetadata().getName()); + } + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ExecExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ExecExample.java new file mode 100644 index 0000000000..acea8e815a --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ExecExample.java @@ -0,0 +1,96 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.Exec; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.util.Config; +import io.kubernetes.client.util.Streams; +import java.io.IOException; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; + +/** + * A simple example of how to use the Java API + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.ExecExample" + * + *

From inside $REPO_DIR/examples + */ +public class ExecExample { + public static void main(String[] args) + throws IOException, ApiException, InterruptedException, ParseException { + final Options options = new Options(); + options.addOption(new Option("p", "pod", true, "The name of the pod")); + options.addOption(new Option("n", "namespace", true, "The namespace of the pod")); + + CommandLineParser parser = new DefaultParser(); + CommandLine cmd = parser.parse(options, args); + + String podName = cmd.getOptionValue("p", "nginx-dbddb74b8-s4cx5"); + String namespace = cmd.getOptionValue("n", "default"); + args = cmd.getArgs(); + + ApiClient client = Config.defaultClient(); + Configuration.setDefaultApiClient(client); + + Exec exec = new Exec(); + boolean tty = System.console() != null; + // final Process proc = exec.exec("default", "nginx-4217019353-k5sn9", new String[] + // {"sh", "-c", "echo foo"}, true, tty); + final Process proc = + exec.exec(namespace, podName, args.length == 0 ? new String[] {"sh"} : args, true, tty); + + Thread in = + new Thread( + new Runnable() { + public void run() { + try { + Streams.copy(System.in, proc.getOutputStream()); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + }); + in.start(); + + Thread out = + new Thread( + new Runnable() { + public void run() { + try { + Streams.copy(proc.getInputStream(), System.out); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + }); + out.start(); + + proc.waitFor(); + + // wait for any last output; no need to wait for input thread + out.join(); + + proc.destroy(); + + System.exit(proc.exitValue()); + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ExpandedExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ExpandedExample.java new file mode 100644 index 0000000000..8a42363c42 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ExpandedExample.java @@ -0,0 +1,274 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.openapi.apis.AppsV1Api; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.models.V1Deployment; +import io.kubernetes.client.openapi.models.V1DeploymentList; +import io.kubernetes.client.openapi.models.V1DeploymentSpec; +import io.kubernetes.client.openapi.models.V1NamespaceList; +import io.kubernetes.client.openapi.models.V1PodList; +import io.kubernetes.client.openapi.models.V1ServiceList; +import io.kubernetes.client.util.Config; +import java.io.IOException; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A simple example of how to use the Java API + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.ExpandedExample" + * + *

From inside $REPO_DIR/examples + */ +public class ExpandedExample { + + private static CoreV1Api COREV1_API; + private static final String DEFAULT_NAME_SPACE = "default"; + private static final Integer TIME_OUT_VALUE = 180; + private static final Logger LOGGER = LoggerFactory.getLogger(ExpandedExample.class); + + /** + * Main method + * + * @param args + */ + public static void main(String[] args) { + try { + ApiClient client = Config.defaultClient(); + // To change the context of k8s cluster, you can use + // io.kubernetes.client.util.KubeConfig + Configuration.setDefaultApiClient(client); + COREV1_API = new CoreV1Api(client); + + // ScaleUp/ScaleDown the Deployment pod + // Please change the name of Deployment? + System.out.println("----- Scale Deployment Start -----"); + scaleDeployment("account-service", 5); + + // List all of the namaspaces and pods + List nameSpaces = getAllNameSpaces(); + nameSpaces.stream() + .forEach( + namespace -> { + try { + System.out.println("----- " + namespace + " -----"); + getNamespacedPod(namespace).stream().forEach(System.out::println); + } catch (ApiException ex) { + LOGGER.warn("Couldn't get the pods in namespace:" + namespace, ex); + } + }); + + // Print all of the Services + System.out.println("----- Print list all Services Start -----"); + List services = getServices(); + services.stream().forEach(System.out::println); + System.out.println("----- Print list all Services End -----"); + + // Print log of specific pod. In this example show the first pod logs. + System.out.println("----- Print Log of Specific Pod Start -----"); + String firstPodName = getPods().get(0); + printLog(DEFAULT_NAME_SPACE, firstPodName); + System.out.println("----- Print Log of Specific Pod End -----"); + } catch (ApiException | IOException ex) { + LOGGER.warn("Exception had occured ", ex); + } + } + + /** + * Get all namespaces in k8s cluster + * + * @return + * @throws ApiException + */ + public static List getAllNameSpaces() throws ApiException { + V1NamespaceList listNamespace = + COREV1_API.listNamespace( + "true", null, null, null, null, 0, null, null, Integer.MAX_VALUE, Boolean.FALSE); + List list = + listNamespace.getItems().stream() + .map(v1Namespace -> v1Namespace.getMetadata().getName()) + .collect(Collectors.toList()); + return list; + } + + /** + * List all pod names in all namespaces in k8s cluster + * + * @return + * @throws ApiException + */ + public static List getPods() throws ApiException { + V1PodList v1podList = + COREV1_API.listPodForAllNamespaces( + null, null, null, null, null, null, null, null, null, null); + List podList = + v1podList.getItems().stream() + .map(v1Pod -> v1Pod.getMetadata().getName()) + .collect(Collectors.toList()); + return podList; + } + + /** + * List all pod in the default namespace + * + * @return + * @throws ApiException + */ + public static List getNamespacedPod() throws ApiException { + return getNamespacedPod(DEFAULT_NAME_SPACE, null); + } + + /** + * List pod in specific namespace + * + * @param namespace + * @return + * @throws ApiException + */ + public static List getNamespacedPod(String namespace) throws ApiException { + return getNamespacedPod(namespace, null); + } + + /** + * List pod in specific namespace with label + * + * @param namespace + * @param label + * @return + * @throws ApiException + */ + public static List getNamespacedPod(String namespace, String label) throws ApiException { + V1PodList listNamespacedPod = + COREV1_API.listNamespacedPod( + namespace, + null, + null, + null, + null, + label, + Integer.MAX_VALUE, + null, + null, + TIME_OUT_VALUE, + Boolean.FALSE); + List listPods = + listNamespacedPod.getItems().stream() + .map(v1pod -> v1pod.getMetadata().getName()) + .collect(Collectors.toList()); + return listPods; + } + + /** + * List all Services in default namespace + * + * @return + * @throws ApiException + */ + public static List getServices() throws ApiException { + V1ServiceList listNamespacedService = + COREV1_API.listNamespacedService( + DEFAULT_NAME_SPACE, + null, + null, + null, + null, + null, + Integer.MAX_VALUE, + null, + null, + TIME_OUT_VALUE, + Boolean.FALSE); + return listNamespacedService.getItems().stream() + .map(v1service -> v1service.getMetadata().getName()) + .collect(Collectors.toList()); + } + + /** + * Scale up/down the number of pod in Deployment + * + * @param deploymentName + * @param numberOfReplicas + * @throws ApiException + */ + public static void scaleDeployment(String deploymentName, int numberOfReplicas) + throws ApiException { + AppsV1Api appsV1Api = new AppsV1Api(); + appsV1Api.setApiClient(COREV1_API.getApiClient()); + V1DeploymentList listNamespacedDeployment = + appsV1Api.listNamespacedDeployment( + DEFAULT_NAME_SPACE, + null, + null, + null, + null, + null, + null, + null, + null, + null, + Boolean.FALSE); + + List appsV1DeploymentItems = listNamespacedDeployment.getItems(); + Optional findedDeployment = + appsV1DeploymentItems.stream() + .filter( + (V1Deployment deployment) -> + deployment.getMetadata().getName().equals(deploymentName)) + .findFirst(); + findedDeployment.ifPresent( + (V1Deployment deploy) -> { + try { + V1DeploymentSpec newSpec = deploy.getSpec().replicas(numberOfReplicas); + V1Deployment newDeploy = deploy.spec(newSpec); + appsV1Api.replaceNamespacedDeployment( + deploymentName, DEFAULT_NAME_SPACE, newDeploy, null, null, null); + } catch (ApiException ex) { + LOGGER.warn("Scale the pod failed for Deployment:" + deploymentName, ex); + } + }); + } + + /** + * Print out the Log for specific Pods + * + * @param namespace + * @param podName + * @throws ApiException + */ + public static void printLog(String namespace, String podName) throws ApiException { + // https://github.com/kubernetes-client/java/blob/master/kubernetes/docs/CoreV1Api.md#readNamespacedPodLog + String readNamespacedPodLog = + COREV1_API.readNamespacedPodLog( + podName, + namespace, + null, + Boolean.FALSE, + null, + Integer.MAX_VALUE, + null, + Boolean.FALSE, + Integer.MAX_VALUE, + 40, + Boolean.FALSE); + System.out.println(readNamespacedPodLog); + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/FluentExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/FluentExample.java new file mode 100644 index 0000000000..5eac0de017 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/FluentExample.java @@ -0,0 +1,75 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.models.V1Container; +import io.kubernetes.client.openapi.models.V1ObjectMeta; +import io.kubernetes.client.openapi.models.V1Pod; +import io.kubernetes.client.openapi.models.V1PodBuilder; +import io.kubernetes.client.openapi.models.V1PodList; +import io.kubernetes.client.openapi.models.V1PodSpec; +import io.kubernetes.client.util.Config; +import java.io.IOException; +import java.util.Arrays; + +/** + * A simple example of how to use the Java API + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.FluentExample" + * + *

From inside $REPO_DIR/examples + */ +public class FluentExample { + public static void main(String[] args) throws IOException, ApiException { + ApiClient client = Config.defaultClient(); + Configuration.setDefaultApiClient(client); + + CoreV1Api api = new CoreV1Api(); + + V1Pod pod = + new V1PodBuilder() + .withNewMetadata() + .withName("apod") + .endMetadata() + .withNewSpec() + .addNewContainer() + .withName("www") + .withImage("nginx") + .endContainer() + .endSpec() + .build(); + + api.createNamespacedPod("default", pod, null, null, null); + + V1Pod pod2 = + new V1Pod() + .metadata(new V1ObjectMeta().name("anotherpod")) + .spec( + new V1PodSpec() + .containers(Arrays.asList(new V1Container().name("www").image("nginx")))); + + api.createNamespacedPod("default", pod2, null, null, null); + + V1PodList list = + api.listNamespacedPod( + "default", null, null, null, null, null, null, null, null, null, null); + for (V1Pod item : list.getItems()) { + System.out.println(item.getMetadata().getName()); + } + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/GenericClientExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/GenericClientExample.java new file mode 100644 index 0000000000..ddfb1243b8 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/GenericClientExample.java @@ -0,0 +1,63 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.custom.V1Patch; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.models.V1Container; +import io.kubernetes.client.openapi.models.V1ObjectMeta; +import io.kubernetes.client.openapi.models.V1Pod; +import io.kubernetes.client.openapi.models.V1PodList; +import io.kubernetes.client.openapi.models.V1PodSpec; +import io.kubernetes.client.util.ClientBuilder; +import io.kubernetes.client.util.generic.GenericKubernetesApi; +import java.util.Arrays; + +public class GenericClientExample { + + public static void main(String[] args) throws Exception { + + // The following codes demonstrates using generic client to manipulate pods + V1Pod pod = + new V1Pod() + .metadata(new V1ObjectMeta().name("foo").namespace("default")) + .spec( + new V1PodSpec() + .containers(Arrays.asList(new V1Container().name("c").image("test")))); + + ApiClient apiClient = ClientBuilder.standard().build(); + GenericKubernetesApi podClient = + new GenericKubernetesApi<>(V1Pod.class, V1PodList.class, "", "v1", "pods", apiClient); + + V1Pod latestPod = podClient.create(pod).throwsApiException().getObject(); + System.out.println("Created!"); + + V1Pod patchedPod = + podClient + .patch( + "default", + "foo", + V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, + new V1Patch("{\"metadata\":{\"finalizers\":[\"example.io/foo\"]}}")) + .throwsApiException() + .getObject(); + System.out.println("Patched!"); + + V1Pod deletedPod = podClient.delete("default", "foo").throwsApiException().getObject(); + if (deletedPod != null) { + System.out.println( + "Received after-deletion status of the requested object, will be deleting in background!"); + } + System.out.println("Deleted!"); + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/InClusterClientExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/InClusterClientExample.java new file mode 100644 index 0000000000..7ab705b440 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/InClusterClientExample.java @@ -0,0 +1,58 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.models.V1Pod; +import io.kubernetes.client.openapi.models.V1PodList; +import io.kubernetes.client.util.ClientBuilder; +import java.io.IOException; + +/** + * A simple example of how to use the Java API inside a kubernetes cluster + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.InClusterClientExample" + * + *

From inside $REPO_DIR/examples + */ +public class InClusterClientExample { + public static void main(String[] args) throws IOException, ApiException { + + // loading the in-cluster config, including: + // 1. service-account CA + // 2. service-account bearer-token + // 3. service-account namespace + // 4. master endpoints(ip, port) from pre-set environment variables + ApiClient client = ClientBuilder.cluster().build(); + + // if you prefer not to refresh service account token, please use: + // ApiClient client = ClientBuilder.oldCluster().build(); + + // set the global default api-client to the in-cluster one from above + Configuration.setDefaultApiClient(client); + + // the CoreV1Api loads default api-client from global configuration. + CoreV1Api api = new CoreV1Api(); + + // invokes the CoreV1Api client + V1PodList list = + api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null); + for (V1Pod item : list.getItems()) { + System.out.println(item.getMetadata().getName()); + } + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/InformerExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/InformerExample.java new file mode 100644 index 0000000000..5a7ff776b1 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/InformerExample.java @@ -0,0 +1,102 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.informer.ResourceEventHandler; +import io.kubernetes.client.informer.SharedIndexInformer; +import io.kubernetes.client.informer.SharedInformerFactory; +import io.kubernetes.client.informer.cache.Lister; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.models.V1Node; +import io.kubernetes.client.openapi.models.V1NodeList; +import io.kubernetes.client.openapi.models.V1ObjectMeta; +import io.kubernetes.client.util.CallGeneratorParams; +import java.util.concurrent.TimeUnit; +import okhttp3.OkHttpClient; + +/** + * A simple example of how to use the Java API + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.InformerExample" + * + *

From inside $REPO_DIR/examples + */ +public class InformerExample { + public static void main(String[] args) throws Exception { + CoreV1Api coreV1Api = new CoreV1Api(); + ApiClient apiClient = coreV1Api.getApiClient(); + OkHttpClient httpClient = + apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build(); + apiClient.setHttpClient(httpClient); + + SharedInformerFactory factory = new SharedInformerFactory(); + + // Node informer + SharedIndexInformer nodeInformer = + factory.sharedIndexInformerFor( + (CallGeneratorParams params) -> { + return coreV1Api.listNodeCall( + null, + null, + null, + null, + null, + null, + params.resourceVersion, + null, + params.timeoutSeconds, + params.watch, + null); + }, + V1Node.class, + V1NodeList.class); + + nodeInformer.addEventHandler( + new ResourceEventHandler() { + @Override + public void onAdd(V1Node node) { + System.out.printf("%s node added!\n", node.getMetadata().getName()); + } + + @Override + public void onUpdate(V1Node oldNode, V1Node newNode) { + System.out.printf( + "%s => %s node updated!\n", + oldNode.getMetadata().getName(), newNode.getMetadata().getName()); + } + + @Override + public void onDelete(V1Node node, boolean deletedFinalStateUnknown) { + System.out.printf("%s node deleted!\n", node.getMetadata().getName()); + } + }); + + factory.startAllRegisteredInformers(); + + V1Node nodeToCreate = new V1Node(); + V1ObjectMeta metadata = new V1ObjectMeta(); + metadata.setName("noxu"); + nodeToCreate.setMetadata(metadata); + V1Node createdNode = coreV1Api.createNode(nodeToCreate, null, null, null); + Thread.sleep(3000); + + Lister nodeLister = new Lister(nodeInformer.getIndexer()); + V1Node node = nodeLister.get("noxu"); + System.out.printf("noxu created! %s\n", node.getMetadata().getCreationTimestamp()); + factory.stopAllRegisteredInformers(); + Thread.sleep(3000); + System.out.println("informer stopped.."); + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/KubeConfigFileClientExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/KubeConfigFileClientExample.java new file mode 100644 index 0000000000..c4b15401b0 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/KubeConfigFileClientExample.java @@ -0,0 +1,58 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.models.V1Pod; +import io.kubernetes.client.openapi.models.V1PodList; +import io.kubernetes.client.util.ClientBuilder; +import io.kubernetes.client.util.KubeConfig; +import java.io.FileReader; +import java.io.IOException; + +/** + * A simple example of how to use the Java API from an application outside a kubernetes cluster + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.KubeConfigFileClientExample" + * + *

From inside $REPO_DIR/examples + */ +public class KubeConfigFileClientExample { + public static void main(String[] args) throws IOException, ApiException { + + // file path to your KubeConfig + + String kubeConfigPath = System.getenv("HOME") + "/.kube/config"; + + // loading the out-of-cluster config, a kubeconfig from file-system + ApiClient client = + ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build(); + + // set the global default api-client to the in-cluster one from above + Configuration.setDefaultApiClient(client); + + // the CoreV1Api loads default api-client from global configuration. + CoreV1Api api = new CoreV1Api(); + + // invokes the CoreV1Api client + V1PodList list = + api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null); + for (V1Pod item : list.getItems()) { + System.out.println(item.getMetadata().getName()); + } + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/KubectlExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/KubectlExample.java new file mode 100644 index 0000000000..2619c9f271 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/KubectlExample.java @@ -0,0 +1,311 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import static io.kubernetes.client.extended.kubectl.Kubectl.apiResources; +import static io.kubernetes.client.extended.kubectl.Kubectl.copy; +import static io.kubernetes.client.extended.kubectl.Kubectl.cordon; +import static io.kubernetes.client.extended.kubectl.Kubectl.delete; +import static io.kubernetes.client.extended.kubectl.Kubectl.drain; +import static io.kubernetes.client.extended.kubectl.Kubectl.exec; +import static io.kubernetes.client.extended.kubectl.Kubectl.label; +import static io.kubernetes.client.extended.kubectl.Kubectl.log; +import static io.kubernetes.client.extended.kubectl.Kubectl.portforward; +import static io.kubernetes.client.extended.kubectl.Kubectl.scale; +import static io.kubernetes.client.extended.kubectl.Kubectl.taint; +import static io.kubernetes.client.extended.kubectl.Kubectl.top; +import static io.kubernetes.client.extended.kubectl.Kubectl.uncordon; +import static io.kubernetes.client.extended.kubectl.Kubectl.version; +import static io.kubernetes.client.extended.kubectl.KubectlTop.podMetricSum; + +import io.kubernetes.client.common.KubernetesObject; +import io.kubernetes.client.custom.NodeMetrics; +import io.kubernetes.client.custom.PodMetrics; +import io.kubernetes.client.extended.kubectl.KubectlExec; +import io.kubernetes.client.extended.kubectl.KubectlPortForward; +import io.kubernetes.client.extended.kubectl.exception.KubectlException; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.models.V1Deployment; +import io.kubernetes.client.openapi.models.V1Node; +import io.kubernetes.client.openapi.models.V1Pod; +import io.kubernetes.client.openapi.models.V1ReplicationController; +import io.kubernetes.client.openapi.models.V1Service; +import io.kubernetes.client.util.Config; +import io.kubernetes.client.util.Streams; +import java.util.Arrays; +import java.util.List; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.lang3.tuple.Pair; + +/** + * A Java equivalent for the kubectl command line tool. Not nearly as complete. + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.KubectlExample" -Dexec.args="log some-pod" + * + *

From inside $REPO_DIR/examples + */ +public class KubectlExample { + static Class getClassForKind(String kind) { + switch (kind) { + case "pod": + case "pods": + return V1Pod.class; + case "deployment": + case "deployments": + return V1Deployment.class; + case "service": + case "services": + return V1Service.class; + case "node": + case "nodes": + return V1Node.class; + case "replicationcontroller": + case "replicationcontrollers": + return V1ReplicationController.class; + } + return null; + } + + private static final String PADDING = " "; + + private static String pad(String value) { + while (value.length() < PADDING.length()) { + value += " "; + } + return value; + } + + 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> nodes = + top(V1Node.class, NodeMetrics.class).apiClient(client).metric("cpu").execute(); + System.out.println(pad("Node") + "\tCPU\t\tMemory"); + for (Pair 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> pods = + top(V1Pod.class, PodMetrics.class) + .apiClient(client) + .namespace(ns) + .metric("cpu") + .execute(); + System.out.println(pad("Pod") + "\tCPU\t\tMemory"); + for (Pair 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 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 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); + } + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/LeaderElectionExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/LeaderElectionExample.java new file mode 100644 index 0000000000..0e48689602 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/LeaderElectionExample.java @@ -0,0 +1,56 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.extended.leaderelection.LeaderElectionConfig; +import io.kubernetes.client.extended.leaderelection.LeaderElector; +import io.kubernetes.client.extended.leaderelection.resourcelock.EndpointsLock; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.util.Config; +import java.time.Duration; +import java.util.UUID; + +/** + * A simple example of how to use the Java API + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.LeaderElectionExample" + * + *

From inside $REPO_DIR/examples + */ +public class LeaderElectionExample { + public static void main(String[] args) throws Exception { + ApiClient client = Config.defaultClient(); + Configuration.setDefaultApiClient(client); + + // New + String appNamespace = "default"; + String appName = "leader-election-foobar"; + String lockHolderIdentityName = UUID.randomUUID().toString(); // Anything unique + EndpointsLock lock = new EndpointsLock(appNamespace, appName, lockHolderIdentityName); + + LeaderElectionConfig leaderElectionConfig = + new LeaderElectionConfig( + lock, Duration.ofMillis(10000), Duration.ofMillis(8000), Duration.ofMillis(2000)); + try (LeaderElector leaderElector = new LeaderElector(leaderElectionConfig)) { + leaderElector.run( + () -> { + System.out.println("Do something when getting leadership."); + }, + () -> { + System.out.println("Do something when losing leadership."); + }); + } + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/LogsExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/LogsExample.java new file mode 100644 index 0000000000..dbfa157eea --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/LogsExample.java @@ -0,0 +1,51 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.PodLogs; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.models.V1Pod; +import io.kubernetes.client.util.Config; +import io.kubernetes.client.util.Streams; +import java.io.IOException; +import java.io.InputStream; + +/** + * A simple example of how to use the Java API + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.LogsExample" + * + *

From inside $REPO_DIR/examples + */ +public class LogsExample { + public static void main(String[] args) throws IOException, ApiException, InterruptedException { + ApiClient client = Config.defaultClient(); + Configuration.setDefaultApiClient(client); + CoreV1Api coreApi = new CoreV1Api(client); + + PodLogs logs = new PodLogs(); + V1Pod pod = + coreApi + .listNamespacedPod( + "default", "false", null, null, null, null, null, null, null, null, null) + .getItems() + .get(0); + + InputStream is = logs.streamNamespacedPodLog(pod); + Streams.copy(is, System.out); + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/MetricsExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/MetricsExample.java new file mode 100644 index 0000000000..0d8c10e30b --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/MetricsExample.java @@ -0,0 +1,68 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.Metrics; +import io.kubernetes.client.custom.ContainerMetrics; +import io.kubernetes.client.custom.NodeMetrics; +import io.kubernetes.client.custom.NodeMetricsList; +import io.kubernetes.client.custom.PodMetrics; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.util.Config; +import java.io.IOException; + +/** + * A simple example of how to use the Java API + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.MetricsExample" + * + *

From inside $REPO_DIR/examples + */ +public class MetricsExample { + public static void main(String[] args) throws IOException, ApiException { + ApiClient client = Config.defaultClient(); + Configuration.setDefaultApiClient(client); + + Metrics metrics = new Metrics(client); + NodeMetricsList list = metrics.getNodeMetrics(); + for (NodeMetrics item : list.getItems()) { + System.out.println(item.getMetadata().getName()); + System.out.println("------------------------------"); + for (String key : item.getUsage().keySet()) { + System.out.println("\t" + key); + System.out.println("\t" + item.getUsage().get(key)); + } + System.out.println(); + } + + for (PodMetrics item : metrics.getPodMetrics("default").getItems()) { + System.out.println(item.getMetadata().getName()); + System.out.println("------------------------------"); + if (item.getContainers() == null) { + continue; + } + for (ContainerMetrics container : item.getContainers()) { + System.out.println(container.getName()); + System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"); + for (String key : container.getUsage().keySet()) { + System.out.println("\t" + key); + System.out.println("\t" + container.getUsage().get(key)); + } + System.out.println(); + } + } + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PagerExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PagerExample.java new file mode 100644 index 0000000000..b0a4ac4fa4 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PagerExample.java @@ -0,0 +1,72 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.extended.pager.Pager; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.models.V1Namespace; +import io.kubernetes.client.openapi.models.V1NamespaceList; +import io.kubernetes.client.util.Config; +import java.io.IOException; +import java.util.concurrent.TimeUnit; +import okhttp3.OkHttpClient; + +/** + * A simple example of how to use the Java API + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.PagerExample" + * + *

From inside $REPO_DIR/examples + */ +public class PagerExample { + public static void main(String[] args) throws IOException { + + ApiClient client = Config.defaultClient(); + OkHttpClient httpClient = + client.getHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); + client.setHttpClient(httpClient); + Configuration.setDefaultApiClient(client); + CoreV1Api api = new CoreV1Api(); + int i = 0; + Pager pager = + new Pager( + (Pager.PagerParams param) -> { + try { + return api.listNamespaceCall( + null, + null, + param.getContinueToken(), + null, + null, + param.getLimit(), + null, + null, + 1, + null, + null); + } catch (Exception e) { + throw new RuntimeException(e); + } + }, + client, + 10, + V1NamespaceList.class); + for (V1Namespace namespace : pager) { + System.out.println(namespace.getMetadata().getName()); + } + System.out.println("------------------"); + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ParseExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ParseExample.java new file mode 100644 index 0000000000..92866a46fe --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ParseExample.java @@ -0,0 +1,64 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.util.Config; +import java.io.FileReader; +import java.io.IOException; +import java.io.StringReader; + +/** + * A simple example of how to parse a Kubernetes object. + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.ParseExample" + * + *

From inside $REPO_DIR/examples + */ +public class ParseExample { + public static void main(String[] args) throws IOException, ApiException, ClassNotFoundException { + if (args.length < 2) { + System.err.println("Usage: ParseExample "); + System.exit(1); + } + ApiClient client = Config.defaultClient(); + FileReader json = new FileReader(args[0]); + Object obj = + client + .getJSON() + .getGson() + .fromJson(json, Class.forName("io.kubernetes.client.models." + args[1])); + + String output = client.getJSON().getGson().toJson(obj); + + // Test round tripping... + Object obj2 = + client + .getJSON() + .getGson() + .fromJson( + new StringReader(output), Class.forName("io.kubernetes.client.models." + args[1])); + + String output2 = client.getJSON().getGson().toJson(obj2); + + // Validate round trip + if (!output.equals(output2)) { + System.err.println("Error, expected:\n" + output + "\nto equal\n" + output2); + System.exit(2); + } + + System.out.println(output); + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PatchExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PatchExample.java new file mode 100644 index 0000000000..603b663636 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PatchExample.java @@ -0,0 +1,126 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.custom.V1Patch; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.openapi.apis.AppsV1Api; +import io.kubernetes.client.openapi.models.V1Deployment; +import io.kubernetes.client.util.ClientBuilder; +import io.kubernetes.client.util.PatchUtils; +import java.io.IOException; + +/** + * A simple Example of how to use the Java API.
+ * This example demonstrates patching of deployment using Json Patch and Strategic Merge Patch.
+ * For generating Json Patches, refer http://jsonpatch.com. For + * generating Strategic Merge Patches, refer strategic-merge-patch.md. + * + *

    + *
  • Creates deployment hello-node with terminationGracePeriodSeconds value as 30 and a + * finalizer. + *
  • Json-Patches deployment hello-node with terminationGracePeriodSeconds value as 27. + *
  • Strategic-Merge-Patches deployment hello-node removing the finalizer. + *
+ * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.PatchExample" + * + *

From inside $REPO_DIR/examples + */ +public class PatchExample { + static String jsonPatchStr = + "[{\"op\":\"replace\",\"path\":\"/spec/template/spec/terminationGracePeriodSeconds\",\"value\":27}]"; + static String strategicMergePatchStr = + "{\"metadata\":{\"$deleteFromPrimitiveList/finalizers\":[\"example.com/test\"]}}"; + static String jsonDeploymentStr = + "{\"kind\":\"Deployment\",\"apiVersion\":\"apps/v1\",\"metadata\":{\"name\":\"hello-node\",\"finalizers\":[\"example.com/test\"],\"labels\":{\"run\":\"hello-node\"}},\"spec\":{\"replicas\":1,\"selector\":{\"matchLabels\":{\"run\":\"hello-node\"}},\"template\":{\"metadata\":{\"creationTimestamp\":null,\"labels\":{\"run\":\"hello-node\"}},\"spec\":{\"terminationGracePeriodSeconds\":30,\"containers\":[{\"name\":\"hello-node\",\"image\":\"hello-node:v1\",\"ports\":[{\"containerPort\":8080,\"protocol\":\"TCP\"}],\"resources\":{}}]}},\"strategy\":{}},\"status\":{}}"; + static String applyYamlStr = + "{\"kind\":\"Deployment\",\"apiVersion\":\"apps/v1\",\"metadata\":{\"name\":\"hello-node\",\"finalizers\":[\"example.com/test\"],\"labels\":{\"run\":\"hello-node\"}},\"spec\":{\"replicas\":1,\"selector\":{\"matchLabels\":{\"run\":\"hello-node\"}},\"template\":{\"metadata\":{\"creationTimestamp\":null,\"labels\":{\"run\":\"hello-node\"}},\"spec\":{\"terminationGracePeriodSeconds\":30,\"containers\":[{\"name\":\"hello-node\",\"image\":\"hello-node:v2\",\"ports\":[{\"containerPort\":8080,\"protocol\":\"TCP\"}],\"resources\":{}}]}},\"strategy\":{}},\"status\":{}}"; + + public static void main(String[] args) throws IOException { + try { + AppsV1Api api = new AppsV1Api(ClientBuilder.standard().build()); + V1Deployment body = + Configuration.getDefaultApiClient() + .getJSON() + .deserialize(jsonDeploymentStr, V1Deployment.class); + + // create a deployment + V1Deployment deploy1 = api.createNamespacedDeployment("default", body, null, null, null); + System.out.println("original deployment" + deploy1); + + // json-patch a deployment + V1Deployment deploy2 = + PatchUtils.patch( + V1Deployment.class, + () -> + api.patchNamespacedDeploymentCall( + "hello-node", + "default", + new V1Patch(jsonPatchStr), + null, + null, + null, // field-manager is optional + null, + null), + V1Patch.PATCH_FORMAT_JSON_PATCH, + api.getApiClient()); + System.out.println("json-patched deployment" + deploy2); + + // strategic-merge-patch a deployment + V1Deployment deploy3 = + PatchUtils.patch( + V1Deployment.class, + () -> + api.patchNamespacedDeploymentCall( + "hello-node", + "default", + new V1Patch(strategicMergePatchStr), + null, + null, + null, // field-manager is optional + null, + null), + V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, + api.getApiClient()); + System.out.println("strategic-merge-patched deployment" + deploy3); + + // apply-yaml a deployment, server side apply is available by default after kubernetes v1.16 + // or opt-in by turning on the feature gate for v1.14 or v1.15. + // https://kubernetes.io/docs/reference/using-api/api-concepts/#server-side-apply + V1Deployment deploy4 = + PatchUtils.patch( + V1Deployment.class, + () -> + api.patchNamespacedDeploymentCall( + "hello-node", + "default", + new V1Patch(applyYamlStr), + null, + null, + "example-field-manager", // field-manager is required for server-side apply + true, + null), + V1Patch.PATCH_FORMAT_APPLY_YAML, + api.getApiClient()); + System.out.println("application/apply-patch+yaml deployment" + deploy4); + + } catch (ApiException e) { + System.out.println(e.getResponseBody()); + e.printStackTrace(); + } + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PortForwardExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PortForwardExample.java new file mode 100644 index 0000000000..cbe064db31 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PortForwardExample.java @@ -0,0 +1,87 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.PortForward; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.util.Config; +import io.kubernetes.client.util.Streams; +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.ArrayList; +import java.util.List; + +/** + * A simple example of how to use the Java API + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.PortForwardExample" from inside + * $REPO_DIR/examples + * + *

Then: curl localhost:8080 from a different terminal (but be quick about it, the socket times + * out pretty fast...) + */ +public class PortForwardExample { + public static void main(String[] args) throws IOException, ApiException, InterruptedException { + ApiClient client = Config.defaultClient(); + Configuration.setDefaultApiClient(client); + + PortForward forward = new PortForward(); + List ports = new ArrayList<>(); + int localPort = 8080; + int targetPort = 8080; + ports.add(targetPort); + final PortForward.PortForwardResult result = + forward.forward("default", "camera-viz-7949dbf7c6-lpxkd", ports); + System.out.println("Forwarding!"); + ServerSocket ss = new ServerSocket(localPort); + + final Socket s = ss.accept(); + System.out.println("Connected!"); + + new Thread( + new Runnable() { + public void run() { + try { + Streams.copy(result.getInputStream(targetPort), s.getOutputStream()); + } catch (IOException ex) { + ex.printStackTrace(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + }) + .start(); + + new Thread( + new Runnable() { + public void run() { + try { + Streams.copy(s.getInputStream(), result.getOutboundStream(targetPort)); + } catch (IOException ex) { + ex.printStackTrace(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + }) + .start(); + + Thread.sleep(10 * 1000); + + System.exit(0); + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PromOpExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PromOpExample.java new file mode 100644 index 0000000000..65f38b8ce4 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PromOpExample.java @@ -0,0 +1,43 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import com.coreos.monitoring.models.V1Prometheus; +import com.coreos.monitoring.models.V1PrometheusList; +import com.coreos.monitoring.models.V1PrometheusSpec; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.models.V1ObjectMeta; +import io.kubernetes.client.util.ClientBuilder; +import io.kubernetes.client.util.generic.GenericKubernetesApi; +import java.io.IOException; + +public class PromOpExample { + public static void main(String[] args) throws IOException, ApiException { + GenericKubernetesApi prometheusApi = + new GenericKubernetesApi<>( + V1Prometheus.class, + V1PrometheusList.class, + "monitoring.coreos.com", + "v1", + "prometheuses", + ClientBuilder.defaultClient()); + prometheusApi + .create( + new V1Prometheus() + .metadata(new V1ObjectMeta().namespace("default").name("my-prometheus")) + .kind("Prometheus") + .apiVersion("monitoring.coreos.com/v1") + .spec(new V1PrometheusSpec())) + .throwsApiException(); + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PrometheusExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PrometheusExample.java new file mode 100644 index 0000000000..3d857e5166 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/PrometheusExample.java @@ -0,0 +1,66 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.monitoring.Monitoring; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.models.V1Pod; +import io.kubernetes.client.openapi.models.V1PodList; +import io.kubernetes.client.util.Config; +import java.io.IOException; + +/** + * A simple example of how to use the Java API with Prometheus metrics + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.PrometheusExample" + * + *

From inside $REPO_DIR/examples + */ +public class PrometheusExample { + public static void main(String[] args) throws IOException, ApiException { + ApiClient client = Config.defaultClient(); + Configuration.setDefaultApiClient(client); + + // Install an HTTP Interceptor that adds metrics + Monitoring.installMetrics(client); + + // Install a simple HTTP server to serve prometheus metrics. If you already are serving + // metrics elsewhere, this is unnecessary. + Monitoring.startMetricsServer("localhost", 8080); + + CoreV1Api api = new CoreV1Api(); + + while (true) { + // A request that should return 200 + V1PodList list = + api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null); + // A request that should return 404 + try { + V1Pod pod = api.readNamespacedPod("foo", "bar", null, null, null); + } catch (ApiException ex) { + if (ex.getCode() != 404) { + throw ex; + } + } + try { + Thread.sleep(10000); + } catch (InterruptedException ex) { + ex.printStackTrace(); + } + } + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ProtoExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ProtoExample.java new file mode 100644 index 0000000000..e4e05a2aa1 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/ProtoExample.java @@ -0,0 +1,69 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.ProtoClient; +import io.kubernetes.client.ProtoClient.ObjectOrStatus; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.proto.Meta.ObjectMeta; +import io.kubernetes.client.proto.V1.Namespace; +import io.kubernetes.client.proto.V1.NamespaceSpec; +import io.kubernetes.client.proto.V1.Pod; +import io.kubernetes.client.proto.V1.PodList; +import io.kubernetes.client.util.Config; +import java.io.IOException; + +/** + * A simple example of how to use the Java API + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.ProtoExample" + * + *

From inside $REPO_DIR/examples + */ +public class ProtoExample { + public static void main(String[] args) throws IOException, ApiException, InterruptedException { + ApiClient client = Config.defaultClient(); + Configuration.setDefaultApiClient(client); + + ProtoClient pc = new ProtoClient(client); + ObjectOrStatus list = pc.list(PodList.newBuilder(), "/api/v1/namespaces/default/pods"); + + if (list.object.getItemsCount() > 0) { + Pod p = list.object.getItems(0); + System.out.println(p); + } + + Namespace namespace = + Namespace.newBuilder().setMetadata(ObjectMeta.newBuilder().setName("test").build()).build(); + + ObjectOrStatus ns = pc.create(namespace, "/api/v1/namespaces", "v1", "Namespace"); + System.out.println(ns); + if (ns.object != null) { + namespace = + ns.object + .toBuilder() + .setSpec(NamespaceSpec.newBuilder().addFinalizers("test").build()) + .build(); + // This is how you would update an object, but you can't actually + // update namespaces, so this returns a 405 + ns = pc.update(namespace, "/api/v1/namespaces/test", "v1", "Namespace"); + System.out.println(ns.status); + } + + ns = pc.delete(Namespace.newBuilder(), "/api/v1/namespaces/test"); + System.out.println(ns); + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/SpringControllerExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/SpringControllerExample.java new file mode 100644 index 0000000000..e968b6e3c4 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/SpringControllerExample.java @@ -0,0 +1,149 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.extended.controller.Controller; +import io.kubernetes.client.extended.controller.reconciler.Reconciler; +import io.kubernetes.client.extended.controller.reconciler.Request; +import io.kubernetes.client.extended.controller.reconciler.Result; +import io.kubernetes.client.informer.SharedInformer; +import io.kubernetes.client.informer.SharedInformerFactory; +import io.kubernetes.client.informer.cache.Lister; +import io.kubernetes.client.openapi.models.V1Endpoints; +import io.kubernetes.client.openapi.models.V1EndpointsList; +import io.kubernetes.client.openapi.models.V1Node; +import io.kubernetes.client.openapi.models.V1NodeList; +import io.kubernetes.client.openapi.models.V1Pod; +import io.kubernetes.client.openapi.models.V1PodList; +import io.kubernetes.client.spring.extended.controller.annotation.GroupVersionResource; +import io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformer; +import io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformers; +import io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconciler; +import io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerReadyFunc; +import io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatch; +import io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatches; +import io.kubernetes.client.spring.extended.controller.factory.KubernetesControllerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +@SpringBootApplication +public class SpringControllerExample { + + public static void main(String[] args) { + SpringApplication.run(SpringControllerExample.class, args); + } + + @Configuration + public static class AppConfig { + + @Bean + public CommandLineRunner commandLineRunner( + SharedInformerFactory sharedInformerFactory, + @Qualifier("node-printing-controller") Controller nodePrintingController) { + return args -> { + System.out.println("starting informers.."); + sharedInformerFactory.startAllRegisteredInformers(); + + System.out.println("running controller.."); + nodePrintingController.run(); + }; + } + + // *REQUIRED* + // factorybean to crete controller + @Bean("node-printing-controller") + public KubernetesControllerFactory kubernetesReconcilerConfigurer( + SharedInformerFactory sharedInformerFactory, Reconciler reconciler) { + return new KubernetesControllerFactory(sharedInformerFactory, reconciler); + } + + // *REQUIRED* + // Injecting your SharedInformerFactory class annotated `@KubernetesInformers` + @Bean("sharedInformerFactory") + public SharedInformerFactory sharedInformerFactory() { + return new MySharedInformerFactory(); + } + } + + @KubernetesInformers({ // Defining what resources is the informer-factory actually watching. + @KubernetesInformer( + apiTypeClass = V1Endpoints.class, + apiListTypeClass = V1EndpointsList.class, + groupVersionResource = + @GroupVersionResource(apiGroup = "", apiVersion = "v1", resourcePlural = "endpoints")), + @KubernetesInformer( + apiTypeClass = V1Node.class, + apiListTypeClass = V1NodeList.class, + groupVersionResource = + @GroupVersionResource(apiGroup = "", apiVersion = "v1", resourcePlural = "nodes"), + resyncPeriodMillis = 60 * 1000L), + @KubernetesInformer( + apiTypeClass = V1Pod.class, + apiListTypeClass = V1PodList.class, + groupVersionResource = + @GroupVersionResource(apiGroup = "", apiVersion = "v1", resourcePlural = "pods")), + }) + public static class MySharedInformerFactory extends SharedInformerFactory {} + + // As long as a reconciler bean attached `@KubernetesReconciler` detected in the context, we + // will + // be automatically creating a conresponding controller bean implementing {@link + // io.kubernetes.client.extended.controller.Controller} + // with the name specified and registering it to the spring bean-factory. + @KubernetesReconciler( + watches = + @KubernetesReconcilerWatches({ + @KubernetesReconcilerWatch( + apiTypeClass = V1Node.class, + resyncPeriodMillis = 60 * 1000L // fully resync every 1 minute + ), + })) + @Component + public static class NodePrintingReconciler implements Reconciler { + + @Value("${namespace}") + private String namespace; + + @Autowired private SharedInformer nodeInformer; + @Autowired private SharedInformer podInformer; + @Autowired private Lister nodeLister; + @Autowired private Lister podLister; + + // *OPTIONAL* + // If you feed like hold the controller from running util some condition.. + @KubernetesReconcilerReadyFunc + public boolean informerReady() { + return podInformer.hasSynced() && nodeInformer.hasSynced(); + } + + @Override + public Result reconcile(Request request) { + V1Node node = nodeLister.get(request.getName()); + + System.out.println("get all pods in namespace " + namespace); + podLister.namespace(namespace).list().stream() + .map(pod -> pod.getMetadata().getName()) + .forEach(System.out::println); + + System.out.println("triggered reconciling " + node.getMetadata().getName()); + return new Result(false); + } + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/SpringLoadBalancerExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/SpringLoadBalancerExample.java new file mode 100644 index 0000000000..69dd3697aa --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/SpringLoadBalancerExample.java @@ -0,0 +1,57 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.extended.network.LoadBalancer; +import io.kubernetes.client.informer.SharedInformerFactory; +import io.kubernetes.client.spring.extended.network.annotation.KubernetesEndpointsLoadBalanced; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@SpringBootApplication +public class SpringLoadBalancerExample { + + public static void main(String[] args) { + SpringApplication.run(SpringLoadBalancerExample.class, args); + } + + @Configuration + public static class AppConfig { + + @Bean + public CommandLineRunner loadBalancerCommandLineRunner( + SharedInformerFactory sharedInformerFactory, MyService myService) { + return args -> { + System.out.println("starting informers.."); + sharedInformerFactory.startAllRegisteredInformers(); + + System.out.println("routing default/kubernetes:"); + System.out.println(myService.defaultKubernetesLoadBalancer.getTargetIP()); + }; + } + + @Bean + public MyService myService() { + return new MyService(); + } + } + + public static class MyService { + + @KubernetesEndpointsLoadBalanced(namespace = "default", name = "kubernetes") + private LoadBalancer defaultKubernetesLoadBalancer; + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/WatchExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/WatchExample.java new file mode 100644 index 0000000000..c32a74afd4 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/WatchExample.java @@ -0,0 +1,54 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import com.google.gson.reflect.TypeToken; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.models.V1Namespace; +import io.kubernetes.client.util.Config; +import io.kubernetes.client.util.Watch; +import java.io.IOException; +import java.util.concurrent.TimeUnit; +import okhttp3.OkHttpClient; + +/** A simple example of how to use Watch API to watch changes in Namespace list. */ +public class WatchExample { + public static void main(String[] args) throws IOException, ApiException { + ApiClient client = Config.defaultClient(); + // infinite timeout + OkHttpClient httpClient = + client.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build(); + client.setHttpClient(httpClient); + Configuration.setDefaultApiClient(client); + + CoreV1Api api = new CoreV1Api(); + + Watch watch = + Watch.createWatch( + client, + api.listNamespaceCall( + null, null, null, null, null, 5, null, null, null, Boolean.TRUE, null), + new TypeToken>() {}.getType()); + + try { + for (Watch.Response item : watch) { + System.out.printf("%s : %s%n", item.type, item.object.getMetadata().getName()); + } + } finally { + watch.close(); + } + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/WebSocketsExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/WebSocketsExample.java new file mode 100644 index 0000000000..e1f54dcd07 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/WebSocketsExample.java @@ -0,0 +1,74 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.util.Config; +import io.kubernetes.client.util.WebSockets; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import okhttp3.WebSocket; + +/** + * This is a pretty low level, most people won't need to use WebSockets directly. + * + *

If you do need to run it, you can run: mvn exec:java \ + * -Dexec.mainClass=io.kubernetes.client.examples.WebSocketsExample \ + * -Dexec.args=/api/v1/namespaces/default/pods//attach?stdout=true + * + *

Note that you'd think 'watch' calls were WebSockets, but you'd be wrong, they're straight HTTP + * GET calls. + */ +public class WebSocketsExample { + public static void main(String... args) throws ApiException, IOException { + final ApiClient client = Config.defaultClient(); + WebSockets.stream( + args[0], + "GET", + client, + new WebSockets.SocketListener() { + private volatile WebSocket socket; + + @Override + public void open(String protocol, WebSocket socket) { + this.socket = socket; + } + + @Override + public void close() {} + + @Override + public void bytesMessage(InputStream is) {} + + @Override + public void failure(Throwable t) { + t.printStackTrace(); + } + + @Override + public void textMessage(Reader in) { + try { + BufferedReader reader = new BufferedReader(in); + for (String line = reader.readLine(); line != null; line = reader.readLine()) { + System.out.println(line); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + } + }); + } +} diff --git a/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/YamlExample.java b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/YamlExample.java new file mode 100644 index 0000000000..84a3324ee2 --- /dev/null +++ b/examples/examples-release-12/src/main/java/io/kubernetes/client/examples/YamlExample.java @@ -0,0 +1,109 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import io.kubernetes.client.custom.IntOrString; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.models.V1DeleteOptions; +import io.kubernetes.client.openapi.models.V1Pod; +import io.kubernetes.client.openapi.models.V1PodBuilder; +import io.kubernetes.client.openapi.models.V1Service; +import io.kubernetes.client.openapi.models.V1ServiceBuilder; +import io.kubernetes.client.openapi.models.V1Status; +import io.kubernetes.client.util.Config; +import io.kubernetes.client.util.Yaml; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; + +/** + * A simple example of how to parse a Kubernetes object. + * + *

Easiest way to run this: mvn exec:java + * -Dexec.mainClass="io.kubernetes.client.examples.YamlExample" + * + *

From inside $REPO_DIR/examples + */ +public class YamlExample { + public static void main(String[] args) throws IOException, ApiException, ClassNotFoundException { + V1Pod pod = + new V1PodBuilder() + .withNewMetadata() + .withName("apod") + .endMetadata() + .withNewSpec() + .addNewContainer() + .withName("www") + .withImage("nginx") + .withNewResources() + .withLimits(new HashMap<>()) + .endResources() + .endContainer() + .endSpec() + .build(); + System.out.println(Yaml.dump(pod)); + + V1Service svc = + new V1ServiceBuilder() + .withNewMetadata() + .withName("aservice") + .endMetadata() + .withNewSpec() + .withSessionAffinity("ClientIP") + .withType("NodePort") + .addNewPort() + .withProtocol("TCP") + .withName("client") + .withPort(8008) + .withNodePort(8080) + .withTargetPort(new IntOrString(8080)) + .endPort() + .endSpec() + .build(); + System.out.println(Yaml.dump(svc)); + + // Read yaml configuration file, and deploy it + ApiClient client = Config.defaultClient(); + Configuration.setDefaultApiClient(client); + + // See issue #474. Not needed at most cases, but it is needed if you are using war + // packging or running this on JUnit. + Yaml.addModelMap("v1", "Service", V1Service.class); + + // Example yaml file can be found in $REPO_DIR/test-svc.yaml + File file = new File("test-svc.yaml"); + V1Service yamlSvc = (V1Service) Yaml.load(file); + + // Deployment and StatefulSet is defined in apps/v1, so you should use AppsV1Api instead of + // CoreV1API + CoreV1Api api = new CoreV1Api(); + V1Service createResult = api.createNamespacedService("default", yamlSvc, null, null, null); + + System.out.println(createResult); + + V1Status deleteResult = + api.deleteNamespacedService( + yamlSvc.getMetadata().getName(), + "default", + null, + null, + null, + null, + null, + new V1DeleteOptions()); + System.out.println(deleteResult); + } +} diff --git a/examples/examples-release-12/src/main/resources/application.properties b/examples/examples-release-12/src/main/resources/application.properties new file mode 100644 index 0000000000..dc886e5f48 --- /dev/null +++ b/examples/examples-release-12/src/main/resources/application.properties @@ -0,0 +1,2 @@ +namespace=airflow +management.endpoints.web.exposure.include=prometheus \ No newline at end of file diff --git a/examples/examples-release-12/src/test/java/io/kubernetes/client/examples/ExampleTest.java b/examples/examples-release-12/src/test/java/io/kubernetes/client/examples/ExampleTest.java new file mode 100644 index 0000000000..8540ce3af1 --- /dev/null +++ b/examples/examples-release-12/src/test/java/io/kubernetes/client/examples/ExampleTest.java @@ -0,0 +1,55 @@ +/* +Copyright 2020 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.examples; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static org.junit.Assert.assertEquals; + +import com.github.tomakehurst.wiremock.junit.WireMockRule; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.Configuration; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.models.V1Namespace; +import io.kubernetes.client.openapi.models.V1ObjectMeta; +import java.io.IOException; +import org.junit.Rule; +import org.junit.Test; + +public class ExampleTest { + private static final int PORT = 8089; + @Rule public WireMockRule wireMockRule = new WireMockRule(PORT); + + @Test + public void exactUrlOnly() throws IOException, ApiException { + ApiClient client = new ApiClient(); + client.setBasePath("http://localhost:" + PORT); + Configuration.setDefaultApiClient(client); + + V1Namespace ns1 = new V1Namespace().metadata(new V1ObjectMeta().name("name")); + + stubFor( + get(urlEqualTo("/api/v1/namespaces/name")) + .willReturn( + aResponse() + .withHeader("Content-Type", "application/json") + .withBody(client.getJSON().serialize(ns1)))); + + CoreV1Api api = new CoreV1Api(); + V1Namespace ns2 = api.readNamespace("name", null, null, null); + assertEquals(ns1, ns2); + } +} diff --git a/examples/examples-release-12/test-svc.yaml b/examples/examples-release-12/test-svc.yaml new file mode 100644 index 0000000000..f225bea76f --- /dev/null +++ b/examples/examples-release-12/test-svc.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: test-service +spec: + type: ClusterIP + selector: + app: test-service + ports: + - name: port-of-container + port: 8080 \ No newline at end of file diff --git a/examples/examples-release-12/test.yaml b/examples/examples-release-12/test.yaml new file mode 100644 index 0000000000..0b46e57003 --- /dev/null +++ b/examples/examples-release-12/test.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: ReplicationController +metadata: + name: test +spec: + replicas: 1 + template: + metadata: + labels: + app: test + spec: + containers: + - name: test + image: test/examples:1.0 + command: ["/bin/sh","-c"] + args: ["java -jar /examples.jar","while :; do sleep 1; done"] diff --git a/examples/pom.xml b/examples/pom.xml index bd52b2793a..3002ba4fea 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -1,7 +1,5 @@ - + 4.0.0 io.kubernetes @@ -21,8 +19,6 @@ - examples-release-11 - examples-release-10