|
22 | 22 | import io.kubernetes.client.util.CallGenerator;
|
23 | 23 | import io.kubernetes.client.util.CallGeneratorParams;
|
24 | 24 | import io.kubernetes.client.util.Watch;
|
| 25 | +import io.kubernetes.client.util.Watchable; |
| 26 | +import io.kubernetes.client.util.generic.GenericKubernetesApi; |
| 27 | +import io.kubernetes.client.util.generic.KubernetesApiResponse; |
| 28 | +import io.kubernetes.client.util.generic.options.ListOptions; |
25 | 29 | import java.lang.reflect.Type;
|
26 | 30 | import java.util.HashMap;
|
27 | 31 | import java.util.Map;
|
@@ -135,12 +139,32 @@ SharedIndexInformer<ApiType> sharedIndexInformerFor(
|
135 | 139 | Class<ApiType> apiTypeClass,
|
136 | 140 | long resyncPeriodInMillis) {
|
137 | 141 | SharedIndexInformer<ApiType> informer =
|
138 |
| - new DefaultSharedIndexInformer<ApiType, ApiListType>( |
139 |
| - apiTypeClass, listerWatcher, resyncPeriodInMillis); |
| 142 | + new DefaultSharedIndexInformer<>(apiTypeClass, listerWatcher, resyncPeriodInMillis); |
140 | 143 | this.informers.putIfAbsent(TypeToken.get(apiTypeClass).getType(), informer);
|
141 | 144 | return informer;
|
142 | 145 | }
|
143 | 146 |
|
| 147 | + /** |
| 148 | + * Constructs and returns a shared index informer by specifying a generic api instance. But the |
| 149 | + * informer cache will not be overwritten on multiple call w/ the the same apiTypeClass i.e. only |
| 150 | + * the first registered informer will be kept. |
| 151 | + * |
| 152 | + * @param <ApiType> the type parameter |
| 153 | + * @param <ApiListType> the type parameter |
| 154 | + * @param genericKubernetesApi the generic kubernetes api |
| 155 | + * @param apiTypeClass the api type class |
| 156 | + * @param resyncPeriodInMillis the resync period in millis |
| 157 | + * @return the shared index informer |
| 158 | + */ |
| 159 | + public synchronized <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject> |
| 160 | + SharedIndexInformer<ApiType> sharedIndexInformerFor( |
| 161 | + GenericKubernetesApi<ApiType, ApiListType> genericKubernetesApi, |
| 162 | + Class<ApiType> apiTypeClass, |
| 163 | + long resyncPeriodInMillis) { |
| 164 | + ListerWatcher<ApiType, ApiListType> listerWatcher = listerWatcherFor(genericKubernetesApi); |
| 165 | + return sharedIndexInformerFor(listerWatcher, apiTypeClass, resyncPeriodInMillis); |
| 166 | + } |
| 167 | + |
144 | 168 | private <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
|
145 | 169 | ListerWatcher<ApiType, ApiListType> listerWatcherFor(
|
146 | 170 | CallGenerator callGenerator,
|
@@ -170,6 +194,37 @@ public Watch<ApiType> watch(CallGeneratorParams params) throws ApiException {
|
170 | 194 | };
|
171 | 195 | }
|
172 | 196 |
|
| 197 | + private <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject> |
| 198 | + ListerWatcher<ApiType, ApiListType> listerWatcherFor( |
| 199 | + GenericKubernetesApi<ApiType, ApiListType> genericKubernetesApi) { |
| 200 | + if (apiClient.getHttpClient().readTimeoutMillis() > 0) { |
| 201 | + // set read timeout zero to ensure client doesn't time out |
| 202 | + OkHttpClient httpClient = |
| 203 | + apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build(); |
| 204 | + apiClient.setHttpClient(httpClient); |
| 205 | + } |
| 206 | + return new ListerWatcher<ApiType, ApiListType>() { |
| 207 | + public ApiListType list(CallGeneratorParams params) throws ApiException { |
| 208 | + KubernetesApiResponse<ApiListType> resp = |
| 209 | + genericKubernetesApi.list( |
| 210 | + new ListOptions() { |
| 211 | + { |
| 212 | + setResourceVersion(params.resourceVersion); |
| 213 | + setTimeoutSeconds(params.timeoutSeconds); |
| 214 | + } |
| 215 | + }); |
| 216 | + if (!resp.isSuccess()) { |
| 217 | + throw new ApiException(resp.getHttpStatusCode(), resp.getStatus().getMessage()); |
| 218 | + } |
| 219 | + return resp.getObject(); |
| 220 | + } |
| 221 | + |
| 222 | + public Watchable<ApiType> watch(CallGeneratorParams params) throws ApiException { |
| 223 | + return genericKubernetesApi.watch(); |
| 224 | + } |
| 225 | + }; |
| 226 | + } |
| 227 | + |
173 | 228 | /**
|
174 | 229 | * Gets existing shared index informer, return null if the requesting informer is never
|
175 | 230 | * constructed.
|
|
0 commit comments