forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseClient.java
341 lines (298 loc) · 10.8 KB
/
BaseClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.impl;
import io.fabric8.kubernetes.api.model.APIGroup;
import io.fabric8.kubernetes.api.model.APIGroupList;
import io.fabric8.kubernetes.api.model.APIResourceList;
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesResource;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.api.model.RootPaths;
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.VersionInfo;
import io.fabric8.kubernetes.client.KubernetesClientBuilder.ExecutorSupplier;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext;
import io.fabric8.kubernetes.client.dsl.internal.HasMetadataOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.OperationContext;
import io.fabric8.kubernetes.client.dsl.internal.OperationSupport;
import io.fabric8.kubernetes.client.extension.ExtensionAdapter;
import io.fabric8.kubernetes.client.extension.SupportTestingClient;
import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.kubernetes.client.utils.ApiVersionUtil;
import io.fabric8.kubernetes.client.utils.Utils;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Predicate;
public abstract class BaseClient implements Client {
/**
* An {@link ExecutorSupplier} that provides an unlimited thread pool {@link Executor} per client.
*/
public static final ExecutorSupplier DEFAULT_EXECUTOR_SUPPLIER = new ExecutorSupplier() {
@Override
public Executor get() {
return Executors.newCachedThreadPool(Utils.daemonThreadFactory(this));
}
@Override
public void onClose(Executor executor) {
((ExecutorService) executor).shutdownNow();
}
};
public static final String APIS = "/apis";
private URL masterUrl;
private String apiVersion;
private String namespace;
private Predicate<String> matchingGroupPredicate;
private final Adapters adapters;
private final Handlers handlers;
protected Config config;
protected HttpClient httpClient;
private OperationSupport operationSupport;
private ExecutorSupplier executorSupplier;
private Executor executor;
private OperationContext operationContext;
BaseClient(Config config, BaseClient baseClient) {
this.config = config;
this.httpClient = baseClient.httpClient;
this.adapters = baseClient.adapters;
this.handlers = baseClient.handlers;
this.matchingGroupPredicate = baseClient.matchingGroupPredicate;
this.executorSupplier = baseClient.executorSupplier;
this.executor = baseClient.executor;
setDerivedFields();
}
BaseClient(final HttpClient httpClient, Config config, ExecutorSupplier executorSupplier) {
this.config = config;
this.httpClient = httpClient;
this.handlers = new Handlers();
this.adapters = new Adapters(this.handlers);
setDerivedFields();
if (executorSupplier == null) {
executorSupplier = DEFAULT_EXECUTOR_SUPPLIER;
}
this.executorSupplier = executorSupplier;
this.executor = executorSupplier.get();
}
protected void setDerivedFields() {
this.namespace = config.getNamespace();
this.apiVersion = config.getApiVersion();
if (config.getMasterUrl() == null) {
throw new KubernetesClientException("Unknown Kubernetes master URL - " +
"please set with the builder, or set with either system property \"" + Config.KUBERNETES_MASTER_SYSTEM_PROPERTY
+ "\"" +
" or environment variable \"" + Utils.convertSystemPropertyNameToEnvVar(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY)
+ "\"");
}
try {
this.masterUrl = new URL(config.getMasterUrl());
} catch (MalformedURLException e) {
throw KubernetesClientException.launderThrowable(e);
}
}
@Override
public synchronized void close() {
httpClient.close();
if (this.executorSupplier != null) {
this.executorSupplier.onClose(executor);
this.executorSupplier = null;
}
}
@Override
public URL getMasterUrl() {
return masterUrl;
}
@Override
public String getApiVersion() {
return apiVersion;
}
@Override
public String getNamespace() {
return namespace;
}
public void setMatchingGroupPredicate(Predicate<String> unsupportedApiGroups) {
this.matchingGroupPredicate = unsupportedApiGroups;
}
@Override
public boolean hasApiGroup(String apiGroup, boolean exact) {
if (matchingGroupPredicate != null) {
return matchingGroupPredicate.test(apiGroup);
}
if (exact) {
return getApiGroup(apiGroup) != null;
}
APIGroupList apiGroups = getApiGroups();
if (apiGroups == null) {
return false;
}
return apiGroups
.getGroups()
.stream()
.anyMatch(g -> g.getName().endsWith(apiGroup));
}
@Override
public <C extends Client> Boolean isAdaptable(Class<C> type) {
// if type is an instanceof SupportTestingClient, then it's a proper
// test, otherwise it could be legacy support on an extension client
C toTest = adapt(type);
if (toTest instanceof SupportTestingClient) {
return ((SupportTestingClient) toTest).isSupported();
}
return true;
}
@Override
public <R extends KubernetesResource> boolean supports(Class<R> type) {
final String typeApiVersion = HasMetadata.getApiVersion(type);
if (matchingGroupPredicate != null) {
return matchingGroupPredicate.test(typeApiVersion);
}
final String typeKind = HasMetadata.getKind(type);
if (Utils.isNullOrEmpty(typeApiVersion) || Utils.isNullOrEmpty(typeKind)) {
return false;
}
final APIResourceList apiResources = getApiResources(ApiVersionUtil.joinApiGroupAndVersion(
HasMetadata.getGroup(type), HasMetadata.getVersion(type)));
if (apiResources == null) {
return false;
}
return apiResources.getResources()
.stream()
.anyMatch(r -> typeKind.equals(r.getKind()));
}
@Override
public <C extends Client> C adapt(Class<C> type) {
if (type.isAssignableFrom(this.getClass())) {
return (C) this;
}
ExtensionAdapter<C> adapter = adapters.get(type);
if (adapter == null) {
throw new IllegalStateException("No adapter available for type:" + type);
}
return adapter.adapt(this);
}
@Override
public RootPaths rootPaths() {
return getOperationSupport().restCall(RootPaths.class);
}
@Override
public boolean supportsApiPath(String apiPath) {
RootPaths rootPaths = rootPaths();
if (rootPaths != null) {
List<String> paths = rootPaths.getPaths();
if (paths != null) {
for (String path : paths) {
if (path.equals(apiPath)) {
return true;
}
}
}
}
return false;
}
@Override
public APIGroupList getApiGroups() {
return getOperationSupport().restCall(APIGroupList.class, APIS);
}
@Override
public APIGroup getApiGroup(String name) {
return getOperationSupport().restCall(APIGroup.class, APIS, name);
}
private OperationSupport getOperationSupport() {
if (operationSupport == null) {
this.operationSupport = new OperationSupport(this);
}
return this.operationSupport;
}
@Override
public APIResourceList getApiResources(String groupVersion) {
if ("v1".equals(groupVersion)) {
return getOperationSupport().restCall(APIResourceList.class, "api", "v1");
}
return getOperationSupport().restCall(APIResourceList.class, APIS, groupVersion);
}
protected VersionInfo getVersionInfo(String path) {
return getOperationSupport().restCall(VersionInfo.class, path);
}
@Override
public <T extends HasMetadata, L extends KubernetesResourceList<T>, R extends Resource<T>> MixedOperation<T, L, R> resources(
Class<T> resourceType, Class<L> listClass, Class<R> resourceClass) {
if (GenericKubernetesResource.class.equals(resourceType)) {
throw new KubernetesClientException("resources cannot be called with a generic type");
}
if (resourceType.isInterface()) {
throw new IllegalArgumentException("resources cannot be called with an interface");
}
try {
// TODO: check the Resource class type
return handlers.getOperation(resourceType, listClass, this);
} catch (Exception e) {
//may be the wrong list type, try more general - may still fail if the resource is not properly annotated
if (resourceClass == null || Resource.class.equals(resourceClass)) {
return (MixedOperation<T, L, R>) newHasMetadataOperation(ResourceDefinitionContext.fromResourceType(resourceType),
resourceType, listClass);
}
throw KubernetesClientException.launderThrowable(e);
}
}
public <T extends HasMetadata, L extends KubernetesResourceList<T>> HasMetadataOperationsImpl<T, L> newHasMetadataOperation(
ResourceDefinitionContext rdContext, Class<T> resourceType, Class<L> listClass) {
return new HasMetadataOperationsImpl<>(this, rdContext, resourceType, listClass);
}
@Override
public Config getConfiguration() {
return config;
}
@Override
public HttpClient getHttpClient() {
return httpClient;
}
public Adapters getAdapters() {
return adapters;
}
public Handlers getHandlers() {
return handlers;
}
/**
* Return the default operation context
*/
public OperationContext getOperationContext() {
return operationContext;
}
public BaseClient operationContext(OperationContext operationContext) {
this.operationContext = operationContext;
return this;
}
/**
* Create a new instance with the given config. All other client resources will be shared.
*
* @param config
* @return
*/
abstract BaseClient newInstance(Config config);
public Client newClient(OperationContext newContext) {
return newInstance(config).operationContext(newContext);
}
public Executor getExecutor() {
return executor;
}
}