Skip to content

Commit a172457

Browse files
authored
Merge pull request #1084 from yue9944882/refresh-patch-example-api
Switiching to apps/v1 deployment api in the examples
2 parents 1030c87 + 215fbb2 commit a172457

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
import io.kubernetes.client.openapi.ApiClient;
1616
import io.kubernetes.client.openapi.ApiException;
1717
import io.kubernetes.client.openapi.Configuration;
18+
import io.kubernetes.client.openapi.apis.AppsV1Api;
1819
import io.kubernetes.client.openapi.apis.CoreV1Api;
19-
import io.kubernetes.client.openapi.apis.ExtensionsV1beta1Api;
20-
import io.kubernetes.client.openapi.models.ExtensionsV1beta1Deployment;
21-
import io.kubernetes.client.openapi.models.ExtensionsV1beta1DeploymentList;
22-
import io.kubernetes.client.openapi.models.ExtensionsV1beta1DeploymentSpec;
20+
import io.kubernetes.client.openapi.models.V1Deployment;
21+
import io.kubernetes.client.openapi.models.V1DeploymentList;
22+
import io.kubernetes.client.openapi.models.V1DeploymentSpec;
2323
import io.kubernetes.client.openapi.models.V1NamespaceList;
2424
import io.kubernetes.client.openapi.models.V1PodList;
2525
import io.kubernetes.client.openapi.models.V1ServiceList;
@@ -208,26 +208,25 @@ public static List<String> getServices() throws ApiException {
208208
*/
209209
public static void scaleDeployment(String deploymentName, int numberOfReplicas)
210210
throws ApiException {
211-
ExtensionsV1beta1Api extensionV1Api = new ExtensionsV1beta1Api();
212-
extensionV1Api.setApiClient(COREV1_API.getApiClient());
213-
ExtensionsV1beta1DeploymentList listNamespacedDeployment =
214-
extensionV1Api.listNamespacedDeployment(
211+
AppsV1Api appsV1Api = new AppsV1Api();
212+
appsV1Api.setApiClient(COREV1_API.getApiClient());
213+
V1DeploymentList listNamespacedDeployment =
214+
appsV1Api.listNamespacedDeployment(
215215
DEFAULT_NAME_SPACE, null, null, null, null, null, null, null, null, Boolean.FALSE);
216216

217-
List<ExtensionsV1beta1Deployment> extensionsV1beta1DeploymentItems =
218-
listNamespacedDeployment.getItems();
219-
Optional<ExtensionsV1beta1Deployment> findedDeployment =
220-
extensionsV1beta1DeploymentItems.stream()
217+
List<V1Deployment> appsV1DeploymentItems = listNamespacedDeployment.getItems();
218+
Optional<V1Deployment> findedDeployment =
219+
appsV1DeploymentItems.stream()
221220
.filter(
222-
(ExtensionsV1beta1Deployment deployment) ->
221+
(V1Deployment deployment) ->
223222
deployment.getMetadata().getName().equals(deploymentName))
224223
.findFirst();
225224
findedDeployment.ifPresent(
226-
(ExtensionsV1beta1Deployment deploy) -> {
225+
(V1Deployment deploy) -> {
227226
try {
228-
ExtensionsV1beta1DeploymentSpec newSpec = deploy.getSpec().replicas(numberOfReplicas);
229-
ExtensionsV1beta1Deployment newDeploy = deploy.spec(newSpec);
230-
extensionV1Api.replaceNamespacedDeployment(
227+
V1DeploymentSpec newSpec = deploy.getSpec().replicas(numberOfReplicas);
228+
V1Deployment newDeploy = deploy.spec(newSpec);
229+
appsV1Api.replaceNamespacedDeployment(
231230
deploymentName, DEFAULT_NAME_SPACE, newDeploy, null, null, null);
232231
} catch (ApiException ex) {
233232
LOGGER.warn("Scale the pod failed for Deployment:" + deploymentName, ex);

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import io.kubernetes.client.custom.V1Patch;
1616
import io.kubernetes.client.openapi.ApiException;
1717
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;
2020
import io.kubernetes.client.util.ClientBuilder;
2121
import io.kubernetes.client.util.PatchUtils;
2222
import java.io.IOException;
@@ -46,74 +46,73 @@ public class PatchExample {
4646
static String strategicMergePatchStr =
4747
"{\"metadata\":{\"$deleteFromPrimitiveList/finalizers\":[\"example.com/test\"]}}";
4848
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\":{}}";
5050
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\":{}}";
5252

5353
public static void main(String[] args) throws IOException {
5454
try {
55-
ExtensionsV1beta1Api api = new ExtensionsV1beta1Api(ClientBuilder.standard().build());
56-
ExtensionsV1beta1Deployment body =
55+
AppsV1Api api = new AppsV1Api(ClientBuilder.standard().build());
56+
V1Deployment body =
5757
Configuration.getDefaultApiClient()
5858
.getJSON()
59-
.deserialize(jsonDeploymentStr, ExtensionsV1beta1Deployment.class);
59+
.deserialize(jsonDeploymentStr, V1Deployment.class);
6060

6161
// create a deployment
62-
ExtensionsV1beta1Deployment deploy1 =
63-
api.createNamespacedDeployment("default", body, null, null, null);
62+
V1Deployment deploy1 = api.createNamespacedDeployment("default", body, null, null, null);
6463
System.out.println("original deployment" + deploy1);
6564

6665
// json-patch a deployment
67-
ExtensionsV1beta1Deployment deploy2 =
66+
V1Deployment deploy2 =
6867
PatchUtils.patch(
69-
ExtensionsV1beta1Deployment.class,
68+
V1Deployment.class,
7069
() ->
7170
api.patchNamespacedDeploymentCall(
7271
"hello-node",
7372
"default",
7473
new V1Patch(jsonPatchStr),
7574
null,
7675
null,
77-
null,
76+
null, // field-manager is optional
7877
null,
7978
null),
8079
V1Patch.PATCH_FORMAT_JSON_PATCH,
8180
api.getApiClient());
8281
System.out.println("json-patched deployment" + deploy2);
8382

8483
// strategic-merge-patch a deployment
85-
ExtensionsV1beta1Deployment deploy3 =
84+
V1Deployment deploy3 =
8685
PatchUtils.patch(
87-
ExtensionsV1beta1Deployment.class,
86+
V1Deployment.class,
8887
() ->
8988
api.patchNamespacedDeploymentCall(
9089
"hello-node",
9190
"default",
9291
new V1Patch(strategicMergePatchStr),
9392
null,
9493
null,
95-
null,
94+
null, // field-manager is optional
9695
null,
9796
null),
9897
V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH,
9998
api.getApiClient());
10099
System.out.println("strategic-merge-patched deployment" + deploy3);
101100

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.
104103
// https://kubernetes.io/docs/reference/using-api/api-concepts/#server-side-apply
105-
ExtensionsV1beta1Deployment deploy4 =
104+
V1Deployment deploy4 =
106105
PatchUtils.patch(
107-
ExtensionsV1beta1Deployment.class,
106+
V1Deployment.class,
108107
() ->
109108
api.patchNamespacedDeploymentCall(
110109
"hello-node",
111110
"default",
112111
new V1Patch(applyYamlStr),
113112
null,
114113
null,
115-
null,
116-
null,
114+
"example-field-manager", // field-manager is required for server-side apply
115+
true,
117116
null),
118117
V1Patch.PATCH_FORMAT_APPLY_YAML,
119118
api.getApiClient());

0 commit comments

Comments
 (0)