Skip to content

Commit ca0f256

Browse files
Merge pull request #40 from mbohlool/watch
Add Watch Support
2 parents 39f471f + 7294ce5 commit ca0f256

38 files changed

+7943
-2355
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2017 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.reflect.TypeToken;
16+
import io.kubernetes.client.ApiClient;
17+
import io.kubernetes.client.Watch;
18+
import io.kubernetes.client.ApiException;
19+
import io.kubernetes.client.Configuration;
20+
import io.kubernetes.client.apis.CoreV1Api;
21+
import io.kubernetes.client.models.V1Namespace;
22+
import io.kubernetes.client.util.Config;
23+
24+
import java.io.IOException;
25+
26+
/**
27+
* A simple example of how to use Watch API to watch changes in Namespace list.
28+
*/
29+
public class WatchExample {
30+
public static void main(String[] args) throws IOException, ApiException{
31+
ApiClient client = Config.defaultClient();
32+
Configuration.setDefaultApiClient(client);
33+
34+
CoreV1Api api = new CoreV1Api();
35+
36+
Watch<V1Namespace> watch = client.watch(
37+
api.listNamespaceCall(null, null, null, null, 5, Boolean.TRUE, null, null),
38+
new TypeToken<Watch.Response<V1Namespace>>(){}.getType());
39+
40+
for (Watch.Response<V1Namespace> item : watch) {
41+
System.out.printf("%s : %s%n", item.type, item.object.getMetadata().getName());
42+
}
43+
}
44+
}

kubernetes/src/main/java/io/kubernetes/client/ApiClient.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,26 @@ public File prepareDownloadFile(Response response) throws IOException {
949949
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
950950
}
951951

952+
public <T> Watch<T> watch(Call call, Type watchType) throws ApiException {
953+
try {
954+
Response response = call.execute();
955+
if (!response.isSuccessful()) {
956+
String respBody = null;
957+
if (response.body() != null) {
958+
try {
959+
respBody = response.body().string();
960+
} catch (IOException e) {
961+
throw new ApiException(response.message(), e, response.code(), response.headers().toMultimap());
962+
}
963+
}
964+
throw new ApiException(response.message(), response.code(), response.headers().toMultimap(), respBody);
965+
}
966+
return new Watch<>(this.json, response.body(), watchType);
967+
} catch (IOException e) {
968+
throw new ApiException(e);
969+
}
970+
}
971+
952972
/**
953973
* {@link #execute(Call, Type)}
954974
*

kubernetes/src/main/java/io/kubernetes/client/ProgressRequestBody.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ public interface ProgressRequestListener {
3434

3535
private final ProgressRequestListener progressListener;
3636

37-
private BufferedSink bufferedSink;
38-
3937
public ProgressRequestBody(RequestBody requestBody, ProgressRequestListener progressListener) {
4038
this.requestBody = requestBody;
4139
this.progressListener = progressListener;
@@ -53,13 +51,9 @@ public long contentLength() throws IOException {
5351

5452
@Override
5553
public void writeTo(BufferedSink sink) throws IOException {
56-
if (bufferedSink == null) {
57-
bufferedSink = Okio.buffer(sink(sink));
58-
}
59-
54+
BufferedSink bufferedSink = Okio.buffer(sink(sink));
6055
requestBody.writeTo(bufferedSink);
6156
bufferedSink.flush();
62-
6357
}
6458

6559
private Sink sink(Sink sink) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright 2017 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;
14+
15+
import com.google.gson.annotations.SerializedName;
16+
import com.squareup.okhttp.ResponseBody;
17+
18+
import java.io.IOException;
19+
import java.lang.reflect.Type;
20+
import java.util.Iterator;
21+
22+
/**
23+
* Watch class implements watch mechansim of kubernetes. For every list API
24+
* call with a watch parameter you should be able to pass its call to this class
25+
* and watch changes to that list. For example CoreV1Api.listNamespace has watch
26+
* parameter, so you can create a call using CoreV1Api.listNamespaceCall and
27+
* set watch to True and watch the changes to namespaces.
28+
*/
29+
public class Watch<T> implements Iterable<Watch.Response<T>>,
30+
Iterator<Watch.Response<T>> {
31+
32+
/**
33+
* Response class holds a watch response that has a `type` that can be
34+
* ADDED, MODIFIED, DELETED and ERROR. It also hold the actual target
35+
* object.
36+
*/
37+
public static class Response<T> {
38+
@SerializedName("type")
39+
public String type;
40+
41+
@SerializedName("object")
42+
public T object;
43+
44+
Response(String type, T object) {
45+
this.type = type;
46+
this.object = object;
47+
}
48+
}
49+
50+
Type watchType;
51+
ResponseBody response;
52+
JSON json;
53+
54+
public Watch(JSON json, ResponseBody body, Type watchType) {
55+
this.response = body;
56+
this.watchType = watchType;
57+
this.json = json;
58+
}
59+
60+
public Response<T> next() {
61+
try {
62+
String line = response.source().readUtf8Line();
63+
if (line == null) {
64+
throw new RuntimeException("Null response from the server.");
65+
}
66+
return json.deserialize(line, watchType);
67+
} catch (IOException e) {
68+
throw new RuntimeException("IO Exception during next method.", e);
69+
}
70+
}
71+
72+
public boolean hasNext() {
73+
try {
74+
return !response.source().exhausted();
75+
} catch (IOException e) {
76+
throw new RuntimeException("IO Exception during hasNext method.",
77+
e);
78+
}
79+
}
80+
81+
public Iterator<Response<T>> iterator() {
82+
return this;
83+
}
84+
85+
public void remove() {
86+
throw new UnsupportedOperationException("remove");
87+
}
88+
}

kubernetes/src/main/java/io/kubernetes/client/apis/ApisApi.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,18 @@ public void setApiClient(ApiClient apiClient) {
5454
this.apiClient = apiClient;
5555
}
5656

57-
/* Build call for getAPIVersions */
58-
private com.squareup.okhttp.Call getAPIVersionsCall(final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
57+
/**
58+
* Build call for getAPIVersions
59+
* @param progressListener Progress listener
60+
* @param progressRequestListener Progress request listener
61+
* @return Call to execute
62+
* @throws ApiException If fail to serialize the request body object
63+
*/
64+
public com.squareup.okhttp.Call getAPIVersionsCall(final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
5965
Object localVarPostBody = null;
6066

6167
// create path and map variables
62-
String localVarPath = "/apis/".replaceAll("\\{format\\}","json");
68+
String localVarPath = "/apis/";
6369

6470
List<Pair> localVarQueryParams = new ArrayList<Pair>();
6571

kubernetes/src/main/java/io/kubernetes/client/apis/AppsApi.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,18 @@ public void setApiClient(ApiClient apiClient) {
5454
this.apiClient = apiClient;
5555
}
5656

57-
/* Build call for getAPIGroup */
58-
private com.squareup.okhttp.Call getAPIGroupCall(final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
57+
/**
58+
* Build call for getAPIGroup
59+
* @param progressListener Progress listener
60+
* @param progressRequestListener Progress request listener
61+
* @return Call to execute
62+
* @throws ApiException If fail to serialize the request body object
63+
*/
64+
public com.squareup.okhttp.Call getAPIGroupCall(final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
5965
Object localVarPostBody = null;
6066

6167
// create path and map variables
62-
String localVarPath = "/apis/apps/".replaceAll("\\{format\\}","json");
68+
String localVarPath = "/apis/apps/";
6369

6470
List<Pair> localVarQueryParams = new ArrayList<Pair>();
6571

0 commit comments

Comments
 (0)