1
1
package io .javaoperatorsdk .operator ;
2
2
3
- import io .fabric8 .kubernetes .api .model .apiextensions .v1beta1 .CustomResourceDefinition ;
4
3
import io .fabric8 .kubernetes .client .CustomResource ;
5
- import io .fabric8 .kubernetes .client .CustomResourceDoneable ;
6
- import io .fabric8 .kubernetes .client .CustomResourceList ;
7
4
import io .fabric8 .kubernetes .client .KubernetesClient ;
8
5
import io .fabric8 .kubernetes .client .dsl .MixedOperation ;
9
- import io .fabric8 .kubernetes .client .dsl .base .CustomResourceDefinitionContext ;
10
- import io .fabric8 .kubernetes .client .dsl .internal .CustomResourceOperationsImpl ;
11
- import io .fabric8 .kubernetes .internal .KubernetesDeserializer ;
12
6
import io .javaoperatorsdk .operator .api .ResourceController ;
13
7
import io .javaoperatorsdk .operator .api .config .ConfigurationService ;
14
8
import io .javaoperatorsdk .operator .processing .CustomResourceCache ;
19
13
import io .javaoperatorsdk .operator .processing .retry .GenericRetry ;
20
14
import io .javaoperatorsdk .operator .processing .retry .Retry ;
21
15
import java .util .Arrays ;
22
- import java .util .HashMap ;
23
- import java .util .Map ;
24
16
import org .slf4j .Logger ;
25
17
import org .slf4j .LoggerFactory ;
26
18
@@ -30,8 +22,6 @@ public class Operator {
30
22
private static final Logger log = LoggerFactory .getLogger (Operator .class );
31
23
private final KubernetesClient k8sClient ;
32
24
private final ConfigurationService configurationService ;
33
- private Map <Class <? extends CustomResource >, CustomResourceOperationsImpl > customResourceClients =
34
- new HashMap <>();
35
25
36
26
public Operator (KubernetesClient k8sClient , ConfigurationService configurationService ) {
37
27
this .k8sClient = k8sClient ;
@@ -41,9 +31,18 @@ public Operator(KubernetesClient k8sClient, ConfigurationService configurationSe
41
31
public <R extends CustomResource > void register (ResourceController <R > controller )
42
32
throws OperatorException {
43
33
final var configuration = configurationService .getConfigurationFor (controller );
44
- final var retry = GenericRetry .fromConfiguration (configuration .getRetryConfiguration ());
45
- final var targetNamespaces = configuration .getNamespaces ().toArray (new String [] {});
46
- registerController (controller , configuration .watchAllNamespaces (), retry , targetNamespaces );
34
+ if (configuration == null ) {
35
+ log .warn (
36
+ "Skipping registration of {} controller named {} because its configuration cannot be found.\n "
37
+ + "Known controllers are: {}" ,
38
+ controller .getClass ().getCanonicalName (),
39
+ ControllerUtils .getNameFor (controller ),
40
+ configurationService .getKnownControllerNames ());
41
+ } else {
42
+ final var retry = GenericRetry .fromConfiguration (configuration .getRetryConfiguration ());
43
+ final var targetNamespaces = configuration .getNamespaces ().toArray (new String [] {});
44
+ registerController (controller , configuration .watchAllNamespaces (), retry , targetNamespaces );
45
+ }
47
46
}
48
47
49
48
public <R extends CustomResource > void registerControllerForAllNamespaces (
@@ -76,12 +75,8 @@ private <R extends CustomResource> void registerController(
76
75
throws OperatorException {
77
76
final var configuration = configurationService .getConfigurationFor (controller );
78
77
Class <R > resClass = configuration .getCustomResourceClass ();
79
- CustomResourceDefinitionContext crd = getCustomResourceDefinitionForController (controller );
80
- KubernetesDeserializer .registerCustomKind (crd .getVersion (), crd .getKind (), resClass );
81
78
String finalizer = configuration .getFinalizer ();
82
- MixedOperation client =
83
- k8sClient .customResources (
84
- crd , resClass , CustomResourceList .class , configuration .getDoneableClass ());
79
+ MixedOperation client = k8sClient .customResources (resClass );
85
80
EventDispatcher eventDispatcher =
86
81
new EventDispatcher (
87
82
controller , finalizer , new EventDispatcher .CustomResourceFacade (client ));
@@ -95,8 +90,6 @@ private <R extends CustomResource> void registerController(
95
90
defaultEventHandler .setEventSourceManager (eventSourceManager );
96
91
eventDispatcher .setEventSourceManager (eventSourceManager );
97
92
98
- customResourceClients .put (resClass , (CustomResourceOperationsImpl ) client );
99
-
100
93
controller .init (eventSourceManager );
101
94
CustomResourceEventSource customResourceEventSource =
102
95
createCustomResourceEventSource (
@@ -137,30 +130,4 @@ private CustomResourceEventSource createCustomResourceEventSource(
137
130
138
131
return customResourceEventSource ;
139
132
}
140
-
141
- private CustomResourceDefinitionContext getCustomResourceDefinitionForController (
142
- ResourceController controller ) {
143
- final var crdName = configurationService .getConfigurationFor (controller ).getCRDName ();
144
- CustomResourceDefinition customResourceDefinition =
145
- k8sClient .customResourceDefinitions ().withName (crdName ).get ();
146
- if (customResourceDefinition == null ) {
147
- throw new OperatorException ("Cannot find Custom Resource Definition with name: " + crdName );
148
- }
149
- CustomResourceDefinitionContext context =
150
- CustomResourceDefinitionContext .fromCrd (customResourceDefinition );
151
- return context ;
152
- }
153
-
154
- public Map <Class <? extends CustomResource >, CustomResourceOperationsImpl >
155
- getCustomResourceClients () {
156
- return customResourceClients ;
157
- }
158
-
159
- public <
160
- T extends CustomResource ,
161
- L extends CustomResourceList <T >,
162
- D extends CustomResourceDoneable <T >>
163
- CustomResourceOperationsImpl <T , L , D > getCustomResourceClients (Class <T > customResourceClass ) {
164
- return customResourceClients .get (customResourceClass );
165
- }
166
133
}
0 commit comments