Skip to content

Commit be02fbc

Browse files
authored
Merge pull request operator-framework#300 from java-operator-sdk/stricter-name
fix: stricter controller name validation to avoid collisions
2 parents 8fbd10d + fc74c7a commit be02fbc

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

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

+13-8
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,28 @@
88

99
public abstract class AbstractConfigurationService implements ConfigurationService {
1010

11-
protected final Map<String, ControllerConfiguration> configurations = new ConcurrentHashMap<>();
11+
private final Map<String, ControllerConfiguration> configurations = new ConcurrentHashMap<>();
1212

1313
protected <R extends CustomResource> void register(ControllerConfiguration<R> config) {
1414
final var name = config.getName();
1515
final var existing = configurations.get(name);
1616
if (existing != null) {
17-
throw new IllegalArgumentException(
18-
"Controller name '"
19-
+ name
20-
+ "' is used by both "
21-
+ existing.getAssociatedControllerClassName()
22-
+ " and "
23-
+ config.getAssociatedControllerClassName());
17+
throwExceptionOnNameCollision(config.getAssociatedControllerClassName(), existing);
2418
}
2519
configurations.put(name, config);
2620
}
2721

22+
protected void throwExceptionOnNameCollision(
23+
String newControllerClassName, ControllerConfiguration existing) {
24+
throw new IllegalArgumentException(
25+
"Controller name '"
26+
+ existing.getName()
27+
+ "' is used by both "
28+
+ existing.getAssociatedControllerClassName()
29+
+ " and "
30+
+ newControllerClassName);
31+
}
32+
2833
@Override
2934
public <R extends CustomResource> ControllerConfiguration<R> getConfigurationFor(
3035
ResourceController<R> controller) {

operator-framework/src/main/java/io/javaoperatorsdk/operator/config/runtime/DefaultConfigurationService.java

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public <R extends CustomResource> ControllerConfiguration<R> getConfigurationFor
2222
// create the the configuration on demand and register it
2323
config = new AnnotationConfiguration(controller);
2424
register(config);
25+
} else {
26+
// check that we don't have a controller name collision
27+
final var newControllerClassName = controller.getClass().getCanonicalName();
28+
if (!config.getAssociatedControllerClassName().equals(newControllerClassName)) {
29+
throwExceptionOnNameCollision(newControllerClassName, config);
30+
}
2531
}
2632
return config;
2733
}

operator-framework/src/test/java/io/javaoperatorsdk/operator/config/runtime/DefaultConfigurationServiceTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public UpdateControl<TestCustomFinalizerController.InnerCustomResource> createOr
7272

7373
@Controller(
7474
generationAwareEventProcessing = false,
75-
crdName = TestCustomResourceController.CRD_NAME)
75+
crdName = TestCustomResourceController.CRD_NAME,
76+
name = "test")
7677
static class TestCustomResourceController implements ResourceController<TestCustomResource> {
7778

7879
public static final String CRD_NAME = "customservices.sample.javaoperatorsdk";

0 commit comments

Comments
 (0)