@@ -33,7 +33,16 @@ public Operator(KubernetesClient k8sClient) {
33
33
this .k8sClient = k8sClient ;
34
34
}
35
35
36
+ public <R extends CustomResource > void registerControllerForAllNamespaces (ResourceController <R > controller ) throws OperatorException {
37
+ registerController (controller , true );
38
+ }
39
+
36
40
public <R extends CustomResource > void registerController (ResourceController <R > controller , String ... targetNamespaces ) throws OperatorException {
41
+ registerController (controller , false , targetNamespaces );
42
+ }
43
+
44
+ private <R extends CustomResource > void registerController (ResourceController <R > controller ,
45
+ boolean watchAllNamespaces , String ... targetNamespaces ) throws OperatorException {
37
46
Class <R > resClass = getCustomResourceClass (controller );
38
47
Optional <CustomResourceDefinition > crd = getCustomResourceDefinitionForController (controller );
39
48
String kind = ControllerUtils .getKind (controller );
@@ -47,17 +56,20 @@ public <R extends CustomResource> void registerController(ResourceController<R>
47
56
EventDispatcher <R > eventDispatcher =
48
57
new EventDispatcher <>(controller , (CustomResourceOperationsImpl ) client , client , k8sClient ,
49
58
ControllerUtils .getDefaultFinalizer (controller ));
50
- registerWatches (controller , client , eventDispatcher , resClass , targetNamespaces );
59
+ registerWatches (controller , client , eventDispatcher , resClass , watchAllNamespaces , targetNamespaces );
51
60
} else {
52
61
throw new OperatorException ("CRD '" + resClass .getSimpleName () + "' with version '"
53
62
+ getVersion (controller ) + "' not found" );
54
63
}
55
64
}
56
65
57
66
private <R extends CustomResource > void registerWatches (ResourceController <R > controller , MixedOperation client ,
58
- EventDispatcher <R > eventDispatcher , Class <R > resClass , String [] targetNamespaces ) {
67
+ EventDispatcher <R > eventDispatcher , Class <R > resClass ,
68
+ boolean watchAllNamespaces , String [] targetNamespaces ) {
59
69
CustomResourceOperationsImpl crClient = (CustomResourceOperationsImpl ) client ;
60
- if (targetNamespaces .length == 0 ) {
70
+ if (watchAllNamespaces ) {
71
+ crClient .inAnyNamespace ().watch (eventDispatcher );
72
+ } else if (targetNamespaces .length == 0 ) {
61
73
client .watch (eventDispatcher );
62
74
} else {
63
75
for (String targetNamespace : targetNamespaces ) {
0 commit comments