diff --git a/shared/src/main/java/net/chrotos/ingress/minecraft/Watcher.java b/shared/src/main/java/net/chrotos/ingress/minecraft/Watcher.java index 970a685..627830e 100644 --- a/shared/src/main/java/net/chrotos/ingress/minecraft/Watcher.java +++ b/shared/src/main/java/net/chrotos/ingress/minecraft/Watcher.java @@ -28,87 +28,118 @@ public Watcher(PodRessourceHandler handler) throws IOException { apiClient.setHttpClient(httpClient); factory = new SharedInformerFactory(); - SharedIndexInformer podInformer = factory.sharedIndexInformerFor( - (CallGeneratorParams params) -> v1Api.listPodForAllNamespacesCall( - null, - null, - null, - "net.chrotos.ingress.minecraft/discover=true", - null, - null, - params.resourceVersion, - null, - params.timeoutSeconds, - params.watch, - null), - V1Pod.class, - V1PodList.class); - - podInformer.addEventHandler( - new ResourceEventHandler<>() { - @Override - public void onAdd(V1Pod obj) { - if (obj.getStatus() == null || obj.getStatus().getContainerStatuses() == null) { - return; - } - - Stream stream = obj.getStatus().getContainerStatuses().stream() - .filter(V1ContainerStatus::getReady); - - try { - if (stream.findAny().isPresent()) { - handler.onEventReceived(new Pod(obj, v1Api), false); - } - } catch (Throwable e) { - e.printStackTrace(); - stream.close(); - } finally { - stream.close(); - } + ResourceEventHandler resourceEventHandler = new ResourceEventHandler<>() { + @Override + public void onAdd(V1Pod obj) { + if (obj.getStatus() == null || obj.getStatus().getContainerStatuses() == null) { + return; + } + + Stream stream = obj.getStatus().getContainerStatuses().stream() + .filter(V1ContainerStatus::getReady); + + try { + if (stream.findAny().isPresent()) { + handler.onEventReceived(new Pod(obj, v1Api), false); } + } catch (Throwable e) { + e.printStackTrace(); + stream.close(); + } finally { + stream.close(); + } + } + + @Override + public void onUpdate(V1Pod oldObj, V1Pod newObj) { + if (oldObj.getStatus() == null || newObj.getStatus() == null + || oldObj.getStatus().getContainerStatuses() == null + || newObj.getStatus().getContainerStatuses() == null) { + return; + } + + Stream oldStream = oldObj.getStatus().getContainerStatuses().stream() + .filter(V1ContainerStatus::getReady); + + Stream newStream = newObj.getStatus().getContainerStatuses().stream() + .filter(V1ContainerStatus::getReady); - @Override - public void onUpdate(V1Pod oldObj, V1Pod newObj) { - if (oldObj.getStatus() == null || newObj.getStatus() == null - || oldObj.getStatus().getContainerStatuses() == null - || newObj.getStatus().getContainerStatuses() == null) { - return; - } - - Stream oldStream = oldObj.getStatus().getContainerStatuses().stream() - .filter(V1ContainerStatus::getReady); - - Stream newStream = newObj.getStatus().getContainerStatuses().stream() - .filter(V1ContainerStatus::getReady); - - boolean oldPresent = oldStream.findAny().isPresent(); - boolean newPresent = newStream.findAny().isPresent(); - - try { - if (!oldPresent && newPresent) { - handler.onEventReceived(new Pod(newObj, v1Api), false); - } - - if (oldPresent && !newPresent) { - Pod pod = new Pod(oldObj, v1Api); - handler.onEventReceived(pod, true); - pod.delete(); - } - } catch (Throwable e) { - e.printStackTrace(); - oldStream.close(); - newStream.close(); - } finally { - oldStream.close(); - newStream.close(); - } + boolean oldPresent = oldStream.findAny().isPresent(); + boolean newPresent = newStream.findAny().isPresent(); + try { + if (!oldPresent && newPresent) { + handler.onEventReceived(new Pod(newObj, v1Api), false); } - @Override - public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) { } + if (oldPresent && !newPresent) { + Pod pod = new Pod(oldObj, v1Api); + handler.onEventReceived(pod, true); + pod.delete(); + } + } catch (Throwable e) { + e.printStackTrace(); + oldStream.close(); + newStream.close(); + } finally { + oldStream.close(); + newStream.close(); } - ); + + } + + @Override + public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) { } + }; + + String namespaces = System.getenv("INGRESS_NAMESPACES"); + if (namespaces == null || namespaces.isEmpty()) { + registerInformer(null, v1Api, resourceEventHandler); + } else { + for (String namespace : namespaces.split(",")) { + registerInformer(namespace, v1Api, resourceEventHandler); + } + } + } + + private void registerInformer(String namespace, CoreV1Api v1Api, ResourceEventHandler handler) { + SharedIndexInformer podInformer; + if (namespace == null) { + podInformer = factory.sharedIndexInformerFor( + (CallGeneratorParams params) -> v1Api.listPodForAllNamespacesCall( + null, + null, + null, + "net.chrotos.ingress.minecraft/discover=true", + null, + null, + params.resourceVersion, + null, + params.timeoutSeconds, + params.watch, + null), + V1Pod.class, + V1PodList.class); + } else { + podInformer = factory.sharedIndexInformerFor( + (CallGeneratorParams params) -> v1Api.listNamespacedPodCall( + namespace, + null, + null, + null, + null, + "net.chrotos.ingress.minecraft/discover=true", + null, + params.resourceVersion, + null, + params.timeoutSeconds, + params.watch, + null), + V1Pod.class, + V1PodList.class); + } + + podInformer.addEventHandler(handler); } public void start() {