Skip to content

Commit 79fbdcf

Browse files
authored
Merge pull request #636 from yue9944882/refactor/informer-call-generator
[Refactor] Add Call-generator interface throwing checked ApiException
2 parents b9c5558 + 6281064 commit 79fbdcf

File tree

5 files changed

+67
-39
lines changed

5 files changed

+67
-39
lines changed

examples/src/main/java/io/kubernetes/client/examples/InformerExample.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.kubernetes.client.examples;
22

33
import io.kubernetes.client.ApiClient;
4-
import io.kubernetes.client.ApiException;
54
import io.kubernetes.client.Configuration;
65
import io.kubernetes.client.apis.CoreV1Api;
76
import io.kubernetes.client.informer.*;
@@ -35,21 +34,17 @@ public static void main(String[] args) throws Exception {
3534
SharedIndexInformer<V1Node> nodeInformer =
3635
factory.sharedIndexInformerFor(
3736
(CallGeneratorParams params) -> {
38-
try {
39-
return coreV1Api.listNodeCall(
40-
null,
41-
null,
42-
null,
43-
null,
44-
null,
45-
params.resourceVersion,
46-
params.timeoutSeconds,
47-
params.watch,
48-
null,
49-
null);
50-
} catch (ApiException e) {
51-
throw new RuntimeException(e);
52-
}
37+
return coreV1Api.listNodeCall(
38+
null,
39+
null,
40+
null,
41+
null,
42+
null,
43+
params.resourceVersion,
44+
params.timeoutSeconds,
45+
params.watch,
46+
null,
47+
null);
5348
},
5449
V1Node.class,
5550
V1NodeList.class);

util/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<dependency>
9393
<groupId>org.mockito</groupId>
9494
<artifactId>mockito-core</artifactId>
95-
<version>2.13.0</version>
95+
<version>2.23.4</version>
9696
<scope>test</scope>
9797
</dependency>
9898
<dependency>

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.squareup.okhttp.Call;
55
import io.kubernetes.client.*;
66
import io.kubernetes.client.informer.impl.DefaultSharedIndexInformer;
7+
import io.kubernetes.client.util.CallGenerator;
78
import io.kubernetes.client.util.CallGeneratorParams;
89
import io.kubernetes.client.util.Watch;
910
import io.kubernetes.client.util.common.Collections;
@@ -13,7 +14,6 @@
1314
import java.util.concurrent.ExecutorService;
1415
import java.util.concurrent.Executors;
1516
import java.util.concurrent.Future;
16-
import java.util.function.Function;
1717

1818
/** SharedInformerFactory class constructs and caches informers for api types. */
1919
public class SharedInformerFactory {
@@ -51,7 +51,7 @@ public SharedInformerFactory(ExecutorService threadPool) {
5151
* @return the shared index informer
5252
*/
5353
public synchronized <ApiType, ApiListType> SharedIndexInformer<ApiType> sharedIndexInformerFor(
54-
Function<CallGeneratorParams, Call> callGenerator,
54+
CallGenerator callGenerator,
5555
Class<ApiType> apiTypeClass,
5656
Class<ApiListType> apiListTypeClass) {
5757
return sharedIndexInformerFor(callGenerator, apiTypeClass, apiListTypeClass, 0);
@@ -70,7 +70,7 @@ public synchronized <ApiType, ApiListType> SharedIndexInformer<ApiType> sharedIn
7070
* @return the shared index informer
7171
*/
7272
public synchronized <ApiType, ApiListType> SharedIndexInformer<ApiType> sharedIndexInformerFor(
73-
Function<CallGeneratorParams, Call> callGenerator,
73+
CallGenerator callGenerator,
7474
Class<ApiType> apiTypeClass,
7575
Class<ApiListType> apiListTypeClass,
7676
long resyncPeriodInMillis) {
@@ -84,20 +84,20 @@ public synchronized <ApiType, ApiListType> SharedIndexInformer<ApiType> sharedIn
8484
}
8585

8686
private <ApiType, ApiListType> ListerWatcher<ApiType, ApiListType> listerWatcherFor(
87-
Function<CallGeneratorParams, Call> callGenerator,
87+
CallGenerator callGenerator,
8888
Class<ApiType> apiTypeClass,
8989
Class<ApiListType> apiListTypeClass) {
9090
ApiClient apiClient = Configuration.getDefaultApiClient();
9191
return new ListerWatcher<ApiType, ApiListType>() {
9292
@Override
9393
public ApiListType list(CallGeneratorParams params) throws ApiException {
94-
Call call = callGenerator.apply(params);
94+
Call call = callGenerator.generate(params);
9595
return (ApiListType) apiClient.execute(call, apiListTypeClass).getData();
9696
}
9797

9898
@Override
9999
public Watch<ApiType> watch(CallGeneratorParams params) throws ApiException {
100-
Call call = callGenerator.apply(params);
100+
Call call = callGenerator.generate(params);
101101
return Watch.createWatch(
102102
apiClient,
103103
call,
@@ -106,6 +106,19 @@ public Watch<ApiType> watch(CallGeneratorParams params) throws ApiException {
106106
};
107107
}
108108

109+
/**
110+
* Gets existing shared index informer, return null if the requesting informer is never
111+
* constructed.
112+
*
113+
* @param <ApiType> the type parameter
114+
* @param apiTypeClass the api type class
115+
* @return the existing shared index informer
116+
*/
117+
public synchronized <ApiType> SharedIndexInformer<ApiType> getExistingSharedIndexInformer(
118+
Class<ApiType> apiTypeClass) {
119+
return this.informers.get(TypeToken.get(apiTypeClass).getType());
120+
}
121+
109122
/** Start all registered informers. */
110123
public synchronized void startAllRegisteredInformers() {
111124
if (Collections.isEmptyMap(informers)) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.kubernetes.client.util;
2+
3+
import com.squareup.okhttp.Call;
4+
import io.kubernetes.client.ApiException;
5+
6+
/**
7+
* The interface Call generator. It's for homogenizing client interface so that we can invoke a
8+
* generized adaptor interface elsewhere.
9+
*
10+
* <p>For example, we can adapt list-node interface from CoreV1Api package like:
11+
*
12+
* <p>(CallGeneratorParams params) -> { return coreV1Api.listNodeCall( null, null, null, null, null,
13+
* params.resourceVersion, params.timeoutSeconds, params.watch, null, null); },
14+
*/
15+
@FunctionalInterface
16+
public interface CallGenerator {
17+
/**
18+
* Generate call.
19+
*
20+
* @param params the params
21+
* @return the call
22+
* @throws ApiException the api exception
23+
*/
24+
Call generate(CallGeneratorParams params) throws ApiException;
25+
}

util/src/test/java/io/kubernetes/client/informer/SharedInformerFactoryTest.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.kubernetes.client.informer;
22

3-
import io.kubernetes.client.ApiException;
43
import io.kubernetes.client.apis.CoreV1Api;
54
import io.kubernetes.client.models.V1Namespace;
65
import io.kubernetes.client.models.V1NamespaceList;
@@ -14,21 +13,17 @@ public void shutdownInformerFactoryInstantlyAfterStarting() {
1413
SharedInformerFactory factory = new SharedInformerFactory();
1514
factory.sharedIndexInformerFor(
1615
(CallGeneratorParams params) -> {
17-
try {
18-
return api.listNamespaceCall(
19-
null,
20-
null,
21-
null,
22-
null,
23-
null,
24-
params.resourceVersion,
25-
params.timeoutSeconds,
26-
params.watch,
27-
null,
28-
null);
29-
} catch (ApiException e) {
30-
throw new RuntimeException(e);
31-
}
16+
return api.listNamespaceCall(
17+
null,
18+
null,
19+
null,
20+
null,
21+
null,
22+
params.resourceVersion,
23+
params.timeoutSeconds,
24+
params.watch,
25+
null,
26+
null);
3227
},
3328
V1Namespace.class,
3429
V1NamespaceList.class);

0 commit comments

Comments
 (0)