44import io .fabric8 .kubernetes .client .CustomResourceDoneable ;
55import io .javaoperatorsdk .operator .api .Controller ;
66import 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
1217public 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