Skip to content

Commit

Permalink
added possibility to set namespaces to discover
Browse files Browse the repository at this point in the history
  • Loading branch information
Katzen48 committed Sep 23, 2022
1 parent 8370d55 commit 8af137c
Showing 1 changed file with 106 additions and 75 deletions.
181 changes: 106 additions & 75 deletions shared/src/main/java/net/chrotos/ingress/minecraft/Watcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,87 +28,118 @@ public Watcher(PodRessourceHandler handler) throws IOException {
apiClient.setHttpClient(httpClient);

factory = new SharedInformerFactory();
SharedIndexInformer<V1Pod> 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<V1ContainerStatus> 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<V1Pod> resourceEventHandler = new ResourceEventHandler<>() {
@Override
public void onAdd(V1Pod obj) {
if (obj.getStatus() == null || obj.getStatus().getContainerStatuses() == null) {
return;
}

Stream<V1ContainerStatus> 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<V1ContainerStatus> oldStream = oldObj.getStatus().getContainerStatuses().stream()
.filter(V1ContainerStatus::getReady);

Stream<V1ContainerStatus> 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<V1ContainerStatus> oldStream = oldObj.getStatus().getContainerStatuses().stream()
.filter(V1ContainerStatus::getReady);

Stream<V1ContainerStatus> 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<V1Pod> handler) {
SharedIndexInformer<V1Pod> 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() {
Expand Down

0 comments on commit 8af137c

Please sign in to comment.