|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | +package io.kubernetes.client.examples; |
| 14 | + |
| 15 | +import io.kubernetes.client.ApiClient; |
| 16 | +import io.kubernetes.client.ApiException; |
| 17 | +import io.kubernetes.client.Configuration; |
| 18 | +import io.kubernetes.client.apis.CoreV1Api; |
| 19 | +import io.kubernetes.client.models.V1Container; |
| 20 | +import io.kubernetes.client.models.V1ObjectMeta; |
| 21 | +import io.kubernetes.client.models.V1Pod; |
| 22 | +import io.kubernetes.client.models.V1PodBuilder; |
| 23 | +import io.kubernetes.client.models.V1PodList; |
| 24 | +import io.kubernetes.client.models.V1PodSpec; |
| 25 | +import io.kubernetes.client.util.Config; |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Arrays; |
| 28 | + |
| 29 | +/** |
| 30 | + * A simple example of how to use the Java API |
| 31 | + * |
| 32 | + * <p>Easiest way to run this: mvn exec:java |
| 33 | + * -Dexec.mainClass="io.kubernetes.client.examples.FluentExample" |
| 34 | + * |
| 35 | + * <p>From inside $REPO_DIR/examples |
| 36 | + */ |
| 37 | +public class FluentExample { |
| 38 | + public static void main(String[] args) throws IOException, ApiException { |
| 39 | + ApiClient client = Config.defaultClient(); |
| 40 | + Configuration.setDefaultApiClient(client); |
| 41 | + |
| 42 | + CoreV1Api api = new CoreV1Api(); |
| 43 | + |
| 44 | + V1Pod pod = |
| 45 | + new V1PodBuilder() |
| 46 | + .withNewMetadata() |
| 47 | + .withName("apod") |
| 48 | + .endMetadata() |
| 49 | + .withNewSpec() |
| 50 | + .addNewContainer() |
| 51 | + .withName("www") |
| 52 | + .withImage("nginx") |
| 53 | + .endContainer() |
| 54 | + .endSpec() |
| 55 | + .build(); |
| 56 | + |
| 57 | + api.createNamespacedPod("default", pod, null); |
| 58 | + |
| 59 | + V1Pod pod2 = |
| 60 | + new V1Pod() |
| 61 | + .metadata(new V1ObjectMeta().name("anotherpod")) |
| 62 | + .spec( |
| 63 | + new V1PodSpec() |
| 64 | + .containers(Arrays.asList(new V1Container().name("www").image("nginx")))); |
| 65 | + |
| 66 | + api.createNamespacedPod("default", pod2, null); |
| 67 | + |
| 68 | + V1PodList list = |
| 69 | + api.listNamespacedPod("default", null, null, null, null, null, null, null, null, null); |
| 70 | + for (V1Pod item : list.getItems()) { |
| 71 | + System.out.println(item.getMetadata().getName()); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments