Skip to content

Commit 319ebee

Browse files
authored
[issue-687] Refactor/operator register api (#688)
1 parent d842c1f commit 319ebee

File tree

2 files changed

+102
-27
lines changed

2 files changed

+102
-27
lines changed

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

+23-27
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void start() {
7070

7171
log.info("Client version: {}", Version.clientVersion());
7272
try {
73-
final var k8sVersion = kubernetesClient.getVersion();
73+
final var k8sVersion = kubernetesClient.getKubernetesVersion();
7474
if (k8sVersion != null) {
7575
log.info("Server version: {}.{}", k8sVersion.getMajor(), k8sVersion.getMinor());
7676
}
@@ -110,13 +110,14 @@ public void close() {
110110
* Add a registration requests for the specified controller with this operator. The effective
111111
* registration of the controller is delayed till the operator is started.
112112
*
113-
* @param controller the controller to register
113+
* @param reconciler the controller to register
114114
* @param <R> the {@code CustomResource} type associated with the controller
115115
* @throws OperatorException if a problem occurred during the registration process
116116
*/
117-
public <R extends HasMetadata> void register(Reconciler<R> controller)
117+
public <R extends HasMetadata> void register(Reconciler<R> reconciler)
118118
throws OperatorException {
119-
register(controller, null);
119+
final var defaultConfiguration = configurationService.getConfigurationFor(reconciler);
120+
register(reconciler, defaultConfiguration);
120121
}
121122

122123
/**
@@ -127,39 +128,34 @@ public <R extends HasMetadata> void register(Reconciler<R> controller)
127128
* controller is delayed till the operator is started.
128129
*
129130
* @param reconciler part of the controller to register
130-
* @param configuration the configuration with which we want to register the controller, if {@code
131-
* null}, the controller's original configuration is used
131+
* @param configuration the configuration with which we want to register the controller
132132
* @param <R> the {@code CustomResource} type associated with the controller
133133
* @throws OperatorException if a problem occurred during the registration process
134134
*/
135-
public <R extends HasMetadata> void register(
136-
Reconciler<R> reconciler, ControllerConfiguration<R> configuration)
135+
public <R extends HasMetadata> void register(Reconciler<R> reconciler,
136+
ControllerConfiguration<R> configuration)
137137
throws OperatorException {
138-
final var existing = configurationService.getConfigurationFor(reconciler);
139-
if (existing == null) {
138+
139+
if (configuration == null) {
140140
throw new OperatorException(
141141
"Cannot register controller with name " + reconciler.getClass().getCanonicalName() +
142142
" controller named " + ControllerUtils.getNameFor(reconciler)
143143
+ " because its configuration cannot be found.\n" +
144144
" Known controllers are: " + configurationService.getKnownControllerNames());
145-
} else {
146-
if (configuration == null) {
147-
configuration = existing;
148-
}
149-
final var controller =
150-
new Controller<>(reconciler, configuration, kubernetesClient);
151-
controllers.add(controller);
152-
153-
final var watchedNS =
154-
configuration.watchAllNamespaces()
155-
? "[all namespaces]"
156-
: configuration.getEffectiveNamespaces();
157-
log.info(
158-
"Registered Controller: '{}' for CRD: '{}' for namespace(s): {}",
159-
configuration.getName(),
160-
configuration.getResourceClass(),
161-
watchedNS);
162145
}
146+
147+
final var controller = new Controller<>(reconciler, configuration, kubernetesClient);
148+
149+
controllers.add(controller);
150+
151+
final var watchedNS = configuration.watchAllNamespaces() ? "[all namespaces]"
152+
: configuration.getEffectiveNamespaces();
153+
154+
log.info(
155+
"Registered Controller: '{}' for CRD: '{}' for namespace(s): {}",
156+
configuration.getName(),
157+
configuration.getResourceClass(),
158+
watchedNS);
163159
}
164160

165161
static class ControllerManager implements LifecycleAware {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package io.javaoperatorsdk.operator;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
7+
import io.fabric8.kubernetes.client.CustomResource;
8+
import io.fabric8.kubernetes.client.KubernetesClient;
9+
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
10+
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
11+
import io.javaoperatorsdk.operator.api.reconciler.Context;
12+
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
13+
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
14+
15+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
16+
import static org.mockito.Mockito.mock;
17+
import static org.mockito.Mockito.verify;
18+
import static org.mockito.Mockito.when;
19+
20+
class OperatorTest {
21+
22+
private final KubernetesClient kubernetesClient = mock(KubernetesClient.class);
23+
private final ConfigurationService configurationService = mock(ConfigurationService.class);
24+
private final ControllerConfiguration configuration = mock(ControllerConfiguration.class);
25+
26+
private final Operator operator = new Operator(kubernetesClient, configurationService);
27+
private final FooReconciler fooReconciler = FooReconciler.create();
28+
29+
@Test
30+
@DisplayName("should register `Reconciler` to Controller")
31+
public void shouldRegisterReconcilerToController() {
32+
// given
33+
when(configurationService.getConfigurationFor(fooReconciler)).thenReturn(configuration);
34+
when(configuration.watchAllNamespaces()).thenReturn(true);
35+
when(configuration.getName()).thenReturn("FOO");
36+
when(configuration.getResourceClass()).thenReturn(FooReconciler.class);
37+
38+
// when
39+
operator.register(fooReconciler);
40+
41+
// then
42+
verify(configuration).watchAllNamespaces();
43+
verify(configuration).getName();
44+
verify(configuration).getResourceClass();
45+
46+
assertThat(operator.getControllers().size()).isEqualTo(1);
47+
assertThat(operator.getControllers().get(0).getReconciler()).isEqualTo(fooReconciler);
48+
}
49+
50+
@Test
51+
@DisplayName("should throw `OperationException` when Configuration is null")
52+
public void shouldThrowOperatorExceptionWhenConfigurationIsNull() {
53+
Assertions.assertThrows(OperatorException.class, () -> operator.register(fooReconciler, null));
54+
}
55+
56+
private static class FooCustomResource extends CustomResource<FooSpec, FooStatus> {
57+
}
58+
59+
private static class FooSpec {
60+
}
61+
62+
private static class FooStatus {
63+
}
64+
65+
private static class FooReconciler implements Reconciler<FooCustomResource> {
66+
67+
private FooReconciler() {}
68+
69+
public static FooReconciler create() {
70+
return new FooReconciler();
71+
}
72+
73+
@Override
74+
public UpdateControl<FooCustomResource> reconcile(FooCustomResource resource, Context context) {
75+
return UpdateControl.noUpdate();
76+
}
77+
}
78+
79+
}

0 commit comments

Comments
 (0)