4
4
import io .fabric8 .kubernetes .client .CustomResourceDoneable ;
5
5
import io .javaoperatorsdk .operator .api .Controller ;
6
6
import io .javaoperatorsdk .operator .api .ResourceController ;
7
+ import org .apache .commons .lang3 .ClassUtils ;
7
8
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 ;
10
15
11
16
12
17
public class ControllerUtils {
13
18
14
19
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
+ }
15
50
16
51
static String getFinalizer (ResourceController controller ) {
17
52
final String annotationFinalizerName = getAnnotation (controller ).finalizerName ();
@@ -26,13 +61,17 @@ static boolean getGenerationEventProcessing(ResourceController<?> controller) {
26
61
}
27
62
28
63
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 ;
36
75
}
37
76
38
77
static String getCrdName (ResourceController controller ) {
0 commit comments