Skip to content

Commit 5ab87e7

Browse files
committed
now controller can be registered to watch all namespaces
1 parent b634c08 commit 5ab87e7

File tree

1 file changed

+15
-3
lines changed
  • operator-framework/src/main/java/com/github/containersolutions/operator

1 file changed

+15
-3
lines changed

operator-framework/src/main/java/com/github/containersolutions/operator/Operator.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ public Operator(KubernetesClient k8sClient) {
3333
this.k8sClient = k8sClient;
3434
}
3535

36+
public <R extends CustomResource> void registerControllerForAllNamespaces(ResourceController<R> controller) throws OperatorException {
37+
registerController(controller, true);
38+
}
39+
3640
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 {
3746
Class<R> resClass = getCustomResourceClass(controller);
3847
Optional<CustomResourceDefinition> crd = getCustomResourceDefinitionForController(controller);
3948
String kind = ControllerUtils.getKind(controller);
@@ -47,17 +56,20 @@ public <R extends CustomResource> void registerController(ResourceController<R>
4756
EventDispatcher<R> eventDispatcher =
4857
new EventDispatcher<>(controller, (CustomResourceOperationsImpl) client, client, k8sClient,
4958
ControllerUtils.getDefaultFinalizer(controller));
50-
registerWatches(controller, client, eventDispatcher, resClass, targetNamespaces);
59+
registerWatches(controller, client, eventDispatcher, resClass, watchAllNamespaces, targetNamespaces);
5160
} else {
5261
throw new OperatorException("CRD '" + resClass.getSimpleName() + "' with version '"
5362
+ getVersion(controller) + "' not found");
5463
}
5564
}
5665

5766
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) {
5969
CustomResourceOperationsImpl crClient = (CustomResourceOperationsImpl) client;
60-
if (targetNamespaces.length == 0) {
70+
if (watchAllNamespaces) {
71+
crClient.inAnyNamespace().watch(eventDispatcher);
72+
} else if (targetNamespaces.length == 0) {
6173
client.watch(eventDispatcher);
6274
} else {
6375
for (String targetNamespace : targetNamespaces) {

0 commit comments

Comments
 (0)