Skip to content

Commit

Permalink
moved to endpoint slices
Browse files Browse the repository at this point in the history
  • Loading branch information
Katzen48 committed Feb 8, 2023
1 parent 7112329 commit e891fc8
Show file tree
Hide file tree
Showing 19 changed files with 659 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
package net.chrotos.ingress.minecraft.bungeecord;

import net.chrotos.ingress.minecraft.Watcher;
import net.chrotos.ingress.minecraft.gamemode.GameMode;
import net.md_5.bungee.api.plugin.Plugin;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Objects;

public class IngressPlugin extends Plugin {
private Watcher watcher;

@Override
public void onEnable() {
try {
watcher = new Watcher((pod, deleted) -> {
String name = Objects.requireNonNull(pod.getPod().getMetadata()).getName();
watcher = new Watcher((endpoint, deleted, watcher) -> {
String fullname = endpoint.getPodNamespace() + "/" + endpoint.getPodName();

if (deleted) {
getLogger().info(String.format("Server %s will be removed.", name));
getProxy().getServers().remove(name);
getLogger().info(String.format("Server %s will be removed.", fullname));
getProxy().getServers().remove(fullname);
getProxy().getConfigurationAdapter().getListeners().forEach(listenerInfo -> {
listenerInfo.getServerPriority().remove(name);
listenerInfo.getServerPriority().remove(fullname);
});
} else {
getLogger().info(String.format("New Server %s will be added.", name));
getLogger().info(String.format("New Server %s will be added.", fullname));

getProxy().getServers().put(name, getProxy().constructServerInfo(
name, InetSocketAddress.createUnresolved(Objects.requireNonNull(
Objects.requireNonNull(pod.getPod().getStatus())
.getPodIP()),
25565),
getProxy().getServers().put(fullname, getProxy().constructServerInfo(
fullname, new InetSocketAddress(endpoint.getAddresses()[0],endpoint.getPorts().get(0)),
"",
false));

String tryServer = pod.getPod().getMetadata().getLabels()
.getOrDefault("net.chrotos.ingress.minecraft/lobby", "false");
if (tryServer.equalsIgnoreCase("true")) {
boolean tryServer = watcher.getGameModes().stream()
.filter(gameMode -> gameMode.areNameAndNamespaceEqual(endpoint.getGameMode(), endpoint.getPodNamespace()))
.findFirst()
.map(GameMode::isLobby)
.orElse(false);
if (tryServer) {
getProxy().getConfigurationAdapter().getListeners().forEach(listenerInfo -> {
listenerInfo.getServerPriority().add(name);
listenerInfo.getServerPriority().add(fullname);
});
}
}
}, (gameMode, deleted) -> {
// TODO
});

watcher.start();
Expand Down
3 changes: 2 additions & 1 deletion shared/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ repositories {
}

dependencies {
implementation 'io.kubernetes:client-java-extended:16.0.0'
implementation 'io.kubernetes:client-java-extended:17.0.1'
implementation 'com.google.guava:guava:31.1-jre'
}
100 changes: 100 additions & 0 deletions shared/src/main/java/net/chrotos/ingress/minecraft/Endpoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package net.chrotos.ingress.minecraft;

import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Endpoint;
import io.kubernetes.client.openapi.models.V1Pod;

import java.net.InetAddress;
import java.util.List;
import java.util.Objects;

public class Endpoint {
private final InetAddress[] addresses;
private final boolean ready;
private final String podName;
private final String podNamespace;
private final String nodeName;
private final List<Integer> ports;
private String gameMode = null;

public Endpoint(V1Endpoint endpoint, EndpointSlice endpointSlice, CoreV1Api api) {
this.ports = endpointSlice.getPorts();

// Translate addresses
this.addresses = new InetAddress[endpoint.getAddresses().size()];
for (int i = 0; i < addresses.length; i++) {
try {
this.addresses[i] = InetAddress.getByName(endpoint.getAddresses().get(i));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

// Get ready condition
if (endpoint.getConditions() == null || endpoint.getConditions().getReady() == null) {
this.ready = false;
} else {
this.ready = endpoint.getConditions().getReady();
}

// Get Pod name and namespace
if (endpoint.getTargetRef() == null || endpoint.getTargetRef().getKind() == null ||
endpoint.getTargetRef().getKind() == null || endpoint.getTargetRef().getName() == null ||
endpoint.getTargetRef().getNamespace() == null ||
!endpoint.getTargetRef().getKind().equalsIgnoreCase("Pod")) {
this.podName = endpoint.getTargetRef().getName();
this.podNamespace = endpoint.getTargetRef().getNamespace();

try {
V1Pod pod = api.readNamespacedPod(this.podName, this.podNamespace, null);
if (pod.getMetadata() != null && pod.getMetadata().getLabels() != null) {
this.gameMode = pod.getMetadata().getLabels().get("net.chrotos.chrotoscloud.gameserver/gamemode");
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
this.podName = null;
this.podNamespace = null;
}

this.nodeName = endpoint.getNodeName();
}

public InetAddress[] getAddresses() {
return addresses;
}

public List<Integer> getPorts() {
return ports;
}

public boolean isReady() {
return ready && this.podName != null && this.podNamespace != null && addresses.length > 0;
}

public String getPodName() {
return podName;
}

public String getPodNamespace() {
return podNamespace;
}

public String getNodeName() {
return nodeName;
}

public String getGameMode() {
return gameMode;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Endpoint endpoint)) {
return false;
}

return Objects.equals(endpoint.podName, podName) && Objects.equals(endpoint.podNamespace, podNamespace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.chrotos.ingress.minecraft;

public interface EndpointResourceHandler {
void onEventReceived(Endpoint endpointSlice, boolean deleted, Watcher watcher) throws Throwable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package net.chrotos.ingress.minecraft;

import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.DiscoveryV1EndpointPort;
import io.kubernetes.client.openapi.models.V1EndpointSlice;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class EndpointSlice {
public static final String APP_PROTOCOL = "com.mojang/minecraft";
private final V1EndpointSlice endpointSlice;
private final List<Endpoint> readyEndpoints;
private final List<Integer> ports;

protected EndpointSlice(V1EndpointSlice endpointSlice, CoreV1Api api) {
this.endpointSlice = endpointSlice;

if (endpointSlice.getPorts() != null) {
this.ports = endpointSlice.getPorts().stream().filter(port -> APP_PROTOCOL.equals(port.getAppProtocol()) && port.getPort() != null)
.map(DiscoveryV1EndpointPort::getPort)
.parallel()
.collect(Collectors.toList());
} else {
this.ports = Collections.emptyList();
}

if (!ports.isEmpty()) {
this.readyEndpoints = endpointSlice.getEndpoints().stream().map(endpoint -> new Endpoint(endpoint, this, api))
.filter(Endpoint::isReady)
.parallel()
.collect(Collectors.toList());
} else {
this.readyEndpoints = Collections.emptyList();
}
}

public List<Endpoint> getReadyEndpoints() {
return readyEndpoints;
}

public String getName() {
return Objects.requireNonNull(endpointSlice.getMetadata()).getName();
}

public List<Integer> getPorts() {
return ports;
}

public V1EndpointSlice getEndpointSlice() {
return this.endpointSlice;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.chrotos.ingress.minecraft;

@FunctionalInterface
public interface EndpointSliceRessourceHandler {
void onEventReceived(EndpointSlice endpointSlice, boolean deleted) throws Throwable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.chrotos.ingress.minecraft;

import net.chrotos.ingress.minecraft.gamemode.GameMode;

@FunctionalInterface
public interface GameModeRessourceHandler {
void onEventReceived(GameMode gameMode, boolean deleted) throws Throwable;
}
Loading

0 comments on commit e891fc8

Please sign in to comment.