Skip to content

Commit da58401

Browse files
authored
Merge pull request #587 from yue9944882/feat/structureize-patch-object
Feat: Structurelize patch object
2 parents 29b7da4 + 71aa54c commit da58401

File tree

112 files changed

+1379
-1308
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+1379
-1308
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
}

kubernetes/docs/AdmissionregistrationV1beta1Api.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ BearerToken.setApiKey("YOUR API KEY");
631631

632632
AdmissionregistrationV1beta1Api apiInstance = new AdmissionregistrationV1beta1Api();
633633
String name = "name_example"; // String | name of the MutatingWebhookConfiguration
634-
Object body = null; // Object |
634+
V1Patch body = new V1Patch(); // V1Patch |
635635
String pretty = "pretty_example"; // String | If 'true', then the output is pretty printed.
636636
String dryRun = "dryRun_example"; // String | When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed
637637
String fieldManager = "fieldManager_example"; // String | fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch).
@@ -650,7 +650,7 @@ try {
650650
Name | Type | Description | Notes
651651
------------- | ------------- | ------------- | -------------
652652
**name** | **String**| name of the MutatingWebhookConfiguration |
653-
**body** | **Object**| |
653+
**body** | [**V1Patch**](V1Patch.md)| |
654654
**pretty** | **String**| If &#39;true&#39;, then the output is pretty printed. | [optional]
655655
**dryRun** | **String**| When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed | [optional]
656656
**fieldManager** | **String**| fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch). | [optional]
@@ -696,7 +696,7 @@ BearerToken.setApiKey("YOUR API KEY");
696696

697697
AdmissionregistrationV1beta1Api apiInstance = new AdmissionregistrationV1beta1Api();
698698
String name = "name_example"; // String | name of the ValidatingWebhookConfiguration
699-
Object body = null; // Object |
699+
V1Patch body = new V1Patch(); // V1Patch |
700700
String pretty = "pretty_example"; // String | If 'true', then the output is pretty printed.
701701
String dryRun = "dryRun_example"; // String | When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed
702702
String fieldManager = "fieldManager_example"; // String | fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch).
@@ -715,7 +715,7 @@ try {
715715
Name | Type | Description | Notes
716716
------------- | ------------- | ------------- | -------------
717717
**name** | **String**| name of the ValidatingWebhookConfiguration |
718-
**body** | **Object**| |
718+
**body** | [**V1Patch**](V1Patch.md)| |
719719
**pretty** | **String**| If &#39;true&#39;, then the output is pretty printed. | [optional]
720720
**dryRun** | **String**| When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed | [optional]
721721
**fieldManager** | **String**| fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch). | [optional]

kubernetes/docs/ApiextensionsV1beta1Api.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ BearerToken.setApiKey("YOUR API KEY");
361361

362362
ApiextensionsV1beta1Api apiInstance = new ApiextensionsV1beta1Api();
363363
String name = "name_example"; // String | name of the CustomResourceDefinition
364-
Object body = null; // Object |
364+
V1Patch body = new V1Patch(); // V1Patch |
365365
String pretty = "pretty_example"; // String | If 'true', then the output is pretty printed.
366366
String dryRun = "dryRun_example"; // String | When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed
367367
String fieldManager = "fieldManager_example"; // String | fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch).
@@ -380,7 +380,7 @@ try {
380380
Name | Type | Description | Notes
381381
------------- | ------------- | ------------- | -------------
382382
**name** | **String**| name of the CustomResourceDefinition |
383-
**body** | **Object**| |
383+
**body** | [**V1Patch**](V1Patch.md)| |
384384
**pretty** | **String**| If &#39;true&#39;, then the output is pretty printed. | [optional]
385385
**dryRun** | **String**| When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed | [optional]
386386
**fieldManager** | **String**| fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch). | [optional]
@@ -426,7 +426,7 @@ BearerToken.setApiKey("YOUR API KEY");
426426

427427
ApiextensionsV1beta1Api apiInstance = new ApiextensionsV1beta1Api();
428428
String name = "name_example"; // String | name of the CustomResourceDefinition
429-
Object body = null; // Object |
429+
V1Patch body = new V1Patch(); // V1Patch |
430430
String pretty = "pretty_example"; // String | If 'true', then the output is pretty printed.
431431
String dryRun = "dryRun_example"; // String | When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed
432432
String fieldManager = "fieldManager_example"; // String | fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch).
@@ -445,7 +445,7 @@ try {
445445
Name | Type | Description | Notes
446446
------------- | ------------- | ------------- | -------------
447447
**name** | **String**| name of the CustomResourceDefinition |
448-
**body** | **Object**| |
448+
**body** | [**V1Patch**](V1Patch.md)| |
449449
**pretty** | **String**| If &#39;true&#39;, then the output is pretty printed. | [optional]
450450
**dryRun** | **String**| When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed | [optional]
451451
**fieldManager** | **String**| fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch). | [optional]

kubernetes/docs/ApiregistrationV1Api.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ BearerToken.setApiKey("YOUR API KEY");
361361

362362
ApiregistrationV1Api apiInstance = new ApiregistrationV1Api();
363363
String name = "name_example"; // String | name of the APIService
364-
Object body = null; // Object |
364+
V1Patch body = new V1Patch(); // V1Patch |
365365
String pretty = "pretty_example"; // String | If 'true', then the output is pretty printed.
366366
String dryRun = "dryRun_example"; // String | When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed
367367
String fieldManager = "fieldManager_example"; // String | fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch).
@@ -380,7 +380,7 @@ try {
380380
Name | Type | Description | Notes
381381
------------- | ------------- | ------------- | -------------
382382
**name** | **String**| name of the APIService |
383-
**body** | **Object**| |
383+
**body** | [**V1Patch**](V1Patch.md)| |
384384
**pretty** | **String**| If &#39;true&#39;, then the output is pretty printed. | [optional]
385385
**dryRun** | **String**| When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed | [optional]
386386
**fieldManager** | **String**| fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch). | [optional]
@@ -426,7 +426,7 @@ BearerToken.setApiKey("YOUR API KEY");
426426

427427
ApiregistrationV1Api apiInstance = new ApiregistrationV1Api();
428428
String name = "name_example"; // String | name of the APIService
429-
Object body = null; // Object |
429+
V1Patch body = new V1Patch(); // V1Patch |
430430
String pretty = "pretty_example"; // String | If 'true', then the output is pretty printed.
431431
String dryRun = "dryRun_example"; // String | When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed
432432
String fieldManager = "fieldManager_example"; // String | fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch).
@@ -445,7 +445,7 @@ try {
445445
Name | Type | Description | Notes
446446
------------- | ------------- | ------------- | -------------
447447
**name** | **String**| name of the APIService |
448-
**body** | **Object**| |
448+
**body** | [**V1Patch**](V1Patch.md)| |
449449
**pretty** | **String**| If &#39;true&#39;, then the output is pretty printed. | [optional]
450450
**dryRun** | **String**| When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed | [optional]
451451
**fieldManager** | **String**| fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch). | [optional]

0 commit comments

Comments
 (0)