-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
KubernetesClientBuilder.java
128 lines (110 loc) · 4.29 KB
/
KubernetesClientBuilder.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
/**
* 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;
import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.kubernetes.client.informers.ResourceEventHandler;
import io.fabric8.kubernetes.client.utils.Serialization;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
/**
* If no {@link Executor} or {@link ExecutorSupplier} is specified, a default {@link ExecutorSupplier} will
* be used which creates an unbounded cached thread pool per client.
*/
public class KubernetesClientBuilder {
@FunctionalInterface
public interface ExecutorSupplier extends Supplier<Executor> {
default void onClose(Executor executor) {
}
}
private Config config;
private HttpClient.Factory factory;
private Class<KubernetesClient> clazz;
private ExecutorSupplier executorSupplier;
public KubernetesClientBuilder() {
// basically the same logic as in KubernetesResourceUtil for finding list types
// we're not guarding against a null context class loader
String className = "io.fabric8.kubernetes.client.impl.KubernetesClientImpl";
try {
clazz = (Class<KubernetesClient>) Thread.currentThread().getContextClassLoader().loadClass(className);
} catch (ClassNotFoundException | ClassCastException e) {
try {
clazz = (Class<KubernetesClient>) KubernetesClient.class.getClassLoader().loadClass(className);
} catch (Exception ex) {
throw KubernetesClientException.launderThrowable(ex);
}
}
}
public KubernetesClient build() {
if (config == null) {
config = new ConfigBuilder().build();
}
try {
if (factory == null) {
return clazz.getConstructor(Config.class).newInstance(config);
}
HttpClient client = factory.createHttpClient(config);
return clazz.getConstructor(HttpClient.class, Config.class, ExecutorSupplier.class).newInstance(client, config,
executorSupplier);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
throw KubernetesClientException.launderThrowable(e);
}
}
public KubernetesClientBuilder withConfig(Config config) {
this.config = config;
return this;
}
public KubernetesClientBuilder withConfig(String config) {
this.config = Serialization.unmarshal(config);
return this;
}
public KubernetesClientBuilder withConfig(InputStream config) {
this.config = Serialization.unmarshal(config);
return this;
}
public KubernetesClientBuilder withHttpClientFactory(HttpClient.Factory factory) {
this.factory = factory;
return this;
}
/**
* Configure the client to use the given executor for async tasks, such as {@link ResourceEventHandler}
* calls and writing to streams.
* <p>
* Only override if you need more control over the number of task threads used by the kubernetes client.
*
* @return this builder
*/
public KubernetesClientBuilder withTaskExecutor(Executor executor) {
this.executorSupplier = () -> executor;
return this;
}
/**
* Configure the client to use the given {@link ExecutorSupplier} for async tasks, such as {@link ResourceEventHandler}
* calls and writing to streams.
* <p>
* There will be a call to {@link ExecutorSupplier#onClose(Executor)} when a client is closed.
* <p>
* Only override if you need more control over the number of task threads used by the kubernetes client.
*
* @return this builder
*/
public KubernetesClientBuilder withTaskExecutorSupplier(ExecutorSupplier executorSupplier) {
this.executorSupplier = executorSupplier;
return this;
}
}