|
12 | 12 | */ |
13 | 13 | package io.kubernetes.client.examples; |
14 | 14 |
|
15 | | -import com.google.gson.Gson; |
16 | | -import com.google.gson.JsonElement; |
17 | | -import com.google.gson.JsonObject; |
18 | 15 | import io.kubernetes.client.ApiClient; |
19 | 16 | import io.kubernetes.client.ApiException; |
20 | 17 | import io.kubernetes.client.Configuration; |
21 | 18 | import io.kubernetes.client.apis.ExtensionsV1beta1Api; |
| 19 | +import io.kubernetes.client.custom.V1Patch; |
22 | 20 | import io.kubernetes.client.models.ExtensionsV1beta1Deployment; |
23 | 21 | import io.kubernetes.client.util.ClientBuilder; |
24 | 22 | import java.io.IOException; |
25 | | -import java.util.ArrayList; |
26 | 23 |
|
27 | 24 | /** |
28 | 25 | * A simple Example of how to use the Java API.<br> |
29 | | - * This example demonstrates patching of deployment using Json Patch.<br> |
30 | | - * For generating Json Patches, refer <a href="http://jsonpatch.com/">http://jsonpatch.com</a>. |
| 26 | + * This example demonstrates patching of deployment using Json Patch and Strategic Merge Patch.<br> |
| 27 | + * For generating Json Patches, refer <a href="http://jsonpatch.com/">http://jsonpatch.com</a>. For |
| 28 | + * generating Strategic Merge Patches, refer <a |
| 29 | + * href="https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md">strategic-merge-patch.md</a>. |
31 | 30 | * |
32 | 31 | * <ul> |
33 | | - * <li>Creates deployment hello-node with <b>terminationGracePeriodSeconds</b> value as 30. |
34 | | - * <li>Patches deployment hello-node with <b>terminationGracePeriodSeconds</b> value as 27. |
| 32 | + * <li>Creates deployment hello-node with <b>terminationGracePeriodSeconds</b> value as 30 and a |
| 33 | + * finalizer. |
| 34 | + * <li>Json-Patches deployment hello-node with <b>terminationGracePeriodSeconds</b> value as 27. |
| 35 | + * <li>Strategic-Merge-Patches deployment hello-node removing the finalizer. |
35 | 36 | * </ul> |
36 | 37 | * |
37 | 38 | * <p>Easiest way to run this: mvn exec:java |
|
41 | 42 | */ |
42 | 43 | public class PatchExample { |
43 | 44 | static String jsonPatchStr = |
44 | | - "{\"op\":\"replace\",\"path\":\"/spec/template/spec/terminationGracePeriodSeconds\",\"value\":27}"; |
45 | | - static String jsonDepStr = |
46 | | - "{\"kind\":\"Deployment\",\"apiVersion\":\"extensions/v1beta1\",\"metadata\":{\"name\":\"hello-node\",\"creationTimestamp\":null,\"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}],\"resources\":{}}]}},\"strategy\":{}},\"status\":{}}"; |
| 45 | + "[{\"op\":\"replace\",\"path\":\"/spec/template/spec/terminationGracePeriodSeconds\",\"value\":27}]"; |
| 46 | + static String strategicMergePatchStr = |
| 47 | + "{\"metadata\":{\"$deleteFromPrimitiveList/finalizers\":[\"example.com/test\"]}}"; |
| 48 | + static String jsonDeploymentStr = |
| 49 | + "{\"kind\":\"Deployment\",\"apiVersion\":\"extensions/v1beta1\",\"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}],\"resources\":{}}]}},\"strategy\":{}},\"status\":{}}"; |
47 | 50 |
|
48 | | - public static void main(String[] args) throws IOException, ApiException { |
49 | | - PatchExample example = new PatchExample(); |
50 | | - ApiClient client = ClientBuilder.defaultClient(); |
51 | | - Configuration.setDefaultApiClient(client); |
| 51 | + public static void main(String[] args) throws IOException { |
| 52 | + try { |
| 53 | + ExtensionsV1beta1Api api = new ExtensionsV1beta1Api(ClientBuilder.standard().build()); |
| 54 | + ExtensionsV1beta1Deployment body = |
| 55 | + Configuration.getDefaultApiClient() |
| 56 | + .getJSON() |
| 57 | + .deserialize(jsonDeploymentStr, ExtensionsV1beta1Deployment.class); |
52 | 58 |
|
53 | | - ExtensionsV1beta1Deployment body = |
54 | | - (ExtensionsV1beta1Deployment) |
55 | | - example.deserialize(jsonDepStr, ExtensionsV1beta1Deployment.class); |
56 | | - ExtensionsV1beta1Deployment deploy1 = example.createDeployment("default", body, "false"); |
57 | | - System.out.println("original deployment" + deploy1); |
| 59 | + // create a deployment |
| 60 | + ExtensionsV1beta1Deployment deploy1 = |
| 61 | + api.createNamespacedDeployment("default", body, null, null, null); |
| 62 | + System.out.println("original deployment" + deploy1); |
58 | 63 |
|
59 | | - ArrayList<JsonObject> arr = new ArrayList<>(); |
60 | | - arr.add(((JsonElement) example.deserialize(jsonPatchStr, JsonElement.class)).getAsJsonObject()); |
61 | | - ExtensionsV1beta1Deployment deploy2 = |
62 | | - example.PatchDeployment("hello-node", "default", arr, "false"); |
63 | | - System.out.println("patched deployment" + deploy2); |
64 | | - } |
65 | | - |
66 | | - public ExtensionsV1beta1Deployment createDeployment( |
67 | | - String namespace, ExtensionsV1beta1Deployment body, String pretty) throws ApiException { |
68 | | - ExtensionsV1beta1Api api = new ExtensionsV1beta1Api(); |
69 | | - ExtensionsV1beta1Deployment deploy = |
70 | | - api.createNamespacedDeployment(namespace, body, pretty, null, null); |
71 | | - return deploy; |
72 | | - } |
| 64 | + // json-patch a deployment |
| 65 | + ApiClient jsonPatchClient = |
| 66 | + ClientBuilder.standard().setOverridePatchFormat(V1Patch.PATCH_FORMAT_JSON_PATCH).build(); |
| 67 | + ExtensionsV1beta1Deployment deploy2 = |
| 68 | + new ExtensionsV1beta1Api(jsonPatchClient) |
| 69 | + .patchNamespacedDeployment( |
| 70 | + "hello-node", "default", new V1Patch(jsonPatchStr), null, null, null, null); |
| 71 | + System.out.println("json-patched deployment" + deploy2); |
73 | 72 |
|
74 | | - public ExtensionsV1beta1Deployment PatchDeployment( |
75 | | - String deployName, String namespace, Object body, String pretty) throws ApiException { |
76 | | - ExtensionsV1beta1Api api = new ExtensionsV1beta1Api(); |
77 | | - ExtensionsV1beta1Deployment deploy = |
78 | | - api.patchNamespacedDeployment(deployName, namespace, body, pretty, null, null, null); |
79 | | - return deploy; |
80 | | - } |
| 73 | + // strategic-merge-patch a deployment |
| 74 | + ApiClient strategicMergePatchClient = |
| 75 | + ClientBuilder.standard() |
| 76 | + .setOverridePatchFormat(V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH) |
| 77 | + .build(); |
| 78 | + strategicMergePatchClient.setDebugging(true); |
| 79 | + ExtensionsV1beta1Deployment deploy3 = |
| 80 | + new ExtensionsV1beta1Api(strategicMergePatchClient) |
| 81 | + .patchNamespacedDeployment( |
| 82 | + "hello-node", |
| 83 | + "default", |
| 84 | + new V1Patch(strategicMergePatchStr), |
| 85 | + null, |
| 86 | + null, |
| 87 | + null, |
| 88 | + null); |
| 89 | + System.out.println("strategic-merge-patched deployment" + deploy3); |
81 | 90 |
|
82 | | - public Object deserialize(String jsonStr, Class<?> targetClass) { |
83 | | - Object obj = (new Gson()).fromJson(jsonStr, targetClass); |
84 | | - return obj; |
| 91 | + } catch (ApiException e) { |
| 92 | + System.out.println(e.getResponseBody()); |
| 93 | + e.printStackTrace(); |
| 94 | + } |
85 | 95 | } |
86 | 96 | } |
0 commit comments