Skip to content

Commit 9482857

Browse files
author
Soroosh Sarabadani
committed
load controllerToCustomResourceMappings map
1 parent 0f110e1 commit 9482857

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

operator-framework/src/main/java/io/javaoperatorsdk/operator/ControllerUtils.java

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,49 @@
44
import io.fabric8.kubernetes.client.CustomResourceDoneable;
55
import io.javaoperatorsdk.operator.api.Controller;
66
import io.javaoperatorsdk.operator.api.ResourceController;
7+
import org.apache.commons.lang3.ClassUtils;
78

8-
import java.lang.reflect.ParameterizedType;
9-
import java.util.Arrays;
9+
import java.io.IOException;
10+
import java.net.URL;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.util.*;
14+
import java.util.stream.Collectors;
1015

1116

1217
public class ControllerUtils {
1318

1419
private static final String FINALIZER_NAME_SUFFIX = "/finalizer";
20+
private static Map<Class<? extends ResourceController>, Class<? extends CustomResource>> controllerToCustomResourceMappings = new HashMap();
21+
22+
static {
23+
try {
24+
final Enumeration<URL> customResourcesMetadaList = ControllerUtils.class.getClassLoader().getResources("javaoperatorsdk-custom-resources");
25+
for (Iterator<URL> it = customResourcesMetadaList.asIterator(); it.hasNext(); ) {
26+
URL url = it.next();
27+
final List<String> classNamePairs = Files.lines(Path.of(url.getPath()))
28+
.collect(Collectors.toList());
29+
30+
classNamePairs.forEach(clazzPair -> {
31+
try {
32+
33+
final String[] classNames = clazzPair.split(",");
34+
if (classNames.length != 2) {
35+
throw new IllegalStateException(String.format("%s is not custom-resource metadata defined in %s", url.toString()));
36+
}
37+
38+
controllerToCustomResourceMappings.put((Class<? extends ResourceController>) ClassUtils.getClass(classNames[0]), (Class<? extends CustomResource>) ClassUtils.getClass(classNames[1]));
39+
} catch (ClassNotFoundException e) {
40+
throw new RuntimeException(e);
41+
}
42+
});
43+
}
44+
45+
} catch (IOException e) {
46+
throw new RuntimeException(e);
47+
}
48+
//TODO: DEBUG log
49+
}
1550

1651
static String getFinalizer(ResourceController controller) {
1752
final String annotationFinalizerName = getAnnotation(controller).finalizerName();
@@ -26,13 +61,17 @@ static boolean getGenerationEventProcessing(ResourceController<?> controller) {
2661
}
2762

2863
static <R extends CustomResource> Class<R> getCustomResourceClass(ResourceController<R> controller) {
29-
return Arrays
30-
.stream(controller.getClass().getGenericInterfaces())
31-
.filter(i -> i instanceof ParameterizedType)
32-
.map(i -> (ParameterizedType) i)
33-
.findFirst()
34-
.map(i -> (Class<R>) i.getActualTypeArguments()[0])
35-
.get();
64+
final Class<? extends CustomResource> customResourceClass = controllerToCustomResourceMappings
65+
.get(controller.getClass());
66+
if (customResourceClass == null) {
67+
throw new IllegalArgumentException(
68+
String.format(
69+
"No custom resource has been found for controller %s",
70+
controller.getClass().getCanonicalName()
71+
)
72+
);
73+
}
74+
return (Class<R>) customResourceClass;
3675
}
3776

3877
static String getCrdName(ResourceController controller) {

0 commit comments

Comments
 (0)