Skip to content

Commit 7294ce5

Browse files
committed
Add Watch support
1 parent 91e8eeb commit 7294ce5

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
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
*
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+
}

0 commit comments

Comments
 (0)