|
12 | 12 | */ |
13 | 13 | package io.kubernetes.client.examples; |
14 | 14 |
|
| 15 | +import io.kubernetes.client.ApiClient; |
15 | 16 | import io.kubernetes.client.ApiException; |
| 17 | +import io.kubernetes.client.Configuration; |
| 18 | +import io.kubernetes.client.apis.CoreV1Api; |
16 | 19 | import io.kubernetes.client.custom.IntOrString; |
| 20 | +import io.kubernetes.client.models.V1DeleteOptions; |
17 | 21 | import io.kubernetes.client.models.V1Pod; |
18 | 22 | import io.kubernetes.client.models.V1PodBuilder; |
19 | 23 | import io.kubernetes.client.models.V1Service; |
20 | 24 | import io.kubernetes.client.models.V1ServiceBuilder; |
| 25 | +import io.kubernetes.client.models.V1Status; |
| 26 | +import io.kubernetes.client.util.Config; |
21 | 27 | import io.kubernetes.client.util.Yaml; |
| 28 | +import java.io.File; |
22 | 29 | import java.io.IOException; |
23 | 30 | import java.util.HashMap; |
24 | 31 |
|
@@ -67,5 +74,36 @@ public static void main(String[] args) throws IOException, ApiException, ClassNo |
67 | 74 | .endSpec() |
68 | 75 | .build(); |
69 | 76 | System.out.println(Yaml.dump(svc)); |
| 77 | + |
| 78 | + // Read yaml configuration file, and deploy it |
| 79 | + ApiClient client = Config.defaultClient(); |
| 80 | + Configuration.setDefaultApiClient(client); |
| 81 | + |
| 82 | + // See issue #474. Not needed at most cases, but it is needed if you are using war |
| 83 | + // packging or running this on JUnit. |
| 84 | + Yaml.addModelMap("v1", "Service", V1Service.class); |
| 85 | + |
| 86 | + // Example yaml file can be found in $REPO_DIR/test-svc.yaml |
| 87 | + File file = new File("test-svc.yaml"); |
| 88 | + V1Service yamlSvc = (V1Service) Yaml.load(file); |
| 89 | + |
| 90 | + // Deployment and StatefulSet is defined in apps/v1, so you should use AppsV1Api instead of |
| 91 | + // CoreV1API |
| 92 | + CoreV1Api api = new CoreV1Api(); |
| 93 | + V1Service createResult = api.createNamespacedService("default", yamlSvc, null, null, null); |
| 94 | + |
| 95 | + System.out.println(createResult); |
| 96 | + |
| 97 | + V1Status deleteResult = |
| 98 | + api.deleteNamespacedService( |
| 99 | + yamlSvc.getMetadata().getName(), |
| 100 | + "default", |
| 101 | + null, |
| 102 | + new V1DeleteOptions(), |
| 103 | + null, |
| 104 | + null, |
| 105 | + null, |
| 106 | + null); |
| 107 | + System.out.println(deleteResult); |
70 | 108 | } |
71 | 109 | } |
0 commit comments