4
4
import com .github .containersolutions .operator .processing .EventDispatcher ;
5
5
import com .github .containersolutions .operator .processing .EventScheduler ;
6
6
import io .fabric8 .kubernetes .api .model .apiextensions .CustomResourceDefinition ;
7
- import io .fabric8 .kubernetes .api .model .apiextensions .CustomResourceDefinitionList ;
8
7
import io .fabric8 .kubernetes .client .CustomResource ;
9
8
import io .fabric8 .kubernetes .client .CustomResourceDoneable ;
10
9
import io .fabric8 .kubernetes .client .CustomResourceList ;
18
17
import java .util .Arrays ;
19
18
import java .util .HashMap ;
20
19
import java .util .Map ;
21
- import java .util .Optional ;
22
20
23
21
import static com .github .containersolutions .operator .ControllerUtils .*;
24
22
@@ -28,7 +26,6 @@ public class Operator {
28
26
29
27
private Map <ResourceController , EventScheduler > controllers = new HashMap <>();
30
28
private Map <Class <? extends CustomResource >, CustomResourceOperationsImpl > customResourceClients = new HashMap <>();
31
- private EventScheduler eventScheduler ;
32
29
33
30
private final static Logger log = LoggerFactory .getLogger (Operator .class );
34
31
@@ -44,36 +41,35 @@ public <R extends CustomResource> void registerController(ResourceController<R>
44
41
registerController (controller , false , targetNamespaces );
45
42
}
46
43
44
+ @ SuppressWarnings ("rawtypes" )
47
45
private <R extends CustomResource > void registerController (ResourceController <R > controller ,
48
46
boolean watchAllNamespaces , String ... targetNamespaces ) throws OperatorException {
49
47
Class <R > resClass = getCustomResourceClass (controller );
50
- Optional <CustomResourceDefinition > crd = getCustomResourceDefinitionForController (controller );
51
- String kind = ControllerUtils .getKind (controller );
52
- KubernetesDeserializer .registerCustomKind (getApiVersion (controller ), kind , resClass );
48
+ CustomResourceDefinition crd = getCustomResourceDefinitionForController (controller );
53
49
54
- if (crd .isPresent ()) {
55
- Class <? extends CustomResourceList <R >> list = getCustomResourceListClass (controller );
56
- Class <? extends CustomResourceDoneable <R >> doneable = getCustomResourceDonebaleClass (controller );
57
- MixedOperation client = k8sClient .customResources (crd .get (), resClass , list , doneable );
50
+ String kind = getKind (crd );
58
51
59
- EventDispatcher <R > eventDispatcher =
60
- new EventDispatcher <>(controller , (CustomResourceOperationsImpl ) client ,
61
- ControllerUtils .getDefaultFinalizer (controller ));
52
+ KubernetesDeserializer .registerCustomKind (getApiVersion (crd ), kind , resClass );
62
53
63
- eventScheduler = new EventScheduler (eventDispatcher );
54
+ Class <? extends CustomResourceList <R >> list = getCustomResourceListClass (controller );
55
+ Class <? extends CustomResourceDoneable <R >> doneable = getCustomResourceDonebaleClass (controller );
56
+ MixedOperation client = k8sClient .customResources (crd , resClass , list , doneable );
64
57
65
- registerWatches (controller , client , resClass , watchAllNamespaces , targetNamespaces );
66
- } else {
67
- throw new OperatorException ("CRD '" + resClass .getSimpleName () + "' with version '"
68
- + getVersion (controller ) + "' not found" );
69
- }
58
+ EventDispatcher eventDispatcher = new EventDispatcher (controller , (CustomResourceOperationsImpl ) client ,
59
+ getDefaultFinalizer (controller ));
60
+
61
+ EventScheduler eventScheduler = new EventScheduler (eventDispatcher );
62
+
63
+ registerWatches (controller , client , resClass , watchAllNamespaces , targetNamespaces , eventScheduler );
70
64
}
71
65
72
66
private <R extends CustomResource > void registerWatches (ResourceController <R > controller , MixedOperation client ,
73
67
Class <R > resClass ,
74
- boolean watchAllNamespaces , String [] targetNamespaces ) {
68
+ boolean watchAllNamespaces , String [] targetNamespaces , EventScheduler eventScheduler ) {
69
+
75
70
CustomResourceOperationsImpl crClient = (CustomResourceOperationsImpl ) client ;
76
71
if (watchAllNamespaces ) {
72
+ // todo check if this works
77
73
crClient .inAnyNamespace ().watch (eventScheduler );
78
74
} else if (targetNamespaces .length == 0 ) {
79
75
client .watch (eventScheduler );
@@ -89,17 +85,13 @@ private <R extends CustomResource> void registerWatches(ResourceController<R> co
89
85
resClass , targetNamespaces .length == 0 ? "[all/client namespace]" : Arrays .toString (targetNamespaces ));
90
86
}
91
87
92
- private Optional <CustomResourceDefinition > getCustomResourceDefinitionForController (ResourceController controller ) {
93
- Optional <String > crdName = getCrdName (controller );
94
- if (crdName .isPresent ()) {
95
- return Optional .ofNullable (k8sClient .customResourceDefinitions ().withName (crdName .get ()).get ());
96
- } else {
97
- CustomResourceDefinitionList crdList = k8sClient .customResourceDefinitions ().list ();
98
- return crdList .getItems ().stream ()
99
- .filter (c -> getKind (controller ).equals (c .getSpec ().getNames ().getKind ()) &&
100
- getVersion (controller ).equals (c .getSpec ().getVersion ()))
101
- .findFirst ();
88
+ private CustomResourceDefinition getCustomResourceDefinitionForController (ResourceController controller ) {
89
+ String crdName = getCrdName (controller );
90
+ CustomResourceDefinition customResourceDefinition = k8sClient .customResourceDefinitions ().withName (crdName ).get ();
91
+ if (customResourceDefinition == null ) {
92
+ throw new OperatorException ("Cannot find Custom Resource Definition with name: " + crdName );
102
93
}
94
+ return customResourceDefinition ;
103
95
}
104
96
105
97
public Map <Class <? extends CustomResource >, CustomResourceOperationsImpl > getCustomResourceClients () {
@@ -113,4 +105,13 @@ public void stop() {
113
105
public <T extends CustomResource > CustomResourceOperationsImpl getCustomResourceClients (Class <T > customResourceClass ) {
114
106
return customResourceClients .get (customResourceClass );
115
107
}
108
+
109
+ private String getKind (CustomResourceDefinition crd ) {
110
+ return crd .getSpec ().getNames ().getKind ();
111
+ }
112
+
113
+ private String getApiVersion (CustomResourceDefinition crd ) {
114
+ return crd .getSpec ().getGroup () + "/" + crd .getSpec ().getVersion ();
115
+ }
116
+
116
117
}
0 commit comments