Skip to content

Commit bb3256a

Browse files
committed
wip
1 parent 189fd4d commit bb3256a

File tree

7 files changed

+88
-67
lines changed

7 files changed

+88
-67
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
import org.slf4j.LoggerFactory;
1111

1212
import io.fabric8.kubernetes.api.model.HasMetadata;
13-
import io.fabric8.kubernetes.client.ConfigBuilder;
1413
import io.fabric8.kubernetes.client.KubernetesClient;
15-
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
1614
import io.fabric8.kubernetes.client.Version;
1715
import io.javaoperatorsdk.operator.api.config.BaseConfigurationService;
1816
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
@@ -26,7 +24,7 @@
2624
@SuppressWarnings("rawtypes")
2725
public class Operator implements LifecycleAware {
2826
private static final Logger log = LoggerFactory.getLogger(Operator.class);
29-
private static final int DEFAULT_MAX_CONCURRENT_REQUEST = 512;
27+
3028
private final KubernetesClient kubernetesClient;
3129
private final ControllerManager controllerManager;
3230
private final LeaderElectionManager leaderElectionManager;
@@ -38,8 +36,8 @@ public Operator() {
3836
this((KubernetesClient) null);
3937
}
4038

41-
public Operator(KubernetesClient kubernetesClient) {
42-
this(kubernetesClient, new BaseConfigurationService());
39+
Operator(KubernetesClient kubernetesClient) {
40+
this(kubernetesClient, null);
4341
}
4442

4543
/**
@@ -48,39 +46,43 @@ public Operator(KubernetesClient kubernetesClient) {
4846
*/
4947
@Deprecated(forRemoval = true)
5048
public Operator(ConfigurationService configurationService) {
51-
this(null, configurationService);
49+
this(null, null);
5250
}
5351

5452
public Operator(Consumer<ConfigurationServiceOverrider> overrider) {
5553
this(null, overrider);
5654
}
5755

58-
public Operator(KubernetesClient client, Consumer<ConfigurationServiceOverrider> overrider) {
59-
this(client, ConfigurationService
60-
.newOverriddenConfigurationService(new BaseConfigurationService(), overrider));
61-
}
62-
6356
/**
6457
* Note that Operator by default closes the client on stop, this can be changed using
6558
* {@link ConfigurationService}
6659
*
67-
* @param kubernetesClient client to use to all Kubernetes related operations
68-
* @param configurationService provides configuration
60+
* @param client client to use to all Kubernetes related operations
61+
* @param overrider a {@link ConfigurationServiceOverrider} consumer used to override the default
62+
* {@link ConfigurationService} values
6963
*/
70-
public Operator(KubernetesClient kubernetesClient, ConfigurationService configurationService) {
71-
this.configurationService = configurationService;
64+
public Operator(KubernetesClient client, Consumer<ConfigurationServiceOverrider> overrider) {
65+
// initialize the client if the user didn't provide one
66+
if (client == null) {
67+
var configurationService = ConfigurationService.newOverriddenConfigurationService(new BaseConfigurationService(), overrider);
68+
client = configurationService.getKubernetesClient();
69+
}
70+
71+
this.kubernetesClient = client;
72+
73+
// override the configuration service to use the same client
74+
if (overrider != null) {
75+
overrider = overrider.andThen(o -> o.withKubernetesClient(this.kubernetesClient));
76+
} else {
77+
overrider = o -> o.withKubernetesClient(this.kubernetesClient);
78+
}
79+
this.configurationService = ConfigurationService.newOverriddenConfigurationService(new BaseConfigurationService(), overrider);
80+
7281
final var executorServiceManager = configurationService.getExecutorServiceManager();
7382
controllerManager = new ControllerManager(executorServiceManager);
74-
this.kubernetesClient =
75-
kubernetesClient != null ? kubernetesClient
76-
: new KubernetesClientBuilder()
77-
.withConfig(new ConfigBuilder()
78-
.withMaxConcurrentRequests(DEFAULT_MAX_CONCURRENT_REQUEST).build())
79-
.build();
80-
8183

8284
leaderElectionManager =
83-
new LeaderElectionManager(kubernetesClient, controllerManager, configurationService);
85+
new LeaderElectionManager(this.kubernetesClient, controllerManager, configurationService);
8486
}
8587

8688
/**

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationService.java

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111

1212
import io.fabric8.kubernetes.api.model.HasMetadata;
1313
import io.fabric8.kubernetes.client.Config;
14+
import io.fabric8.kubernetes.client.ConfigBuilder;
1415
import io.fabric8.kubernetes.client.CustomResource;
15-
import io.fabric8.kubernetes.client.utils.Serialization;
16+
import io.fabric8.kubernetes.client.KubernetesClient;
17+
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
18+
import io.fabric8.kubernetes.client.utils.KubernetesSerialization;
1619
import io.javaoperatorsdk.operator.api.monitoring.Metrics;
1720
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
1821
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResourceFactory;
1922
import io.javaoperatorsdk.operator.processing.dependent.workflow.ManagedWorkflowFactory;
2023

21-
import com.fasterxml.jackson.core.JsonProcessingException;
2224
import com.fasterxml.jackson.databind.ObjectMapper;
2325

2426
import static io.javaoperatorsdk.operator.api.config.ExecutorServiceManager.newThreadPoolExecutor;
@@ -28,6 +30,8 @@ public interface ConfigurationService {
2830

2931
Logger log = LoggerFactory.getLogger(ConfigurationService.class);
3032

33+
int DEFAULT_MAX_CONCURRENT_REQUEST = 512;
34+
3135
/**
3236
* Retrieves the configuration associated with the specified reconciler
3337
*
@@ -43,9 +47,44 @@ public interface ConfigurationService {
4347
*
4448
* @return the configuration of the Kubernetes client, defaulting to the provided
4549
* auto-configuration
50+
* @deprecated Configure your client as needed using {@link #getKubernetesClient()} or a
51+
* {@link ConfigurationServiceOverrider} to pass your own client instance, configured
52+
* as needed, instead
4653
*/
54+
@Deprecated(since = "4.4.0", forRemoval = true)
4755
default Config getClientConfiguration() {
48-
return Config.autoConfigure(null);
56+
return getKubernetesClient().getConfiguration();
57+
}
58+
59+
60+
ObjectMapper mapper = new ObjectMapper();
61+
default ObjectMapper getObjectMapper() {
62+
return mapper;
63+
}
64+
65+
/**
66+
* Used to clone custom resources. It is strongly suggested that implementors override this method
67+
* since the default implementation creates a new {@link Cloner} instance each time this method is
68+
* called.
69+
*
70+
* @return the configured {@link Cloner}
71+
*/
72+
default Cloner getResourceCloner() {
73+
return new Cloner() {
74+
@Override
75+
public <R extends HasMetadata> R clone(R object) {
76+
return getKubernetesClient().getKubernetesSerialization().clone(object);
77+
}
78+
};
79+
}
80+
81+
default KubernetesClient getKubernetesClient() {
82+
return new KubernetesClientBuilder()
83+
.withConfig(new ConfigBuilder(Config.autoConfigure(null))
84+
.withMaxConcurrentRequests(DEFAULT_MAX_CONCURRENT_REQUEST)
85+
.build())
86+
.withKubernetesSerialization(new KubernetesSerialization(getObjectMapper(), true))
87+
.build();
4988
}
5089

5190
/**
@@ -120,28 +159,6 @@ default int minConcurrentWorkflowExecutorThreads() {
120159
return MIN_DEFAULT_WORKFLOW_EXECUTOR_THREAD_NUMBER;
121160
}
122161

123-
/**
124-
* Used to clone custom resources. It is strongly suggested that implementors override this method
125-
* since the default implementation creates a new {@link Cloner} instance each time this method is
126-
* called.
127-
*
128-
* @return the configured {@link Cloner}
129-
*/
130-
default Cloner getResourceCloner() {
131-
return new Cloner() {
132-
@SuppressWarnings("unchecked")
133-
@Override
134-
public HasMetadata clone(HasMetadata object) {
135-
try {
136-
final var mapper = getObjectMapper();
137-
return mapper.readValue(mapper.writeValueAsString(object), object.getClass());
138-
} catch (JsonProcessingException e) {
139-
throw new IllegalStateException(e);
140-
}
141-
}
142-
};
143-
}
144-
145162
int DEFAULT_TERMINATION_TIMEOUT_SECONDS = 10;
146163

147164
/**
@@ -176,10 +193,6 @@ default boolean closeClientOnStop() {
176193
return true;
177194
}
178195

179-
default ObjectMapper getObjectMapper() {
180-
return Serialization.jsonMapper();
181-
}
182-
183196
@SuppressWarnings("rawtypes")
184197
default DependentResourceFactory dependentResourceFactory() {
185198
return DependentResourceFactory.DEFAULT;

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationServiceOverrider.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
import java.util.concurrent.ExecutorService;
77
import java.util.function.Consumer;
88

9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
912
import io.fabric8.kubernetes.client.Config;
13+
import io.fabric8.kubernetes.client.KubernetesClient;
1014
import io.javaoperatorsdk.operator.api.monitoring.Metrics;
1115

1216
import com.fasterxml.jackson.databind.ObjectMapper;
1317

1418
@SuppressWarnings("unused")
1519
public class ConfigurationServiceOverrider {
20+
21+
private static final Logger log = LoggerFactory.getLogger(ConfigurationServiceOverrider.class);
1622
private final ConfigurationService original;
1723
private Metrics metrics;
1824
private Config clientConfig;
@@ -25,6 +31,7 @@ public class ConfigurationServiceOverrider {
2531
private Integer timeoutSeconds;
2632
private Boolean closeClientOnStop;
2733
private ObjectMapper objectMapper;
34+
private KubernetesClient client;
2835
private ExecutorService executorService;
2936
private ExecutorService workflowExecutorService;
3037
private LeaderElectionConfiguration leaderElectionConfiguration;
@@ -113,6 +120,11 @@ public ConfigurationServiceOverrider withObjectMapper(ObjectMapper objectMapper)
113120
return this;
114121
}
115122

123+
public ConfigurationServiceOverrider withKubernetesClient(KubernetesClient client) {
124+
this.client = client;
125+
return this;
126+
}
127+
116128
public ConfigurationServiceOverrider withLeaderElectionConfiguration(
117129
LeaderElectionConfiguration leaderElectionConfiguration) {
118130
this.leaderElectionConfiguration = leaderElectionConfiguration;

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ResourceConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ static Set<String> ensureValidNamespaces(Collection<String> namespaces) {
114114
default Set<String> getEffectiveNamespaces(ConfigurationService configurationService) {
115115
var targetNamespaces = getNamespaces();
116116
if (watchCurrentNamespace()) {
117-
final String namespace =
118-
configurationService.getClientConfiguration().getNamespace();
117+
final String namespace = configurationService.getClientConfiguration().getNamespace();
119118
if (namespace == null) {
120119
throw new OperatorException(
121120
"Couldn't retrieve the currently connected namespace. Make sure it's correctly set in your ~/.kube/config file, using, e.g. 'kubectl config set-context <your context> --namespace=<your namespace>'");

operator-framework-junit5/src/main/java/io/javaoperatorsdk/operator/junit/AbstractOperatorExtension.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import io.fabric8.kubernetes.client.dsl.Resource;
2828
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil;
2929
import io.fabric8.kubernetes.client.utils.Utils;
30-
import io.javaoperatorsdk.operator.api.config.BaseConfigurationService;
31-
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
3230
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceOverrider;
3331

3432
public abstract class AbstractOperatorExtension implements HasKubernetesClient,
@@ -221,7 +219,7 @@ public static abstract class AbstractBuilder<T extends AbstractBuilder<T>> {
221219
protected boolean waitForNamespaceDeletion;
222220
protected boolean oneNamespacePerClass;
223221
protected int namespaceDeleteTimeout;
224-
protected ConfigurationService configurationService = new BaseConfigurationService();
222+
protected Consumer<ConfigurationServiceOverrider> configurationServiceOverrider;
225223

226224
protected AbstractBuilder() {
227225
this.infrastructure = new ArrayList<>();
@@ -260,8 +258,7 @@ public T oneNamespacePerClass(boolean value) {
260258
}
261259

262260
public T withConfigurationService(Consumer<ConfigurationServiceOverrider> overrider) {
263-
configurationService =
264-
ConfigurationService.newOverriddenConfigurationService(configurationService, overrider);
261+
configurationServiceOverrider = overrider;
265262
return (T) this;
266263
}
267264

operator-framework-junit5/src/main/java/io/javaoperatorsdk/operator/junit/LocallyRunOperatorExtension.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import io.javaoperatorsdk.operator.Operator;
2525
import io.javaoperatorsdk.operator.ReconcilerUtils;
2626
import io.javaoperatorsdk.operator.RegisteredController;
27-
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
27+
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceOverrider;
2828
import io.javaoperatorsdk.operator.api.config.ControllerConfigurationOverrider;
2929
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
3030
import io.javaoperatorsdk.operator.processing.retry.Retry;
@@ -53,7 +53,7 @@ private LocallyRunOperatorExtension(
5353
boolean waitForNamespaceDeletion,
5454
boolean oneNamespacePerClass,
5555
KubernetesClient kubernetesClient,
56-
ConfigurationService configurationService) {
56+
Consumer<ConfigurationServiceOverrider> configurationServiceOverrider) {
5757
super(
5858
infrastructure,
5959
infrastructureTimeout,
@@ -65,7 +65,7 @@ private LocallyRunOperatorExtension(
6565
this.portForwards = portForwards;
6666
this.localPortForwards = new ArrayList<>(portForwards.size());
6767
this.additionalCustomResourceDefinitions = additionalCustomResourceDefinitions;
68-
this.operator = new Operator(getKubernetesClient(), configurationService);
68+
this.operator = new Operator(getKubernetesClient(), configurationServiceOverrider);
6969
this.registeredControllers = new HashMap<>();
7070
}
7171

@@ -289,7 +289,7 @@ public LocallyRunOperatorExtension build() {
289289
waitForNamespaceDeletion,
290290
oneNamespacePerClass,
291291
kubernetesClient,
292-
configurationService);
292+
configurationServiceOverrider);
293293
}
294294
}
295295

sample-operators/tomcat-operator/src/main/java/io/javaoperatorsdk/operator/sample/TomcatOperator.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.takes.http.Exit;
1010
import org.takes.http.FtBasic;
1111

12-
import io.fabric8.kubernetes.client.*;
1312
import io.javaoperatorsdk.operator.Operator;
1413

1514
public class TomcatOperator {
@@ -18,10 +17,9 @@ public class TomcatOperator {
1817

1918
public static void main(String[] args) throws IOException {
2019

21-
KubernetesClient client = new KubernetesClientBuilder().build();
22-
Operator operator = new Operator(client);
20+
Operator operator = new Operator();
2321
operator.register(new TomcatReconciler());
24-
operator.register(new WebappReconciler(client));
22+
operator.register(new WebappReconciler(operator.getKubernetesClient()));
2523
operator.start();
2624

2725
new FtBasic(new TkFork(new FkRegex("/health", "ALL GOOD.")), 8080).start(Exit.NEVER);

0 commit comments

Comments
 (0)