|
15 | 15 | import io.kubernetes.client.custom.V1Patch; |
16 | 16 | import io.kubernetes.client.openapi.ApiException; |
17 | 17 | import io.kubernetes.client.openapi.Configuration; |
18 | | -import io.kubernetes.client.openapi.apis.ExtensionsV1beta1Api; |
19 | | -import io.kubernetes.client.openapi.models.ExtensionsV1beta1Deployment; |
| 18 | +import io.kubernetes.client.openapi.apis.AppsV1Api; |
| 19 | +import io.kubernetes.client.openapi.models.V1Deployment; |
20 | 20 | import io.kubernetes.client.util.ClientBuilder; |
21 | 21 | import io.kubernetes.client.util.PatchUtils; |
22 | 22 | import java.io.IOException; |
@@ -46,74 +46,73 @@ public class PatchExample { |
46 | 46 | static String strategicMergePatchStr = |
47 | 47 | "{\"metadata\":{\"$deleteFromPrimitiveList/finalizers\":[\"example.com/test\"]}}"; |
48 | 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\":{}}"; |
| 49 | + "{\"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\":{}}"; |
50 | 50 | static String applyYamlStr = |
51 | | - "{\"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:v2\",\"ports\":[{\"containerPort\":8080}],\"resources\":{}}]}},\"strategy\":{}},\"status\":{}}"; |
| 51 | + "{\"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\":{}}"; |
52 | 52 |
|
53 | 53 | public static void main(String[] args) throws IOException { |
54 | 54 | try { |
55 | | - ExtensionsV1beta1Api api = new ExtensionsV1beta1Api(ClientBuilder.standard().build()); |
56 | | - ExtensionsV1beta1Deployment body = |
| 55 | + AppsV1Api api = new AppsV1Api(ClientBuilder.standard().build()); |
| 56 | + V1Deployment body = |
57 | 57 | Configuration.getDefaultApiClient() |
58 | 58 | .getJSON() |
59 | | - .deserialize(jsonDeploymentStr, ExtensionsV1beta1Deployment.class); |
| 59 | + .deserialize(jsonDeploymentStr, V1Deployment.class); |
60 | 60 |
|
61 | 61 | // create a deployment |
62 | | - ExtensionsV1beta1Deployment deploy1 = |
63 | | - api.createNamespacedDeployment("default", body, null, null, null); |
| 62 | + V1Deployment deploy1 = api.createNamespacedDeployment("default", body, null, null, null); |
64 | 63 | System.out.println("original deployment" + deploy1); |
65 | 64 |
|
66 | 65 | // json-patch a deployment |
67 | | - ExtensionsV1beta1Deployment deploy2 = |
| 66 | + V1Deployment deploy2 = |
68 | 67 | PatchUtils.patch( |
69 | | - ExtensionsV1beta1Deployment.class, |
| 68 | + V1Deployment.class, |
70 | 69 | () -> |
71 | 70 | api.patchNamespacedDeploymentCall( |
72 | 71 | "hello-node", |
73 | 72 | "default", |
74 | 73 | new V1Patch(jsonPatchStr), |
75 | 74 | null, |
76 | 75 | null, |
77 | | - null, |
| 76 | + null, // field-manager is optional |
78 | 77 | null, |
79 | 78 | null), |
80 | 79 | V1Patch.PATCH_FORMAT_JSON_PATCH, |
81 | 80 | api.getApiClient()); |
82 | 81 | System.out.println("json-patched deployment" + deploy2); |
83 | 82 |
|
84 | 83 | // strategic-merge-patch a deployment |
85 | | - ExtensionsV1beta1Deployment deploy3 = |
| 84 | + V1Deployment deploy3 = |
86 | 85 | PatchUtils.patch( |
87 | | - ExtensionsV1beta1Deployment.class, |
| 86 | + V1Deployment.class, |
88 | 87 | () -> |
89 | 88 | api.patchNamespacedDeploymentCall( |
90 | 89 | "hello-node", |
91 | 90 | "default", |
92 | 91 | new V1Patch(strategicMergePatchStr), |
93 | 92 | null, |
94 | 93 | null, |
95 | | - null, |
| 94 | + null, // field-manager is optional |
96 | 95 | null, |
97 | 96 | null), |
98 | 97 | V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, |
99 | 98 | api.getApiClient()); |
100 | 99 | System.out.println("strategic-merge-patched deployment" + deploy3); |
101 | 100 |
|
102 | | - // apply-yaml a deployment, server side apply is alpha in kubernetes v1.14, |
103 | | - // You need to actively enable the Server Side Apply alpha feature |
| 101 | + // apply-yaml a deployment, server side apply is available by default after kubernetes v1.16 |
| 102 | + // or opt-in by turning on the feature gate for v1.14 or v1.15. |
104 | 103 | // https://kubernetes.io/docs/reference/using-api/api-concepts/#server-side-apply |
105 | | - ExtensionsV1beta1Deployment deploy4 = |
| 104 | + V1Deployment deploy4 = |
106 | 105 | PatchUtils.patch( |
107 | | - ExtensionsV1beta1Deployment.class, |
| 106 | + V1Deployment.class, |
108 | 107 | () -> |
109 | 108 | api.patchNamespacedDeploymentCall( |
110 | 109 | "hello-node", |
111 | 110 | "default", |
112 | 111 | new V1Patch(applyYamlStr), |
113 | 112 | null, |
114 | 113 | null, |
115 | | - null, |
116 | | - null, |
| 114 | + "example-field-manager", // field-manager is required for server-side apply |
| 115 | + true, |
117 | 116 | null), |
118 | 117 | V1Patch.PATCH_FORMAT_APPLY_YAML, |
119 | 118 | api.getApiClient()); |
|
0 commit comments