Skip to content

Commit 11a4833

Browse files
committed
use factory member http client to invoke watch call; throw IllegalArgumentException if read timeout of http client that is passing to SharedInformerFactory is not zero
1 parent c66e385 commit 11a4833

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

util/src/main/java/io/kubernetes/client/informer/SharedInformerFactory.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
import java.util.concurrent.ExecutorService;
3232
import java.util.concurrent.Executors;
3333
import java.util.concurrent.Future;
34-
import java.util.concurrent.TimeUnit;
3534
import okhttp3.Call;
36-
import okhttp3.OkHttpClient;
3735
import org.apache.commons.collections4.MapUtils;
3836

3937
/** SharedInformerFactory class constructs and caches informers for api types. */
@@ -49,7 +47,7 @@ public class SharedInformerFactory {
4947

5048
/** Constructor w/ default thread pool. */
5149
public SharedInformerFactory() {
52-
this(Configuration.getDefaultApiClient(), Executors.newCachedThreadPool());
50+
this(Configuration.getDefaultApiClient().setReadTimeout(0), Executors.newCachedThreadPool());
5351
}
5452

5553
/** Constructor w/ api client specified and default thread pool. */
@@ -63,7 +61,7 @@ public SharedInformerFactory(ApiClient apiClient) {
6361
* @param threadPool specified thread pool
6462
*/
6563
public SharedInformerFactory(ExecutorService threadPool) {
66-
this(Configuration.getDefaultApiClient(), threadPool);
64+
this(Configuration.getDefaultApiClient().setReadTimeout(0), threadPool);
6765
}
6866

6967
/**
@@ -73,6 +71,10 @@ public SharedInformerFactory(ExecutorService threadPool) {
7371
* @param threadPool specified thread pool
7472
*/
7573
public SharedInformerFactory(ApiClient client, ExecutorService threadPool) {
74+
if (client.getReadTimeout() != 0) {
75+
throw new IllegalArgumentException("read timeout of ApiClient must be zero");
76+
}
77+
7678
apiClient = client;
7779
informerExecutor = threadPool;
7880
informers = new HashMap<>();
@@ -169,11 +171,9 @@ ListerWatcher<ApiType, ApiListType> listerWatcherFor(
169171
CallGenerator callGenerator,
170172
Class<ApiType> apiTypeClass,
171173
Class<ApiListType> apiListTypeClass) {
172-
if (apiClient.getHttpClient().readTimeoutMillis() > 0) {
174+
if (apiClient.getReadTimeout() > 0) {
173175
// set read timeout zero to ensure client doesn't time out
174-
OkHttpClient httpClient =
175-
apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build();
176-
apiClient.setHttpClient(httpClient);
176+
apiClient.setReadTimeout(0);
177177
}
178178
return new ListerWatcher<ApiType, ApiListType>() {
179179
@Override
@@ -185,6 +185,8 @@ public ApiListType list(CallGeneratorParams params) throws ApiException {
185185
@Override
186186
public Watch<ApiType> watch(CallGeneratorParams params) throws ApiException {
187187
Call call = callGenerator.generate(params);
188+
// bind call with private http client to make sure read timeout is zero.
189+
call = apiClient.getHttpClient().newCall(call.request());
188190
return Watch.createWatch(
189191
apiClient,
190192
call,
@@ -196,12 +198,11 @@ public Watch<ApiType> watch(CallGeneratorParams params) throws ApiException {
196198
private <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
197199
ListerWatcher<ApiType, ApiListType> listerWatcherFor(
198200
GenericKubernetesApi<ApiType, ApiListType> genericKubernetesApi) {
199-
if (apiClient.getHttpClient().readTimeoutMillis() > 0) {
201+
if (apiClient.getReadTimeout() > 0) {
200202
// set read timeout zero to ensure client doesn't time out
201-
OkHttpClient httpClient =
202-
apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build();
203-
apiClient.setHttpClient(httpClient);
203+
apiClient.setReadTimeout(0);
204204
}
205+
// TODO: it seems read timeout is determined by genericKubernetesApi instead of above apiClient.
205206
return new ListerWatcher<ApiType, ApiListType>() {
206207
public ApiListType list(CallGeneratorParams params) throws ApiException {
207208
return genericKubernetesApi

0 commit comments

Comments
 (0)