Skip to content

Commit 8bae9ab

Browse files
committed
fix: clean up controller default name generation
Fixes #279
1 parent 13fc2c1 commit 8bae9ab

File tree

5 files changed

+21
-23
lines changed

5 files changed

+21
-23
lines changed
Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package io.javaoperatorsdk.operator;
22

33
import io.fabric8.kubernetes.client.CustomResource;
4-
import io.javaoperatorsdk.operator.api.ResourceController;
5-
import java.util.Locale;
64

75
public class ControllerUtils {
86

@@ -16,20 +14,4 @@ public static boolean hasGivenFinalizer(CustomResource resource, String finalize
1614
return resource.getMetadata().getFinalizers() != null
1715
&& resource.getMetadata().getFinalizers().contains(finalizer);
1816
}
19-
20-
public static String getDefaultNameFor(ResourceController controller) {
21-
return getDefaultNameFor(controller.getClass());
22-
}
23-
24-
public static String getDefaultNameFor(Class<? extends ResourceController> controllerClass) {
25-
return getDefaultResourceControllerName(controllerClass.getSimpleName());
26-
}
27-
28-
public static String getDefaultResourceControllerName(String rcControllerClassName) {
29-
final var lastDot = rcControllerClassName.lastIndexOf('.');
30-
if (lastDot > 0) {
31-
rcControllerClassName = rcControllerClassName.substring(lastDot);
32-
}
33-
return rcControllerClassName.toLowerCase(Locale.ROOT);
34-
}
3517
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@
66

77
public interface ResourceController<R extends CustomResource> {
88

9+
static String getDefaultNameFor(ResourceController controller) {
10+
return getDefaultNameFor(controller.getClass());
11+
}
12+
13+
static String getDefaultNameFor(Class<? extends ResourceController> controllerClass) {
14+
return getDefaultResourceControllerName(controllerClass.getCanonicalName());
15+
}
16+
17+
static String getDefaultResourceControllerName(String rcControllerClassName) {
18+
if (rcControllerClassName.indexOf('.') < 0) {
19+
throw new IllegalArgumentException(
20+
"Must provide a fully-qualified resource controller class name, was: "
21+
+ rcControllerClassName);
22+
}
23+
return rcControllerClassName.toLowerCase(Locale.ROOT);
24+
}
25+
926
/**
1027
* The implementation should delete the associated component(s). Note that this is method is
1128
* called when an object is marked for deletion. After its executed the custom resource finalizer
@@ -52,6 +69,6 @@ default String getName() {
5269
}
5370

5471
// otherwise, use the lower-cased full class name
55-
return clazz.getCanonicalName().toLowerCase(Locale.ROOT);
72+
return getDefaultNameFor(this);
5673
}
5774
}

operator-framework-quarkus-extension/deployment/src/main/java/io/javaoperatorsdk/quarkus/extension/deployment/QuarkusExtensionProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private ControllerConfiguration createControllerConfiguration(
141141
controllerAnnotation,
142142
"name",
143143
AnnotationValue::asString,
144-
() -> ControllerUtils.getDefaultResourceControllerName(info.simpleName())),
144+
() -> ResourceController.getDefaultResourceControllerName(info.name().toString())),
145145
crdName,
146146
valueOrDefault(
147147
controllerAnnotation,

operator-framework-spring-boot-starter/src/test/java/io/javaoperatorsdk/operator/springboot/starter/AutoConfigurationTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66

77
import io.fabric8.kubernetes.client.KubernetesClient;
8-
import io.javaoperatorsdk.operator.ControllerUtils;
98
import io.javaoperatorsdk.operator.Operator;
109
import io.javaoperatorsdk.operator.api.ResourceController;
1110
import java.util.List;
@@ -41,7 +40,7 @@ public void loadsRetryPropertiesProperly() {
4140
final var retryProperties =
4241
config
4342
.getControllers()
44-
.get(ControllerUtils.getDefaultNameFor(TestController.class))
43+
.get(ResourceController.getDefaultNameFor(TestController.class))
4544
.getRetry();
4645
assertEquals(3, retryProperties.getMaxAttempts());
4746
assertEquals(1000, retryProperties.getInitialInterval());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public AnnotationConfiguration(ResourceController<R> controller) {
2222
@Override
2323
public String getName() {
2424
final var name = annotation.name();
25-
return Controller.NULL.equals(name) ? ControllerUtils.getDefaultNameFor(controller) : name;
25+
return Controller.NULL.equals(name) ? ResourceController.getDefaultNameFor(controller) : name;
2626
}
2727

2828
@Override

0 commit comments

Comments
 (0)