Skip to content

Commit

Permalink
auto-delete pods on becoming unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
Katzen48 committed Aug 15, 2022
1 parent 4097d4a commit 8370d55
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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 -> {
Expand Down
8 changes: 1 addition & 7 deletions shared/build.gradle
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -14,5 +8,5 @@ repositories {
}

dependencies {
implementation 'io.kubernetes:client-java-extended:10.0.0'
implementation 'io.kubernetes:client-java-extended:16.0.0'
}
26 changes: 26 additions & 0 deletions shared/src/main/java/net/chrotos/ingress/minecraft/Pod.java
Original file line number Diff line number Diff line change
@@ -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) {}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 7 additions & 5 deletions shared/src/main/java/net/chrotos/ingress/minecraft/Watcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -38,6 +37,7 @@ public Watcher(PodRessourceHandler handler) throws IOException {
null,
null,
params.resourceVersion,
null,
params.timeoutSeconds,
params.watch,
null),
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)));

Expand All @@ -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) ) {
Expand Down

0 comments on commit 8370d55

Please sign in to comment.