Skip to content

Commit 5921a6c

Browse files
committed
feat!: allow config override when registering a controller
1 parent 12abb42 commit 5921a6c

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.fabric8.kubernetes.client.dsl.MixedOperation;
66
import io.javaoperatorsdk.operator.api.ResourceController;
77
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
8+
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
89
import io.javaoperatorsdk.operator.processing.CustomResourceCache;
910
import io.javaoperatorsdk.operator.processing.DefaultEventHandler;
1011
import io.javaoperatorsdk.operator.processing.EventDispatcher;
@@ -30,31 +31,30 @@ public Operator(KubernetesClient k8sClient, ConfigurationService configurationSe
3031

3132
public <R extends CustomResource> void register(ResourceController<R> controller)
3233
throws OperatorException {
33-
final var configuration = configurationService.getConfigurationFor(controller);
34-
if (configuration == null) {
34+
register(controller, null);
35+
}
36+
37+
public <R extends CustomResource> void register(
38+
ResourceController<R> controller, ControllerConfiguration<R> configuration)
39+
throws OperatorException {
40+
final var existing = configurationService.getConfigurationFor(controller);
41+
if (existing == null) {
3542
log.warn(
3643
"Skipping registration of {} controller named {} because its configuration cannot be found.\n"
3744
+ "Known controllers are: {}",
3845
controller.getClass().getCanonicalName(),
3946
ControllerUtils.getNameFor(controller),
4047
configurationService.getKnownControllerNames());
4148
} else {
49+
if (configuration == null) {
50+
configuration = existing;
51+
}
4252
final var retry = GenericRetry.fromConfiguration(configuration.getRetryConfiguration());
4353
final var targetNamespaces = configuration.getNamespaces().toArray(new String[] {});
4454
registerController(controller, configuration.watchAllNamespaces(), retry, targetNamespaces);
4555
}
4656
}
4757

48-
// currently only used by IntegrationTestSupport but this should also disappear as by default the
49-
// controllers should register into the current namespace the client is connected to which should
50-
// work as expected for tests, in particular if we want to start testing using random namespace
51-
// names
52-
<R extends CustomResource> void registerController(
53-
ResourceController<R> controller, Retry retry, String... targetNamespaces)
54-
throws OperatorException {
55-
registerController(controller, false, retry, targetNamespaces);
56-
}
57-
5858
@SuppressWarnings("rawtypes")
5959
private <R extends CustomResource> void registerController(
6060
ResourceController<R> controller,

operator-framework/src/test/java/io/javaoperatorsdk/operator/IntegrationTestSupport.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
import io.fabric8.kubernetes.client.KubernetesClient;
1313
import io.fabric8.kubernetes.client.dsl.MixedOperation;
1414
import io.fabric8.kubernetes.client.dsl.Resource;
15-
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
1615
import io.fabric8.kubernetes.client.utils.Serialization;
1716
import io.javaoperatorsdk.operator.api.ResourceController;
17+
import io.javaoperatorsdk.operator.api.config.ControllerConfigurationOverrider;
1818
import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService;
1919
import io.javaoperatorsdk.operator.processing.retry.Retry;
2020
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource;
@@ -46,8 +46,7 @@ public void initialize(
4646
KubernetesClient k8sClient, ResourceController controller, String crdPath, Retry retry) {
4747
log.info("Initializing integration test in namespace {}", TEST_NAMESPACE);
4848
this.k8sClient = k8sClient;
49-
CustomResourceDefinition crd = loadCRDAndApplyToCluster(crdPath);
50-
CustomResourceDefinitionContext crdContext = CustomResourceDefinitionContext.fromCrd(crd);
49+
loadCRDAndApplyToCluster(crdPath);
5150
this.controller = controller;
5251

5352
final var configurationService = DefaultConfigurationService.instance();
@@ -56,16 +55,18 @@ public void initialize(
5655
final var customResourceClass = config.getCustomResourceClass();
5756
this.crOperations = k8sClient.customResources(customResourceClass);
5857

59-
if (k8sClient.namespaces().withName(TEST_NAMESPACE).get() == null) {
60-
k8sClient
61-
.namespaces()
62-
.create(
63-
new NamespaceBuilder()
64-
.withMetadata(new ObjectMetaBuilder().withName(TEST_NAMESPACE).build())
65-
.build());
58+
final var namespaces = k8sClient.namespaces();
59+
if (namespaces.withName(TEST_NAMESPACE).get() == null) {
60+
namespaces.create(
61+
new NamespaceBuilder().withNewMetadata().withName(TEST_NAMESPACE).endMetadata().build());
6662
}
6763
operator = new Operator(k8sClient, configurationService);
68-
operator.registerController(controller, retry, TEST_NAMESPACE);
64+
final var overriddenConfig =
65+
ControllerConfigurationOverrider.override(config).settingNamespace(TEST_NAMESPACE);
66+
if (retry != null) {
67+
overriddenConfig.withRetry(retry);
68+
}
69+
operator.register(controller, overriddenConfig.build());
6970
log.info("Operator is running with {}", controller.getClass().getCanonicalName());
7071
}
7172

0 commit comments

Comments
 (0)