diff --git a/bungeecord/src/main/java/net/chrotos/ingress/minecraft/bungeecord/IngressPlugin.java b/bungeecord/src/main/java/net/chrotos/ingress/minecraft/bungeecord/IngressPlugin.java index 4698059..8729a77 100644 --- a/bungeecord/src/main/java/net/chrotos/ingress/minecraft/bungeecord/IngressPlugin.java +++ b/bungeecord/src/main/java/net/chrotos/ingress/minecraft/bungeecord/IngressPlugin.java @@ -14,7 +14,7 @@ public class IngressPlugin extends Plugin { public void onEnable() { try { watcher = new Watcher((pod, deleted) -> { - String name = Objects.requireNonNull(pod.getMetadata()).getName(); + String name = Objects.requireNonNull(pod.getPod().getMetadata()).getName(); if (deleted) { getLogger().info(String.format("Server %s will be removed.", name)); @@ -27,13 +27,13 @@ public void onEnable() { getProxy().getServers().put(name, getProxy().constructServerInfo( name, InetSocketAddress.createUnresolved(Objects.requireNonNull( - Objects.requireNonNull(pod.getStatus()) + Objects.requireNonNull(pod.getPod().getStatus()) .getPodIP()), 25565), "", false)); - String tryServer = pod.getMetadata().getLabels() + String tryServer = pod.getPod().getMetadata().getLabels() .getOrDefault("net.chrotos.ingress.minecraft/lobby", "false"); if (tryServer.equalsIgnoreCase("true")) { getProxy().getConfigurationAdapter().getListeners().forEach(listenerInfo -> { diff --git a/shared/build.gradle b/shared/build.gradle index 1a0bc43..4752416 100644 --- a/shared/build.gradle +++ b/shared/build.gradle @@ -1,9 +1,3 @@ -/* - * This file was generated by the Gradle 'init' task. - * - * This is a general purpose Gradle build. - * Learn how to create Gradle builds at https://guides.gradle.org/creating-new-gradle-builds/ - */ plugins { id 'idea' id 'java' @@ -14,5 +8,5 @@ repositories { } dependencies { - implementation 'io.kubernetes:client-java-extended:10.0.0' + implementation 'io.kubernetes:client-java-extended:16.0.0' } \ No newline at end of file diff --git a/shared/src/main/java/net/chrotos/ingress/minecraft/Pod.java b/shared/src/main/java/net/chrotos/ingress/minecraft/Pod.java new file mode 100644 index 0000000..12409bb --- /dev/null +++ b/shared/src/main/java/net/chrotos/ingress/minecraft/Pod.java @@ -0,0 +1,26 @@ +package net.chrotos.ingress.minecraft; + +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.apis.CoreV1Api; +import io.kubernetes.client.openapi.models.V1Pod; + +public class Pod { + private final V1Pod pod; + private final CoreV1Api api; + + protected Pod(V1Pod pod, CoreV1Api api) { + this.pod = pod; + this.api = api; + } + + public V1Pod getPod() { + return this.pod; + } + + public void delete() { + try { + api.deleteNamespacedPod(pod.getMetadata().getName(), pod.getMetadata().getNamespace(), null, null, + null, null, null, null); + } catch (ApiException ignored) {} + } +} diff --git a/shared/src/main/java/net/chrotos/ingress/minecraft/PodRessourceHandler.java b/shared/src/main/java/net/chrotos/ingress/minecraft/PodRessourceHandler.java index 8df33bd..6e1f46a 100644 --- a/shared/src/main/java/net/chrotos/ingress/minecraft/PodRessourceHandler.java +++ b/shared/src/main/java/net/chrotos/ingress/minecraft/PodRessourceHandler.java @@ -1,8 +1,6 @@ package net.chrotos.ingress.minecraft; -import io.kubernetes.client.openapi.models.V1Pod; - @FunctionalInterface public interface PodRessourceHandler { - void onEventReceived(V1Pod pod, boolean deleted) throws Throwable; + void onEventReceived(Pod pod, boolean deleted) throws Throwable; } 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 bb6cb6c..970a685 100644 --- a/shared/src/main/java/net/chrotos/ingress/minecraft/Watcher.java +++ b/shared/src/main/java/net/chrotos/ingress/minecraft/Watcher.java @@ -12,12 +12,11 @@ import okhttp3.OkHttpClient; import java.io.IOException; -import java.util.Objects; import java.util.concurrent.TimeUnit; import java.util.stream.Stream; public class Watcher { - private SharedInformerFactory factory; + private final SharedInformerFactory factory; public Watcher(PodRessourceHandler handler) throws IOException { ApiClient apiClient = Config.defaultClient(); @@ -38,6 +37,7 @@ public Watcher(PodRessourceHandler handler) throws IOException { null, null, params.resourceVersion, + null, params.timeoutSeconds, params.watch, null), @@ -57,7 +57,7 @@ public void onAdd(V1Pod obj) { try { if (stream.findAny().isPresent()) { - handler.onEventReceived(obj, false); + handler.onEventReceived(new Pod(obj, v1Api), false); } } catch (Throwable e) { e.printStackTrace(); @@ -86,11 +86,13 @@ public void onUpdate(V1Pod oldObj, V1Pod newObj) { try { if (!oldPresent && newPresent) { - handler.onEventReceived(newObj, false); + handler.onEventReceived(new Pod(newObj, v1Api), false); } if (oldPresent && !newPresent) { - handler.onEventReceived(oldObj, true); + Pod pod = new Pod(oldObj, v1Api); + handler.onEventReceived(pod, true); + pod.delete(); } } catch (Throwable e) { e.printStackTrace(); diff --git a/velocity/src/main/java/net/chrotos/ingress/minecraft/velocity/IngressPlugin.java b/velocity/src/main/java/net/chrotos/ingress/minecraft/velocity/IngressPlugin.java index 07a5c39..0b40db7 100644 --- a/velocity/src/main/java/net/chrotos/ingress/minecraft/velocity/IngressPlugin.java +++ b/velocity/src/main/java/net/chrotos/ingress/minecraft/velocity/IngressPlugin.java @@ -34,7 +34,7 @@ public IngressPlugin(ProxyServer proxyServer, Logger logger) { public void onProxyInitialization(ProxyInitializeEvent event) { try { watcher = new Watcher((pod, deleted) -> { - String name = Objects.requireNonNull(pod.getMetadata()).getName(); + String name = Objects.requireNonNull(pod.getPod().getMetadata()).getName(); if (deleted) { logger.info("Server {} will be removed.", name); @@ -45,7 +45,7 @@ public void onProxyInitialization(ProxyInitializeEvent event) { RegisteredServer server = proxyServer.registerServer(new ServerInfo(name, InetSocketAddress.createUnresolved(Objects.requireNonNull( - Objects.requireNonNull(pod.getStatus()) + Objects.requireNonNull(pod.getPod().getStatus()) .getPodIP()), 25565))); @@ -54,7 +54,7 @@ public void onProxyInitialization(ProxyInitializeEvent event) { return; } - String tryServer = pod.getMetadata().getLabels() + String tryServer = pod.getPod().getMetadata().getLabels() .getOrDefault("net.chrotos.ingress.minecraft/lobby", "false"); if (tryServer.equalsIgnoreCase("true")) { if (!(proxyServer.getConfiguration().getAttemptConnectionOrder() instanceof ArrayList) ) {