Skip to content

Commit 37c630c

Browse files
committed
add exceptionHandler for SharedInformerFactory.sharedIndexInformerFor
1 parent 6b705c6 commit 37c630c

1 file changed

Lines changed: 102 additions & 57 deletions

File tree

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

Lines changed: 102 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public SharedInformerFactory(ApiClient client, ExecutorService threadPool) {
9797
* @return the shared index informer
9898
*/
9999
public synchronized <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
100-
SharedIndexInformer<ApiType> sharedIndexInformerFor(
100+
SharedIndexInformer<ApiType> sharedIndexInformerFor(
101101
CallGenerator callGenerator,
102102
Class<ApiType> apiTypeClass,
103103
Class<ApiListType> apiListTypeClass) {
@@ -117,14 +117,35 @@ SharedIndexInformer<ApiType> sharedIndexInformerFor(
117117
* @return the shared index informer
118118
*/
119119
public synchronized <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
120-
SharedIndexInformer<ApiType> sharedIndexInformerFor(
120+
SharedIndexInformer<ApiType> sharedIndexInformerFor(
121121
CallGenerator callGenerator,
122122
Class<ApiType> apiTypeClass,
123123
Class<ApiListType> apiListTypeClass,
124124
long resyncPeriodInMillis) {
125+
return sharedIndexInformerFor(callGenerator, apiTypeClass, apiListTypeClass, resyncPeriodInMillis, null);
126+
}
127+
128+
/**
129+
* Shared index informer for shared index informer.
130+
*
131+
* @param <ApiType> the type parameter
132+
* @param <ApiListType> the type parameter
133+
* @param callGenerator the call generator
134+
* @param apiTypeClass the api type class
135+
* @param apiListTypeClass the api list type class
136+
* @param exceptionHandler the exception Handler
137+
* @return the shared index informer
138+
*/
139+
public synchronized <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
140+
SharedIndexInformer<ApiType> sharedIndexInformerFor(
141+
CallGenerator callGenerator,
142+
Class<ApiType> apiTypeClass,
143+
Class<ApiListType> apiListTypeClass,
144+
long resyncPeriodInMillis,
145+
BiConsumer<Class<ApiType>, Throwable> exceptionHandler) {
125146
ListerWatcher<ApiType, ApiListType> listerWatcher =
126-
listerWatcherFor(callGenerator, apiTypeClass, apiListTypeClass);
127-
return sharedIndexInformerFor(listerWatcher, apiTypeClass, resyncPeriodInMillis);
147+
listerWatcherFor(callGenerator, apiTypeClass, apiListTypeClass);
148+
return sharedIndexInformerFor(listerWatcher, apiTypeClass, resyncPeriodInMillis, exceptionHandler);
128149
}
129150

130151
/**
@@ -140,23 +161,23 @@ SharedIndexInformer<ApiType> sharedIndexInformerFor(
140161
* @return the shared index informer
141162
*/
142163
public synchronized <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
143-
SharedIndexInformer<ApiType> sharedIndexInformerFor(
164+
SharedIndexInformer<ApiType> sharedIndexInformerFor(
144165
ListerWatcher<ApiType, ApiListType> listerWatcher,
145166
Class<ApiType> apiTypeClass,
146167
long resyncPeriodInMillis) {
147168
return sharedIndexInformerFor(listerWatcher, apiTypeClass, resyncPeriodInMillis, null);
148169
}
149170

150171
public synchronized <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
151-
SharedIndexInformer<ApiType> sharedIndexInformerFor(
172+
SharedIndexInformer<ApiType> sharedIndexInformerFor(
152173
ListerWatcher<ApiType, ApiListType> listerWatcher,
153174
Class<ApiType> apiTypeClass,
154175
long resyncPeriodInMillis,
155176
BiConsumer<Class<ApiType>, Throwable> exceptionHandler) {
156177

157178
SharedIndexInformer<ApiType> informer =
158-
new DefaultSharedIndexInformer<>(
159-
apiTypeClass, listerWatcher, resyncPeriodInMillis, new Cache<>(), exceptionHandler);
179+
new DefaultSharedIndexInformer<>(
180+
apiTypeClass, listerWatcher, resyncPeriodInMillis, new Cache<>(), exceptionHandler);
160181
this.informers.putIfAbsent(TypeToken.get(apiTypeClass).getType(), informer);
161182
return informer;
162183
}
@@ -174,12 +195,12 @@ SharedIndexInformer<ApiType> sharedIndexInformerFor(
174195
* @return the shared index informer
175196
*/
176197
public synchronized <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
177-
SharedIndexInformer<ApiType> sharedIndexInformerFor(
198+
SharedIndexInformer<ApiType> sharedIndexInformerFor(
178199
GenericKubernetesApi<ApiType, ApiListType> genericKubernetesApi,
179200
Class<ApiType> apiTypeClass,
180201
long resyncPeriodInMillis) {
181202
return sharedIndexInformerFor(
182-
genericKubernetesApi, apiTypeClass, resyncPeriodInMillis, Namespaces.NAMESPACE_ALL);
203+
genericKubernetesApi, apiTypeClass, resyncPeriodInMillis, Namespaces.NAMESPACE_ALL);
183204
}
184205

185206
/**
@@ -196,18 +217,42 @@ SharedIndexInformer<ApiType> sharedIndexInformerFor(
196217
* @return the shared index informer
197218
*/
198219
public synchronized <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
199-
SharedIndexInformer<ApiType> sharedIndexInformerFor(
220+
SharedIndexInformer<ApiType> sharedIndexInformerFor(
200221
GenericKubernetesApi<ApiType, ApiListType> genericKubernetesApi,
201222
Class<ApiType> apiTypeClass,
202223
long resyncPeriodInMillis,
203224
String namespace) {
225+
return sharedIndexInformerFor(genericKubernetesApi, apiTypeClass, resyncPeriodInMillis, namespace, null);
226+
}
227+
228+
/**
229+
* Working the same as {@link SharedInformerFactory#sharedIndexInformerFor} above.
230+
*
231+
* <p>Constructs and returns a shared index informer for a specific namespace.
232+
*
233+
* @param <ApiType> the type parameter
234+
* @param <ApiListType> the type parameter
235+
* @param genericKubernetesApi the generic kubernetes api
236+
* @param apiTypeClass the api type class
237+
* @param resyncPeriodInMillis the resync period in millis
238+
* @param namespace the target namespace
239+
* @param exceptionHandler the exception Handler
240+
* @return the shared index informer
241+
*/
242+
public synchronized <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
243+
SharedIndexInformer<ApiType> sharedIndexInformerFor(
244+
GenericKubernetesApi<ApiType, ApiListType> genericKubernetesApi,
245+
Class<ApiType> apiTypeClass,
246+
long resyncPeriodInMillis,
247+
String namespace,
248+
BiConsumer<Class<ApiType>, Throwable> exceptionHandler) {
204249
ListerWatcher<ApiType, ApiListType> listerWatcher =
205-
listerWatcherFor(genericKubernetesApi, namespace);
206-
return sharedIndexInformerFor(listerWatcher, apiTypeClass, resyncPeriodInMillis);
250+
listerWatcherFor(genericKubernetesApi, namespace);
251+
return sharedIndexInformerFor(listerWatcher, apiTypeClass, resyncPeriodInMillis, exceptionHandler);
207252
}
208253

209254
private <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
210-
ListerWatcher<ApiType, ApiListType> listerWatcherFor(
255+
ListerWatcher<ApiType, ApiListType> listerWatcherFor(
211256
CallGenerator callGenerator,
212257
Class<ApiType> apiTypeClass,
213258
Class<ApiListType> apiListTypeClass) {
@@ -228,15 +273,15 @@ public Watch<ApiType> watch(CallGeneratorParams params) throws ApiException {
228273
// bind call with private http client to make sure read timeout is zero.
229274
call = apiClient.getHttpClient().newCall(call.request());
230275
return Watch.createWatch(
231-
apiClient,
232-
call,
233-
TypeToken.getParameterized(Watch.Response.class, apiTypeClass).getType());
276+
apiClient,
277+
call,
278+
TypeToken.getParameterized(Watch.Response.class, apiTypeClass).getType());
234279
}
235280
};
236281
}
237282

238283
private <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
239-
ListerWatcher<ApiType, ApiListType> listerWatcherFor(
284+
ListerWatcher<ApiType, ApiListType> listerWatcherFor(
240285
GenericKubernetesApi<ApiType, ApiListType> genericKubernetesApi, String namespace) {
241286
if (apiClient.getReadTimeout() > 0) {
242287
// set read timeout zero to ensure client doesn't time out
@@ -247,48 +292,48 @@ ListerWatcher<ApiType, ApiListType> listerWatcherFor(
247292
public ApiListType list(CallGeneratorParams params) throws ApiException {
248293
if (Namespaces.NAMESPACE_ALL.equals(namespace)) {
249294
return genericKubernetesApi
250-
.list(
295+
.list(
296+
new ListOptions() {
297+
{
298+
setResourceVersion(params.resourceVersion);
299+
setTimeoutSeconds(params.timeoutSeconds);
300+
}
301+
})
302+
.throwsApiException()
303+
.getObject();
304+
} else {
305+
return genericKubernetesApi
306+
.list(
307+
namespace,
308+
new ListOptions() {
309+
{
310+
setResourceVersion(params.resourceVersion);
311+
setTimeoutSeconds(params.timeoutSeconds);
312+
}
313+
})
314+
.throwsApiException()
315+
.getObject();
316+
}
317+
}
318+
319+
public Watchable<ApiType> watch(CallGeneratorParams params) throws ApiException {
320+
if (Namespaces.NAMESPACE_ALL.equals(namespace)) {
321+
return genericKubernetesApi.watch(
251322
new ListOptions() {
252323
{
253324
setResourceVersion(params.resourceVersion);
254325
setTimeoutSeconds(params.timeoutSeconds);
255326
}
256-
})
257-
.throwsApiException()
258-
.getObject();
327+
});
259328
} else {
260-
return genericKubernetesApi
261-
.list(
329+
return genericKubernetesApi.watch(
262330
namespace,
263331
new ListOptions() {
264332
{
265333
setResourceVersion(params.resourceVersion);
266334
setTimeoutSeconds(params.timeoutSeconds);
267335
}
268-
})
269-
.throwsApiException()
270-
.getObject();
271-
}
272-
}
273-
274-
public Watchable<ApiType> watch(CallGeneratorParams params) throws ApiException {
275-
if (Namespaces.NAMESPACE_ALL.equals(namespace)) {
276-
return genericKubernetesApi.watch(
277-
new ListOptions() {
278-
{
279-
setResourceVersion(params.resourceVersion);
280-
setTimeoutSeconds(params.timeoutSeconds);
281-
}
282-
});
283-
} else {
284-
return genericKubernetesApi.watch(
285-
namespace,
286-
new ListOptions() {
287-
{
288-
setResourceVersion(params.resourceVersion);
289-
setTimeoutSeconds(params.timeoutSeconds);
290-
}
291-
});
336+
});
292337
}
293338
}
294339
};
@@ -303,7 +348,7 @@ public Watchable<ApiType> watch(CallGeneratorParams params) throws ApiException
303348
* @return the existing shared index informer
304349
*/
305350
public synchronized <ApiType extends KubernetesObject>
306-
SharedIndexInformer<ApiType> getExistingSharedIndexInformer(Class<ApiType> apiTypeClass) {
351+
SharedIndexInformer<ApiType> getExistingSharedIndexInformer(Class<ApiType> apiTypeClass) {
307352
return this.informers.get(TypeToken.get(apiTypeClass).getType());
308353
}
309354

@@ -313,9 +358,9 @@ public synchronized void startAllRegisteredInformers() {
313358
return;
314359
}
315360
informers.forEach(
316-
(informerType, informer) ->
317-
startedInformers.computeIfAbsent(
318-
informerType, key -> informerExecutor.submit((Runnable) informer::run)));
361+
(informerType, informer) ->
362+
startedInformers.computeIfAbsent(
363+
informerType, key -> informerExecutor.submit((Runnable) informer::run)));
319364
}
320365

321366
/** Stop all registered informers and shut down the thread pool. */
@@ -333,11 +378,11 @@ public synchronized void stopAllRegisteredInformers(boolean shutdownThreadPool)
333378
return;
334379
}
335380
informers.forEach(
336-
(informerType, informer) -> {
337-
if (startedInformers.remove(informerType) != null) {
338-
informer.stop();
339-
}
340-
});
381+
(informerType, informer) -> {
382+
if (startedInformers.remove(informerType) != null) {
383+
informer.stop();
384+
}
385+
});
341386
if (shutdownThreadPool) {
342387
informerExecutor.shutdown();
343388
}

0 commit comments

Comments
 (0)