8000 Merge pull request #303 from java-operator-sdk/clean-register · r00ta/java-operator-sdk@be912b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit be912b7

Browse files
authored
Merge pull request operator-framework#303 from java-operator-sdk/clean-register
Simplify controller registration and unify controller configuration
2 parents 623b510 + 456ea3c commit be912b7

File tree

18 files changed

+217
-123
lines changed

18 files changed

+217
-123
lines changed

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

Lines changed: 12 additions & 23 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,42 +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-
public <R extends CustomResource> void registerControllerForAllNamespaces(
49-
ResourceController<R> controller, Retry retry) throws OperatorException {
50-
registerController(controller, true, retry);
51-
}
52-
53-
public <R extends CustomResource> void registerControllerForAllNamespaces(
54-
ResourceController<R> controller) throws OperatorException {
55-
registerController(controller, true, null);
56-
}
57-
58-
public <R extends CustomResource> void registerController(
59-
ResourceController<R> controller, Retry retry, String... targetNamespaces)
60-
throws OperatorException {
61-
registerController(controller, false, retry, targetNamespaces);
62-
}
63-
64-
public <R extends CustomResource> void registerController(
65-
ResourceController<R> controller, String... targetNamespaces) throws OperatorException {
66-
registerController(controller, false, null, targetNamespaces);
67-
}
68-
6958
@SuppressWarnings("rawtypes")
7059
private <R extends CustomResource> void registerController(
7160
ResourceController<R> controller,

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@
2828

2929
/**
3030
* Specified which namespaces this Controller monitors for custom resources events. If no
31-
* namespace is specified then the controller will monitor the namespace it is deployed in (or the
32-
* namespace to which the Kubernetes client is connected to). To specify that the controller needs
33-
* to monitor all namespaces, add {@link
34-
* io.javaoperatorsdk.operator.api.config.ControllerConfiguration#WATCH_ALL_NAMESPACES_MARKER} to
35-
* this field.
31+
* namespace is specified then the controller will monitor all namespaces by default.
3632
*
3733
* @return the list of namespaces this controller monitors
3834
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package io.javaoperatorsdk.operator.api.config;
2+
3+
import io.fabric8.kubernetes.client.CustomResource;
4+
import java.util.Collections;
5+
import java.util.Set;
6+
7+
public abstract class AbstractControllerConfiguration<R extends CustomResource>
8+
implements ControllerConfiguration<R> {
9+
10+
private final String associatedControllerClassName;
11+
private final String name;
12+
private final String crdName;
13+
private final String finalizer;
14+
private final boolean generationAware;
15+
private final Set<String> namespaces;
16+
private final boolean watchAllNamespaces;
17+
private final RetryConfiguration retryConfiguration;
18+
19+
public AbstractControllerConfiguration(
20+
String associatedControllerClassName,
21+
String name,
22+
String crdName,
23+
String finalizer,
24+
boolean generationAware,
25+
Set<String> namespaces,
26+
RetryConfiguration retryConfiguration) {
27+
this.associatedControllerClassName = associatedControllerClassName;
28+
this.name = name;
29+
this.crdName = crdName;
30+
this.finalizer = finalizer;
31+
this.generationAware = generationAware;
32+
this.namespaces =
33+
namespaces != null ? Collections.unmodifiableSet(namespaces) : Collections.emptySet();
34+
this.watchAllNamespaces = this.namespaces.isEmpty();
35+
this.retryConfiguration =
36+
retryConfiguration == null
37+
? ControllerConfiguration.super.getRetryConfiguration()
38+
: retryConfiguration;
39+
}
40+
41+
@Override
42+
public String getName() {
43+
return name;
44+
}
45+
46+
@Override
47+
public String getCRDName() {
48+
return crdName;
49+
}
50+
51+
@Override
52+
public String getFinalizer() {
53+
return finalizer;
54+
}
55+
56+
@Override
57+
public boolean isGenerationAware() {
58+
return generationAware;
59+
}
60+
61+
@Override
62+
public String getAssociatedControllerClassName() {
63+
return associatedControllerClassName;
64+
}
65+
66+
@Override
67+
public Set<String> getNamespaces() {
68+
return namespaces;
69+
}
70+
71+
@Override
72+
public boolean watchAllNamespaces() {
73+
return watchAllNamespaces;
74+
}
75+
76+
@Override
77+
public RetryConfiguration getRetryConfiguration() {
78+
return retryConfiguration;
79+
}
80+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ default Set<String> getNamespaces() {
2525
}
2626

2727
default boolean watchAllNamespaces() {
28-
return getNamespaces().contains(WATCH_ALL_NAMESPACES_MARKER);
28+
return getNamespaces().isEmpty();
2929
}
3030

3131
default RetryConfiguration getRetryConfiguration() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package io.javaoperatorsdk.operator.api.config;
2+
3+
import io.fabric8.kubernetes.client.CustomResource;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
public class ControllerConfigurationOverrider<R extends CustomResource> {
9+
10+
private String finalizer;
11+
private boolean generationAware;
12+
private Set<String> namespaces;
13+
private RetryConfiguration retry;
14+
private final ControllerConfiguration<R> original;
15+
16+
private ControllerConfigurationOverrider(ControllerConfiguration<R> original) {
17+
finalizer = original.getFinalizer();
18+
generationAware = original.isGenerationAware();
19+
namespaces = new HashSet<>(original.getNamespaces());
20+
retry = original.getRetryConfiguration();
21+
this.original = original;
22+
}
23+
24+
public ControllerConfigurationOverrider<R> withFinalizer(String finalizer) {
25+
this.finalizer = finalizer;
26+
return this;
27+
}
28+
29+
public ControllerConfigurationOverrider<R> withGenerationAware(boolean generationAware) {
30+
this.generationAware = generationAware;
31+
return this;
32+
}
33+
34+
public ControllerConfigurationOverrider<R> withCurrentNamespace() {
35+
namespaces.clear();
36+
return this;
37+
}
38+
39+
public ControllerConfigurationOverrider<R> addingNamespaces(String... namespaces) {
40+
this.namespaces.addAll(List.of(namespaces));
41+
return this;
42+
}
43+
44+
public ControllerConfigurationOverrider<R> removingNamespaces(String... namespaces) {
45+
this.namespaces.removeAll(List.of(namespaces));
46+
return this;
47+
}
48+
49+
public ControllerConfigurationOverrider<R> settingNamespace(String namespace) {
50+
this.namespaces.clear();
51+
this.namespaces.add(namespace);
52+
return this;
53+
}
54+
55+
public ControllerConfigurationOverrider<R> withRetry(RetryConfiguration retry) {
56+
this.retry = retry;
57+
return this;
58+
}
59+
60+
public ControllerConfiguration<R> build() {
61+
return new AbstractControllerConfiguration<R>(
62+
original.getAssociatedControllerClassName(),
63+
original.getName(),
64+
original.getCRDName(),
65+
finalizer,
66+
generationAware,
67+
namespaces,
68+
retry) {
69+
@Override
70+
public Class<R> getCustomResourceClass() {
71+
return original.getCustomResourceClass();
72+
}
73+
};
74+
}
75+
76+
public static <R extends CustomResource> ControllerConfigurationOverrider<R> override(
77+
ControllerConfiguration<R> original) {
78+
return new ControllerConfigurationOverrider<>(original);
79+
}
80+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ public interface RetryConfiguration {
66

77
int DEFAULT_MAX_ATTEMPTS = 5;
88
long DEFAULT_INITIAL_INTERVAL = 2000L;
9-
109
double DEFAULT_MULTIPLIER = 1.5D;
1110

1211
default int getMaxAttempts() {

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/retry/GenericRetry.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
import io.javaoperatorsdk.operator.api.config.RetryConfiguration;
44

55
public class GenericRetry implements Retry {
6-
7-
public static final int DEFAULT_MAX_ATTEMPTS = 5;
8-
public static final long DEFAULT_INITIAL_INTERVAL = 2000L;
9-
public static final double DEFAULT_MULTIPLIER = 1.5D;
10-
116
private int maxAttempts = DEFAULT_MAX_ATTEMPTS;
127
private long initialInterval = DEFAULT_INITIAL_INTERVAL;
138
private double intervalMultiplier = DEFAULT_MULTIPLIER;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.javaoperatorsdk.operator.processing.retry;
22

3-
public interface Retry {
3+
import io.javaoperatorsdk.operator.api.config.RetryConfiguration;
4+
5+
public interface Retry extends RetryConfiguration {
46

57
RetryExecution initExecution();
68
}

operator-framework-quarkus-extension/runtime/src/main/java/io/javaoperatorsdk/quarkus/extension/ExternalControllerConfiguration.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.javaoperatorsdk.quarkus.extension;
22

3-
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
43
import io.quarkus.runtime.annotations.ConfigGroup;
54
import io.quarkus.runtime.annotations.ConfigItem;
65
import java.util.List;
@@ -10,9 +9,8 @@
109
public class ExternalControllerConfiguration {
1110

1211
/**
13-
* An optional list of comma-separated namespace names the controller should watch. If the list
14-
* contains {@link ControllerConfiguration#WATCH_ALL_NAMESPACES_MARKER} then the controller will
15-
* watch all namespaces.
12+
* An optional list of comma-separated namespace names the controller should watch. If this
13+
* property is left empty then the controller will watch all namespaces.
1614
*/
1715
@ConfigItem public Optional<List<String>> namespaces;
1816

0 commit comments

Comments
 (0)
0