1
1
package io .javaoperatorsdk .operator ;
2
2
3
- import io .javaoperatorsdk .operator .api .ResourceController ;
4
- import io .javaoperatorsdk .operator .processing .EventDispatcher ;
5
- import io .javaoperatorsdk .operator .processing .EventScheduler ;
6
- import io .javaoperatorsdk .operator .processing .retry .GenericRetry ;
7
- import io .javaoperatorsdk .operator .processing .retry .Retry ;
8
3
import io .fabric8 .kubernetes .api .model .apiextensions .v1beta1 .CustomResourceDefinition ;
9
4
import io .fabric8 .kubernetes .client .CustomResource ;
10
5
import io .fabric8 .kubernetes .client .CustomResourceDoneable ;
14
9
import io .fabric8 .kubernetes .client .dsl .base .CustomResourceDefinitionContext ;
15
10
import io .fabric8 .kubernetes .client .dsl .internal .CustomResourceOperationsImpl ;
16
11
import io .fabric8 .kubernetes .internal .KubernetesDeserializer ;
12
+ import io .javaoperatorsdk .operator .api .ResourceController ;
13
+ import io .javaoperatorsdk .operator .processing .EventDispatcher ;
14
+ import io .javaoperatorsdk .operator .processing .DefaultEventHandler ;
15
+ import io .javaoperatorsdk .operator .processing .CustomResourceCache ;
16
+ import io .javaoperatorsdk .operator .processing .event .DefaultEventSourceManager ;
17
+ import io .javaoperatorsdk .operator .processing .event .internal .CustomResourceEventSource ;
17
18
import org .slf4j .Logger ;
18
19
import org .slf4j .LoggerFactory ;
19
20
20
21
import java .util .Arrays ;
21
22
import java .util .HashMap ;
22
23
import java .util .Map ;
23
24
25
+ import static io .javaoperatorsdk .operator .ControllerUtils .*;
26
+
27
+
24
28
@ SuppressWarnings ("rawtypes" )
25
29
public class Operator {
26
30
@@ -34,58 +38,61 @@ public Operator(KubernetesClient k8sClient) {
34
38
35
39
36
40
public <R extends CustomResource > void registerControllerForAllNamespaces (ResourceController <R > controller ) throws OperatorException {
37
- registerController (controller , true , GenericRetry .defaultLimitedExponentialRetry ());
38
- }
39
-
40
- public <R extends CustomResource > void registerControllerForAllNamespaces (ResourceController <R > controller , Retry retry ) throws OperatorException {
41
- registerController (controller , true , retry );
41
+ registerController (controller , true );
42
42
}
43
43
44
44
public <R extends CustomResource > void registerController (ResourceController <R > controller , String ... targetNamespaces ) throws OperatorException {
45
- registerController (controller , false , GenericRetry .defaultLimitedExponentialRetry (), targetNamespaces );
46
- }
47
-
48
- public <R extends CustomResource > void registerController (ResourceController <R > controller , Retry retry , String ... targetNamespaces ) throws OperatorException {
49
- registerController (controller , false , retry , targetNamespaces );
45
+ registerController (controller , false , targetNamespaces );
50
46
}
51
47
52
48
@ SuppressWarnings ("rawtypes" )
53
49
private <R extends CustomResource > void registerController (ResourceController <R > controller ,
54
- boolean watchAllNamespaces , Retry retry , String ... targetNamespaces ) throws OperatorException {
55
- Class <R > resClass = ControllerUtils . getCustomResourceClass (controller );
50
+ boolean watchAllNamespaces , String ... targetNamespaces ) throws OperatorException {
51
+ Class <R > resClass = getCustomResourceClass (controller );
56
52
CustomResourceDefinitionContext crd = getCustomResourceDefinitionForController (controller );
57
53
KubernetesDeserializer .registerCustomKind (crd .getVersion (), crd .getKind (), resClass );
58
54
String finalizer = ControllerUtils .getFinalizer (controller );
59
55
MixedOperation client = k8sClient .customResources (crd , resClass , CustomResourceList .class , ControllerUtils .getCustomResourceDoneableClass (controller ));
60
56
EventDispatcher eventDispatcher = new EventDispatcher (controller ,
61
- finalizer , new EventDispatcher .CustomResourceFacade (client ), ControllerUtils .getGenerationEventProcessing (controller ));
62
- EventScheduler eventScheduler = new EventScheduler (eventDispatcher , retry );
63
- registerWatches (controller , client , resClass , watchAllNamespaces , targetNamespaces , eventScheduler );
64
- }
57
+ finalizer , new EventDispatcher .CustomResourceFacade (client ));
65
58
66
59
67
- private <R extends CustomResource > void registerWatches (ResourceController <R > controller , MixedOperation client ,
68
- Class <R > resClass ,
69
- boolean watchAllNamespaces , String [] targetNamespaces , EventScheduler eventScheduler ) {
70
-
71
- CustomResourceOperationsImpl crClient = (CustomResourceOperationsImpl ) client ;
72
- if (watchAllNamespaces ) {
73
- crClient .inAnyNamespace ().watch (eventScheduler );
74
- } else if (targetNamespaces .length == 0 ) {
75
- client .watch (eventScheduler );
76
- } else {
77
- for (String targetNamespace : targetNamespaces ) {
78
- crClient .inNamespace (targetNamespace ).watch (eventScheduler );
79
- log .debug ("Registered controller for namespace: {}" , targetNamespace );
80
- }
81
- }
60
+ CustomResourceCache customResourceCache = new CustomResourceCache ();
61
+ DefaultEventHandler defaultEventHandler = new DefaultEventHandler (customResourceCache , eventDispatcher , controller .getClass ().getName ());
62
+ DefaultEventSourceManager eventSourceManager = new DefaultEventSourceManager (defaultEventHandler );
63
+ defaultEventHandler .setDefaultEventSourceManager (eventSourceManager );
64
+ eventDispatcher .setEventSourceManager (eventSourceManager );
65
+
82
66
customResourceClients .put (resClass , (CustomResourceOperationsImpl ) client );
67
+
68
+ controller .init (eventSourceManager );
69
+ CustomResourceEventSource customResourceEventSource
70
+ = createCustomResourceEventSource (client , customResourceCache , watchAllNamespaces , targetNamespaces ,
71
+ defaultEventHandler , ControllerUtils .getGenerationEventProcessing (controller ));
72
+ eventSourceManager .registerCustomResourceEventSource (customResourceEventSource );
73
+
74
+
83
75
log .info ("Registered Controller: '{}' for CRD: '{}' for namespaces: {}" , controller .getClass ().getSimpleName (),
84
76
resClass , targetNamespaces .length == 0 ? "[all/client namespace]" : Arrays .toString (targetNamespaces ));
85
77
}
86
78
79
+ private CustomResourceEventSource createCustomResourceEventSource (MixedOperation client ,
80
+ CustomResourceCache customResourceCache ,
81
+ boolean watchAllNamespaces ,
82
+ String [] targetNamespaces ,
83
+ DefaultEventHandler defaultEventHandler ,
84
+ boolean generationAware ) {
85
+ CustomResourceEventSource customResourceEventSource = watchAllNamespaces ?
86
+ CustomResourceEventSource .customResourceEventSourceForAllNamespaces (customResourceCache , client , generationAware ) :
87
+ CustomResourceEventSource .customResourceEventSourceForTargetNamespaces (customResourceCache , client , targetNamespaces , generationAware );
88
+
89
+ customResourceEventSource .setEventHandler (defaultEventHandler );
90
+
91
+ return customResourceEventSource ;
92
+ }
93
+
87
94
private CustomResourceDefinitionContext getCustomResourceDefinitionForController (ResourceController controller ) {
88
- String crdName = ControllerUtils . getCrdName (controller );
95
+ String crdName = getCrdName (controller );
89
96
CustomResourceDefinition customResourceDefinition = k8sClient .customResourceDefinitions ().withName (crdName ).get ();
90
97
if (customResourceDefinition == null ) {
91
98
throw new OperatorException ("Cannot find Custom Resource Definition with name: " + crdName );
@@ -103,11 +110,4 @@ public Map<Class<? extends CustomResource>, CustomResourceOperationsImpl> getCus
103
110
return customResourceClients .get (customResourceClass );
104
111
}
105
112
106
- private String getKind (CustomResourceDefinition crd ) {
107
- return crd .getSpec ().getNames ().getKind ();
108
- }
109
-
110
- private String getApiVersion (CustomResourceDefinition crd ) {
111
- return crd .getSpec ().getGroup () + "/" + crd .getSpec ().getVersion ();
112
- }
113
113
}
0 commit comments