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
23
+ @ SuppressWarnings ("rawtypes" )
25
24
public class Operator {
26
25
27
- private final KubernetesClient k8sClient ;
26
+ private final static Logger log = LoggerFactory . getLogger ( Operator . class ) ;
28
27
29
- private Map < ResourceController , EventScheduler > controllers = new HashMap <>() ;
28
+ private final KubernetesClient k8sClient ;
30
29
private Map <Class <? extends CustomResource >, CustomResourceOperationsImpl > customResourceClients = new HashMap <>();
31
- private EventScheduler eventScheduler ;
32
-
33
- private final static Logger log = LoggerFactory .getLogger (Operator .class );
34
30
35
31
public Operator (KubernetesClient k8sClient ) {
36
32
this .k8sClient = k8sClient ;
@@ -44,36 +40,30 @@ public <R extends CustomResource> void registerController(ResourceController<R>
44
40
registerController (controller , false , targetNamespaces );
45
41
}
46
42
43
+ @ SuppressWarnings ("rawtypes" )
47
44
private <R extends CustomResource > void registerController (ResourceController <R > controller ,
48
45
boolean watchAllNamespaces , String ... targetNamespaces ) throws OperatorException {
49
46
Class <R > resClass = getCustomResourceClass (controller );
50
- Optional <CustomResourceDefinition > crd = getCustomResourceDefinitionForController (controller );
51
- String kind = ControllerUtils .getKind (controller );
52
- KubernetesDeserializer .registerCustomKind (getApiVersion (controller ), kind , resClass );
53
-
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 );
47
+ CustomResourceDefinition crd = getCustomResourceDefinitionForController (controller );
48
+ KubernetesDeserializer .registerCustomKind (getApiVersion (crd ), getKind (crd ), resClass );
58
49
59
- EventDispatcher <R > eventDispatcher =
60
- new EventDispatcher <> (controller , ( CustomResourceOperationsImpl ) client , client , k8sClient ,
61
- ControllerUtils . getDefaultFinalizer ( controller ) );
50
+ Class <? extends CustomResourceList <R >> list = getCustomResourceListClass ( controller );
51
+ Class <? extends CustomResourceDoneable < R >> doneable = getCustomResourceDonebaleClass (controller );
52
+ MixedOperation client = k8sClient . customResources ( crd , resClass , list , doneable );
62
53
63
- eventScheduler = new EventScheduler (eventDispatcher );
64
-
65
- registerWatches (controller , client , resClass , watchAllNamespaces , targetNamespaces );
66
- } else {
67
- throw new OperatorException ("CRD '" + resClass .getSimpleName () + "' with version '"
68
- + getVersion (controller ) + "' not found" );
69
- }
54
+ EventDispatcher eventDispatcher = new EventDispatcher (controller , (CustomResourceOperationsImpl ) client ,
55
+ getDefaultFinalizer (controller ));
56
+ EventScheduler eventScheduler = new EventScheduler (eventDispatcher );
57
+ registerWatches (controller , client , resClass , watchAllNamespaces , targetNamespaces , eventScheduler );
70
58
}
71
59
72
60
private <R extends CustomResource > void registerWatches (ResourceController <R > controller , MixedOperation client ,
73
61
Class <R > resClass ,
74
- boolean watchAllNamespaces , String [] targetNamespaces ) {
62
+ boolean watchAllNamespaces , String [] targetNamespaces , EventScheduler eventScheduler ) {
63
+
75
64
CustomResourceOperationsImpl crClient = (CustomResourceOperationsImpl ) client ;
76
65
if (watchAllNamespaces ) {
66
+ // todo test this
77
67
crClient .inAnyNamespace ().watch (eventScheduler );
78
68
} else if (targetNamespaces .length == 0 ) {
79
69
client .watch (eventScheduler );
@@ -84,22 +74,17 @@ private <R extends CustomResource> void registerWatches(ResourceController<R> co
84
74
}
85
75
}
86
76
customResourceClients .put (resClass , (CustomResourceOperationsImpl ) client );
87
- controllers .put (controller , eventScheduler );
88
77
log .info ("Registered Controller: '{}' for CRD: '{}' for namespaces: {}" , controller .getClass ().getSimpleName (),
89
78
resClass , targetNamespaces .length == 0 ? "[all/client namespace]" : Arrays .toString (targetNamespaces ));
90
79
}
91
80
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 ();
81
+ private CustomResourceDefinition getCustomResourceDefinitionForController (ResourceController controller ) {
82
+ String crdName = getCrdName (controller );
83
+ CustomResourceDefinition customResourceDefinition = k8sClient .customResourceDefinitions ().withName (crdName ).get ();
84
+ if (customResourceDefinition == null ) {
85
+ throw new OperatorException ("Cannot find Custom Resource Definition with name: " + crdName );
102
86
}
87
+ return customResourceDefinition ;
103
88
}
104
89
105
90
public Map <Class <? extends CustomResource >, CustomResourceOperationsImpl > getCustomResourceClients () {
@@ -113,4 +98,12 @@ public void stop() {
113
98
public <T extends CustomResource > CustomResourceOperationsImpl getCustomResourceClients (Class <T > customResourceClass ) {
114
99
return customResourceClients .get (customResourceClass );
115
100
}
101
+
102
+ private String getKind (CustomResourceDefinition crd ) {
103
+ return crd .getSpec ().getNames ().getKind ();
104
+ }
105
+
106
+ private String getApiVersion (CustomResourceDefinition crd ) {
107
+ return crd .getSpec ().getGroup () + "/" + crd .getSpec ().getVersion ();
108
+ }
116
109
}
0 commit comments