|
| 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.ApiException; |
| 16 | +import io.kubernetes.client.custom.IntOrString; |
| 17 | +import io.kubernetes.client.models.V1Pod; |
| 18 | +import io.kubernetes.client.models.V1PodBuilder; |
| 19 | +import io.kubernetes.client.models.V1Service; |
| 20 | +import io.kubernetes.client.models.V1ServiceBuilder; |
| 21 | +import io.kubernetes.client.util.Yaml; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.HashMap; |
| 24 | + |
| 25 | +/** |
| 26 | + * A simple example of how to parse a Kubernetes object. |
| 27 | + * |
| 28 | + * <p>Easiest way to run this: mvn exec:java |
| 29 | + * -Dexec.mainClass="io.kubernetes.client.examples.YamlExample" |
| 30 | + * |
| 31 | + * <p>From inside $REPO_DIR/examples |
| 32 | + */ |
| 33 | +public class YamlExample { |
| 34 | + public static void main(String[] args) throws IOException, ApiException, ClassNotFoundException { |
| 35 | + V1Pod pod = |
| 36 | + new V1PodBuilder() |
| 37 | + .withNewMetadata() |
| 38 | + .withName("apod") |
| 39 | + .endMetadata() |
| 40 | + .withNewSpec() |
| 41 | + .addNewContainer() |
| 42 | + .withName("www") |
| 43 | + .withImage("nginx") |
| 44 | + .withNewResources() |
| 45 | + .withLimits(new HashMap<>()) |
| 46 | + .endResources() |
| 47 | + .endContainer() |
| 48 | + .endSpec() |
| 49 | + .build(); |
| 50 | + System.out.println(Yaml.dump(pod)); |
| 51 | + |
| 52 | + V1Service svc = |
| 53 | + new V1ServiceBuilder() |
| 54 | + .withNewMetadata() |
| 55 | + .withName("aservice") |
| 56 | + .endMetadata() |
| 57 | + .withNewSpec() |
| 58 | + .withSessionAffinity("ClientIP") |
| 59 | + .withType("NodePort") |
| 60 | + .addNewPort() |
| 61 | + .withProtocol("TCP") |
| 62 | + .withName("client") |
| 63 | + .withPort(8008) |
| 64 | + .withNodePort(8080) |
| 65 | + .withTargetPort(new IntOrString(8080)) |
| 66 | + .endPort() |
| 67 | + .endSpec() |
| 68 | + .build(); |
| 69 | + System.out.println(Yaml.dump(svc)); |
| 70 | + } |
| 71 | +} |
0 commit comments