Skip to content

Commit 869f7c6

Browse files
metacosmcsviri
authored andcommitted
fix: default implementations shouldn't always recreate a client (#1959)
1 parent f448a3d commit 869f7c6

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/AbstractConfigurationService.java

+41-7
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,44 @@
66
import java.util.stream.Stream;
77

88
import io.fabric8.kubernetes.api.model.HasMetadata;
9+
import io.fabric8.kubernetes.client.KubernetesClient;
910
import io.javaoperatorsdk.operator.ReconcilerUtils;
1011
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
1112

1213
@SuppressWarnings("rawtypes")
1314
public class AbstractConfigurationService implements ConfigurationService {
1415
private final Map<String, ControllerConfiguration> configurations = new ConcurrentHashMap<>();
1516
private final Version version;
17+
private KubernetesClient client;
1618
private Cloner cloner;
1719
private ExecutorServiceManager executorServiceManager;
1820

1921
public AbstractConfigurationService(Version version) {
20-
this(version, null, null);
22+
this(version, null);
2123
}
2224

2325
public AbstractConfigurationService(Version version, Cloner cloner) {
24-
this(version, cloner, null);
26+
this(version, cloner, null, null);
2527
}
2628

29+
/**
30+
* Creates a new {@link AbstractConfigurationService} with the specified parameters.
31+
*
32+
* @param client the {@link KubernetesClient} instance to use to connect to the cluster, if let
33+
* {@code null}, the client will be lazily instantiated with the default configuration
34+
* provided by {@link ConfigurationService#getKubernetesClient()} the first time
35+
* {@link #getKubernetesClient()} is called
36+
* @param version the version information
37+
* @param cloner the {@link Cloner} to use, if {@code null} the default provided by
38+
* {@link ConfigurationService#getResourceCloner()} will be used
39+
* @param executorServiceManager the {@link ExecutorServiceManager} instance to be used, can be
40+
* {@code null} to lazily initialize one by default when
41+
* {@link #getExecutorServiceManager()} is called
42+
*/
2743
public AbstractConfigurationService(Version version, Cloner cloner,
28-
ExecutorServiceManager executorServiceManager) {
44+
ExecutorServiceManager executorServiceManager, KubernetesClient client) {
2945
this.version = version;
30-
init(cloner, executorServiceManager);
46+
init(cloner, executorServiceManager, client);
3147
}
3248

3349
/**
@@ -36,10 +52,19 @@ public AbstractConfigurationService(Version version, Cloner cloner,
3652
* is useful in situations where the cloner depends on a mapper that might require additional
3753
* configuration steps before it's ready to be used.
3854
*
39-
* @param cloner the {@link Cloner} instance to be used
40-
* @param executorServiceManager the {@link ExecutorServiceManager} instance to be used
55+
* @param cloner the {@link Cloner} instance to be used, if {@code null}, the default provided by
56+
* {@link ConfigurationService#getResourceCloner()} will be used
57+
* @param executorServiceManager the {@link ExecutorServiceManager} instance to be used, can be
58+
* {@code null} to lazily initialize one by default when
59+
* {@link #getExecutorServiceManager()} is called
60+
* @param client the {@link KubernetesClient} instance to use to connect to the cluster, if let
61+
* {@code null}, the client will be lazily instantiated with the default configuration
62+
* provided by {@link ConfigurationService#getKubernetesClient()} the first time
63+
* {@link #getKubernetesClient()} is called
4164
*/
42-
protected void init(Cloner cloner, ExecutorServiceManager executorServiceManager) {
65+
protected void init(Cloner cloner, ExecutorServiceManager executorServiceManager,
66+
KubernetesClient client) {
67+
this.client = client;
4368
this.cloner = cloner != null ? cloner : ConfigurationService.super.getResourceCloner();
4469
this.executorServiceManager = executorServiceManager;
4570
}
@@ -127,6 +152,15 @@ public Cloner getResourceCloner() {
127152
return cloner;
128153
}
129154

155+
@Override
156+
public KubernetesClient getKubernetesClient() {
157+
// lazy init to avoid needing initializing a client when not needed (in tests, in particular)
158+
if (client == null) {
159+
client = ConfigurationService.super.getKubernetesClient();
160+
}
161+
return client;
162+
}
163+
130164
@Override
131165
public ExecutorServiceManager getExecutorServiceManager() {
132166
// lazy init to avoid initializing thread pools for nothing in an overriding scenario

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/BaseConfigurationService.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.slf4j.LoggerFactory;
1414

1515
import io.fabric8.kubernetes.api.model.HasMetadata;
16+
import io.fabric8.kubernetes.client.KubernetesClient;
1617
import io.fabric8.kubernetes.client.informers.cache.ItemStore;
1718
import io.javaoperatorsdk.operator.OperatorException;
1819
import io.javaoperatorsdk.operator.ReconcilerUtils;
@@ -40,11 +41,15 @@ public class BaseConfigurationService extends AbstractConfigurationService {
4041
private static final Logger logger = LoggerFactory.getLogger(LOGGER_NAME);
4142

4243
public BaseConfigurationService(Version version) {
43-
super(version);
44+
this(version, null);
4445
}
4546

4647
public BaseConfigurationService(Version version, Cloner cloner) {
47-
super(version, cloner);
48+
this(version, cloner, null);
49+
}
50+
51+
public BaseConfigurationService(Version version, Cloner cloner, KubernetesClient client) {
52+
super(version, cloner, null, client);
4853
}
4954

5055
public BaseConfigurationService() {

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public ConfigurationServiceOverrider withSSABasedDefaultMatchingForDependentReso
158158
}
159159

160160
public ConfigurationService build() {
161-
return new BaseConfigurationService(original.getVersion(), cloner) {
161+
return new BaseConfigurationService(original.getVersion(), cloner, client) {
162162
@Override
163163
public Set<String> getKnownReconcilerNames() {
164164
return original.getKnownReconcilerNames();
@@ -228,11 +228,6 @@ public ExecutorService getWorkflowExecutorService() {
228228
: super.getWorkflowExecutorService();
229229
}
230230

231-
@Override
232-
public KubernetesClient getKubernetesClient() {
233-
return client != null ? client : original.getKubernetesClient();
234-
}
235-
236231
@Override
237232
public Optional<LeaderElectionConfiguration> getLeaderElectionConfiguration() {
238233
return leaderElectionConfiguration != null ? Optional.of(leaderElectionConfiguration)

0 commit comments

Comments
 (0)