Skip to content

Commit 85a0258

Browse files
Merge pull request #187 from kondapally1989/master
working example of patch deployment
2 parents 505a812 + 9909604 commit 85a0258

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.examples;
14+
15+
import com.google.gson.Gson;
16+
import com.google.gson.JsonElement;
17+
import com.google.gson.JsonObject;
18+
import io.kubernetes.client.ApiClient;
19+
import io.kubernetes.client.ApiException;
20+
import io.kubernetes.client.Configuration;
21+
import io.kubernetes.client.apis.ExtensionsV1beta1Api;
22+
import io.kubernetes.client.models.ExtensionsV1beta1Deployment;
23+
import io.kubernetes.client.util.ClientBuilder;
24+
import java.io.IOException;
25+
import java.util.ArrayList;
26+
27+
/**
28+
* 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>.
31+
*
32+
* <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.
35+
* </ul>
36+
*
37+
* <p>Easiest way to run this: mvn exec:java
38+
* -Dexec.mainClass="io.kubernetes.client.examples.PatchExample"
39+
*
40+
* <p>From inside $REPO_DIR/examples
41+
*/
42+
public class PatchExample {
43+
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\":{}}";
47+
48+
public static void main(String[] args) throws IOException, ApiException {
49+
PatchExample example = new PatchExample();
50+
ApiClient client = ClientBuilder.defaultClient();
51+
Configuration.setDefaultApiClient(client);
52+
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);
58+
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 = api.createNamespacedDeployment(namespace, body, pretty);
70+
return deploy;
71+
}
72+
73+
public ExtensionsV1beta1Deployment PatchDeployment(
74+
String deployName, String namespace, Object body, String pretty) throws ApiException {
75+
ExtensionsV1beta1Api api = new ExtensionsV1beta1Api();
76+
ExtensionsV1beta1Deployment deploy =
77+
api.patchNamespacedDeployment(deployName, namespace, body, pretty);
78+
return deploy;
79+
}
80+
81+
public Object deserialize(String jsonStr, Class<?> targetClass) {
82+
Object obj = (new Gson()).fromJson(jsonStr, targetClass);
83+
return obj;
84+
}
85+
}

0 commit comments

Comments
 (0)