Skip to content

Commit 8af4715

Browse files
committed
structurelized custom patch model
1 parent 29b7da4 commit 8af4715

File tree

2 files changed

+93
-44
lines changed

2 files changed

+93
-44
lines changed

examples/src/main/java/io/kubernetes/client/examples/PatchExample.java

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,27 @@
1212
*/
1313
package io.kubernetes.client.examples;
1414

15-
import com.google.gson.Gson;
16-
import com.google.gson.JsonElement;
17-
import com.google.gson.JsonObject;
1815
import io.kubernetes.client.ApiClient;
1916
import io.kubernetes.client.ApiException;
2017
import io.kubernetes.client.Configuration;
2118
import io.kubernetes.client.apis.ExtensionsV1beta1Api;
19+
import io.kubernetes.client.custom.V1Patch;
2220
import io.kubernetes.client.models.ExtensionsV1beta1Deployment;
2321
import io.kubernetes.client.util.ClientBuilder;
2422
import java.io.IOException;
25-
import java.util.ArrayList;
2623

2724
/**
2825
* 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>.
3130
*
3231
* <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.
3536
* </ul>
3637
*
3738
* <p>Easiest way to run this: mvn exec:java
@@ -41,46 +42,55 @@
4142
*/
4243
public class PatchExample {
4344
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\":{}}";
4750

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);
5258

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);
5863

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);
7372

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);
8190

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+
}
8595
}
8696
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.kubernetes.client.custom;
2+
3+
import com.google.gson.TypeAdapter;
4+
import com.google.gson.annotations.JsonAdapter;
5+
import com.google.gson.stream.JsonReader;
6+
import com.google.gson.stream.JsonWriter;
7+
8+
import java.io.IOException;
9+
10+
@JsonAdapter(V1Patch.V1PatchAdapter.class)
11+
public class V1Patch {
12+
13+
public static final String PATCH_FORMAT_JSON_PATCH = "application/json-patch+json";
14+
public static final String PATCH_FORMAT_JSON_MERGE_PATCH = "application/merge-patch+json";
15+
public static final String PATCH_FORMAT_STRATEGIC_MERGE_PATCH = "application/strategic-merge-patch+json";
16+
17+
public V1Patch(final String value) {
18+
this.value = value;
19+
}
20+
21+
private String value;
22+
23+
public String getValue() {
24+
return value;
25+
}
26+
27+
public class V1PatchAdapter extends TypeAdapter<V1Patch> {
28+
@Override
29+
public void write(JsonWriter jsonWriter, V1Patch patch) throws IOException {
30+
jsonWriter.jsonValue(patch.getValue());
31+
}
32+
33+
@Override
34+
public V1Patch read(JsonReader jsonReader) throws IOException {
35+
throw new UnsupportedOperationException("deserializing patch data is not supported");
36+
}
37+
}
38+
39+
}

0 commit comments

Comments
 (0)