12
12
import io .javaoperatorsdk .operator .processing .event .DefaultEventSourceManager ;
13
13
import io .javaoperatorsdk .operator .processing .event .internal .CustomResourceEventSource ;
14
14
import io .javaoperatorsdk .operator .processing .retry .GenericRetry ;
15
- import io .javaoperatorsdk .operator .processing .retry .Retry ;
16
15
import java .util .Arrays ;
17
16
import org .slf4j .Logger ;
18
17
import org .slf4j .LoggerFactory ;
@@ -29,11 +28,44 @@ public Operator(KubernetesClient k8sClient, ConfigurationService configurationSe
29
28
this .configurationService = configurationService ;
30
29
}
31
30
31
+ /**
32
+ * Finishes the operator startup process. This is mostly used in injection-aware applications
33
+ * where there is no obvious entrypoint to the application which can trigger the injection process
34
+ * and start the cluster monitoring processes.
35
+ */
36
+ public void start () {
37
+ final var version = configurationService .getVersion ();
38
+ log .info (
39
+ "Operator {} (commit: {}) built on {} starting..." ,
40
+ version .getSdkVersion (),
41
+ version .getCommit (),
42
+ version .getBuiltTime ());
43
+ }
44
+
45
+ /**
46
+ * Registers the specified controller with this operator.
47
+ *
48
+ * @param controller the controller to register
49
+ * @param <R> the {@code CustomResource} type associated with the controller
50
+ * @throws OperatorException if a problem occurred during the registration process
51
+ */
32
52
public <R extends CustomResource > void register (ResourceController <R > controller )
33
53
throws OperatorException {
34
54
register (controller , null );
35
55
}
36
56
57
+ /**
58
+ * Registers the specified controller with this operator, overriding its default configuration by
59
+ * the specified one (usually created via {@link
60
+ * io.javaoperatorsdk.operator.api.config.ControllerConfigurationOverrider#override(ControllerConfiguration)},
61
+ * passing it the controller's original configuration.
62
+ *
63
+ * @param controller the controller to register
64
+ * @param configuration the configuration with which we want to register the controller, if {@code
65
+ * null}, the controller's orginal configuration is used
66
+ * @param <R> the {@code CustomResource} type associated with the controller
67
+ * @throws OperatorException if a problem occurred during the registration process
68
+ */
37
69
public <R extends CustomResource > void register (
38
70
ResourceController <R > controller , ControllerConfiguration <R > configuration )
39
71
throws OperatorException {
@@ -49,55 +81,57 @@ public <R extends CustomResource> void register(
49
81
if (configuration == null ) {
50
82
configuration = existing ;
51
83
}
84
+
52
85
final var retry = GenericRetry .fromConfiguration (configuration .getRetryConfiguration ());
53
86
final var targetNamespaces = configuration .getNamespaces ().toArray (new String [] {});
54
- registerController (controller , configuration .watchAllNamespaces (), retry , targetNamespaces );
55
- }
56
- }
87
+ Class <R > resClass = configuration .getCustomResourceClass ();
88
+ String finalizer = configuration .getFinalizer ();
89
+ final var client = k8sClient .customResources (resClass );
90
+ EventDispatcher dispatcher =
91
+ new EventDispatcher (
92
+ controller , finalizer , new EventDispatcher .CustomResourceFacade (client ));
57
93
58
- @ SuppressWarnings ("rawtypes" )
59
- private <R extends CustomResource > void registerController (
60
- ResourceController <R > controller ,
61
- boolean watchAllNamespaces ,
62
- Retry retry ,
63
- String ... targetNamespaces )
64
- throws OperatorException {
65
- final var configuration = configurationService .getConfigurationFor (controller );
66
- Class <R > resClass = configuration .getCustomResourceClass ();
67
- String finalizer = configuration .getFinalizer ();
68
- MixedOperation client = k8sClient .customResources (resClass );
69
- EventDispatcher eventDispatcher =
70
- new EventDispatcher (
71
- controller , finalizer , new EventDispatcher .CustomResourceFacade (client ));
94
+ // check that the custom resource is known by the cluster
95
+ final var crdName = configuration .getCRDName ();
96
+ final var crd =
97
+ k8sClient .apiextensions ().v1 ().customResourceDefinitions ().withName (crdName ).get ();
98
+ final var controllerName = configuration .getName ();
99
+ if (crd == null ) {
100
+ throw new OperatorException (
101
+ "'"
102
+ + crdName
103
+ + "' CRD was not found on the cluster, controller "
104
+ + controllerName
105
+ + " cannot be registered" );
106
+ }
72
107
73
- CustomResourceCache customResourceCache = new CustomResourceCache ();
74
- DefaultEventHandler defaultEventHandler =
75
- new DefaultEventHandler (
76
- customResourceCache , eventDispatcher , controller .getClass ().getName (), retry );
77
- DefaultEventSourceManager eventSourceManager =
78
- new DefaultEventSourceManager (defaultEventHandler , retry != null );
79
- defaultEventHandler .setEventSourceManager (eventSourceManager );
80
- eventDispatcher .setEventSourceManager (eventSourceManager );
108
+ CustomResourceCache customResourceCache = new CustomResourceCache ();
109
+ DefaultEventHandler defaultEventHandler =
110
+ new DefaultEventHandler (customResourceCache , dispatcher , controllerName , retry );
111
+ DefaultEventSourceManager eventSourceManager =
112
+ new DefaultEventSourceManager (defaultEventHandler , retry != null );
113
+ defaultEventHandler .setEventSourceManager (eventSourceManager );
114
+ dispatcher .setEventSourceManager (eventSourceManager );
81
115
82
- controller .init (eventSourceManager );
83
- CustomResourceEventSource customResourceEventSource =
84
- createCustomResourceEventSource (
85
- client ,
86
- customResourceCache ,
87
- watchAllNamespaces ,
88
- targetNamespaces ,
89
- defaultEventHandler ,
90
- configuration .isGenerationAware (),
91
- finalizer );
92
- eventSourceManager .registerCustomResourceEventSource (customResourceEventSource );
116
+ controller .init (eventSourceManager );
117
+ final boolean watchAllNamespaces = configuration .watchAllNamespaces ();
118
+ CustomResourceEventSource customResourceEventSource =
119
+ createCustomResourceEventSource (
120
+ client ,
121
+ customResourceCache ,
122
+ watchAllNamespaces ,
123
+ targetNamespaces ,
124
+ defaultEventHandler ,
125
+ configuration .isGenerationAware (),
126
+ finalizer );
127
+ eventSourceManager .registerCustomResourceEventSource (customResourceEventSource );
93
128
94
- log .info (
95
- "Registered Controller: '{}' for CRD: '{}' for namespaces: {}" ,
96
- controller .getClass ().getSimpleName (),
97
- resClass ,
98
- targetNamespaces .length == 0
99
- ? "[all/client namespace]"
100
- : Arrays .toString (targetNamespaces ));
129
+ log .info (
130
+ "Registered Controller: '{}' for CRD: '{}' for namespaces: {}" ,
131
+ controller .getClass ().getSimpleName (),
132
+ resClass ,
133
+ watchAllNamespaces ? "[all namespaces]" : Arrays .toString (targetNamespaces ));
134
+ }
101
135
}
102
136
103
137
private CustomResourceEventSource createCustomResourceEventSource (
0 commit comments