27
27
import io .kubernetes .client .util .Watchable ;
28
28
import io .kubernetes .client .util .generic .GenericKubernetesApi ;
29
29
import io .kubernetes .client .util .generic .options .ListOptions ;
30
+ import okhttp3 .Call ;
31
+ import org .apache .commons .collections4 .MapUtils ;
32
+
30
33
import java .lang .reflect .Type ;
31
34
import java .util .HashMap ;
32
35
import java .util .Map ;
33
36
import java .util .concurrent .ExecutorService ;
34
37
import java .util .concurrent .Executors ;
35
38
import java .util .concurrent .Future ;
36
39
import java .util .function .BiConsumer ;
37
- import okhttp3 .Call ;
38
- import org .apache .commons .collections4 .MapUtils ;
39
40
40
- /** SharedInformerFactory class constructs and caches informers for api types. */
41
+ /**
42
+ * SharedInformerFactory class constructs and caches informers for api types.
43
+ * For each api type, only the first created informer is stored in the cache.
44
+ * If reuseExistingCachedInformer is set to true, It returns the cached informer when creating
45
+ * an informer for the same api type.
46
+ * Otherwise, this factory creates a new informer on each invocation.
47
+ */
41
48
public class SharedInformerFactory {
42
49
43
50
protected Map <Type , SharedIndexInformer > informers ;
@@ -47,35 +54,54 @@ public class SharedInformerFactory {
47
54
private ExecutorService informerExecutor ;
48
55
49
56
private ApiClient apiClient ;
57
+ private boolean reuseExistingCachedInformer ;
50
58
51
59
/** Constructor w/ default thread pool. */
52
60
/** DEPRECATE: In favor of explicit apiClient constructor to avoid misguiding */
53
61
@ Deprecated
54
62
public SharedInformerFactory () {
55
- this (Configuration .getDefaultApiClient ().setReadTimeout (0 ), Executors .newCachedThreadPool ());
63
+ this (Configuration .getDefaultApiClient ().setReadTimeout (0 ), Executors .newCachedThreadPool (), false );
56
64
}
57
65
58
- /** Constructor w/ api client specified and default thread pool. */
66
+ /**
67
+ * Constructor w/ api client specified, default thread pool and reuseExistingCachedInformer is false.
68
+ */
59
69
public SharedInformerFactory (ApiClient apiClient ) {
60
- this (apiClient , Executors .newCachedThreadPool ());
70
+ this (apiClient , false );
71
+ }
72
+
73
+ /**
74
+ * Constructor w/ api client specified and default thread pool
75
+ * @param apiClient specific api client
76
+ * @param reuseCache Determines whether to utilize an existing cached informer.
77
+ * If true, the method returns the first registered informer for multiple creations of the same apiClassType,
78
+ * else, each method calls results in the creation of a new informer,
79
+ * while only the first informer is stored in the cache.
80
+ */
81
+ public SharedInformerFactory (ApiClient apiClient , boolean reuseCache ) {
82
+ this (apiClient , Executors .newCachedThreadPool (), reuseCache );
61
83
}
62
84
63
85
/**
64
86
* Constructor w/ thread pool specified.
65
- *
66
- * @param threadPool specified thread pool
87
+ * DEPRECATE: In favor of explicit apiClient constructor to avoid misguiding.
67
88
*/
89
+ @ Deprecated
68
90
public SharedInformerFactory (ExecutorService threadPool ) {
69
- this (Configuration .getDefaultApiClient ().setReadTimeout (0 ), threadPool );
91
+ this (Configuration .getDefaultApiClient ().setReadTimeout (0 ),threadPool , false );
70
92
}
71
93
72
94
/**
73
95
* Constructor w/ api client and thread pool specified.
74
96
*
75
97
* @param client specific api client
76
98
* @param threadPool specified thread pool
99
+ * @param reuseCache Determines whether to utilize an existing cached informer.
100
+ * If true, the method returns the first registered informer for multiple creations of the same apiClassType,
101
+ * else, each method calls results in the creation of a new informer,
102
+ * while only the first informer is stored in the cache.
77
103
*/
78
- public SharedInformerFactory (ApiClient client , ExecutorService threadPool ) {
104
+ public SharedInformerFactory (ApiClient client , ExecutorService threadPool , boolean reuseCache ) {
79
105
if (client .getReadTimeout () != 0 ) {
80
106
throw new IllegalArgumentException ("read timeout of ApiClient must be zero" );
81
107
}
@@ -84,6 +110,7 @@ public SharedInformerFactory(ApiClient client, ExecutorService threadPool) {
84
110
informerExecutor = threadPool ;
85
111
informers = new HashMap <>();
86
112
startedInformers = new HashMap <>();
113
+ reuseExistingCachedInformer = reuseCache ;
87
114
}
88
115
89
116
/**
@@ -105,8 +132,7 @@ SharedIndexInformer<ApiType> sharedIndexInformerFor(
105
132
}
106
133
107
134
/**
108
- * Constructs and returns a shared index informer w/ resync period specified. But the informer
109
- * cache will not be overwritten i.e. only the first registered informer will be kept.
135
+ * Constructs and returns a shared index informer w/ resync period specified.
110
136
*
111
137
* @param <ApiType> the type parameter
112
138
* @param <ApiListType> the type parameter
@@ -151,9 +177,7 @@ SharedIndexInformer<ApiType> sharedIndexInformerFor(
151
177
}
152
178
153
179
/**
154
- * Constructs and returns a shared index informer by specifying lister-watcher. But the informer
155
- * cache will not be overwritten on multiple call w/ the the same apiTypeClass i.e. only the first
156
- * registered informer will be kept.
180
+ * Constructs and returns a shared index informer by specifying lister-watcher.
157
181
*
158
182
* @param <ApiType> the type parameter
159
183
* @param <ApiListType> the type parameter
@@ -176,18 +200,22 @@ SharedIndexInformer<ApiType> sharedIndexInformerFor(
176
200
Class <ApiType > apiTypeClass ,
177
201
long resyncPeriodInMillis ,
178
202
BiConsumer <Class <ApiType >, Throwable > exceptionHandler ) {
203
+ Type apiType = TypeToken .get (apiTypeClass ).getType ();
204
+
205
+ if (informers .containsKey (apiType ) && reuseExistingCachedInformer ) {
206
+ return informers .get (apiType );
207
+ }
179
208
180
209
SharedIndexInformer <ApiType > informer =
181
210
new DefaultSharedIndexInformer <>(
182
211
apiTypeClass , listerWatcher , resyncPeriodInMillis , new Cache <>(), exceptionHandler );
183
- this .informers .putIfAbsent (TypeToken .get (apiTypeClass ).getType (), informer );
212
+
213
+ this .informers .putIfAbsent (apiType , informer );
184
214
return informer ;
185
215
}
186
216
187
217
/**
188
- * Constructs and returns a shared index informer by specifying a generic api instance. But the
189
- * informer cache will not be overwritten on multiple call w/ the the same apiTypeClass i.e. only
190
- * the first registered informer will be kept.
218
+ * Constructs and returns a shared index informer by specifying a generic api instance.
191
219
*
192
220
* @param <ApiType> the type parameter
193
221
* @param <ApiListType> the type parameter
0 commit comments