Skip to content

Commit

Permalink
fixed wrong ready state, when multiple containers in pod
Browse files Browse the repository at this point in the history
  • Loading branch information
Katzen48 committed Oct 15, 2022
1 parent 8af137c commit 7112329
Showing 1 changed file with 55 additions and 23 deletions.
78 changes: 55 additions & 23 deletions shared/src/main/java/net/chrotos/ingress/minecraft/Watcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
import io.kubernetes.client.util.CallGeneratorParams;
import io.kubernetes.client.util.Config;
import okhttp3.OkHttpClient;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

public class Watcher {
private final SharedInformerFactory factory;
Expand All @@ -27,26 +28,22 @@ public Watcher(PodRessourceHandler handler) throws IOException {
apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build();
apiClient.setHttpClient(httpClient);

factory = new SharedInformerFactory();
factory = new SharedInformerFactory(apiClient);
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);
// TODO change to service lookup
Optional<V1ContainerStatus> status = getServerStatus(obj);

try {
if (stream.findAny().isPresent()) {
if (status.isPresent() && status.get().getReady()) {
handler.onEventReceived(new Pod(obj, v1Api), false);
}
} catch (Throwable e) {
e.printStackTrace();
stream.close();
} finally {
stream.close();
}
}

Expand All @@ -58,34 +55,34 @@ public void onUpdate(V1Pod oldObj, V1Pod newObj) {
return;
}

Stream<V1ContainerStatus> oldStream = oldObj.getStatus().getContainerStatuses().stream()
.filter(V1ContainerStatus::getReady);
String containerName = getServerContainerName(newObj);
if (containerName == null) {
containerName = getServerContainerName(oldObj);
}

if (containerName == null) {
return;
}

Stream<V1ContainerStatus> newStream = newObj.getStatus().getContainerStatuses().stream()
.filter(V1ContainerStatus::getReady);
Optional<V1ContainerStatus> oldStatus = getServerStatus(oldObj, containerName);
Optional<V1ContainerStatus> newStatus = getServerStatus(newObj, containerName);

boolean oldPresent = oldStream.findAny().isPresent();
boolean newPresent = newStream.findAny().isPresent();
boolean oldReady = oldStatus.isPresent() && oldStatus.get().getReady();
boolean newReady = newStatus.isPresent() && newStatus.get().getReady();

try {
if (!oldPresent && newPresent) {
if (!oldReady && newReady) {
handler.onEventReceived(new Pod(newObj, v1Api), false);
}

if (oldPresent && !newPresent) {
if (oldReady && !newReady) {
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
Expand Down Expand Up @@ -142,6 +139,41 @@ private void registerInformer(String namespace, CoreV1Api v1Api, ResourceEventHa
podInformer.addEventHandler(handler);
}

private Optional<V1ContainerStatus> getServerStatus(@NonNull V1Pod obj) {
String containerName = getServerContainerName(obj);

return getServerStatus(obj, containerName);
}

private Optional<V1ContainerStatus> getServerStatus(@NonNull V1Pod obj, String containerName) {
if (containerName == null || obj.getStatus() == null || obj.getStatus().getContainerStatuses() == null) {
return Optional.empty();
}

return obj.getStatus().getContainerStatuses().stream()
.filter(stat -> stat.getName() != null && stat.getName().equals(containerName)).findFirst();
}

private String getServerContainerName(@NonNull V1Pod obj) { // TODO change to service lookup
if (obj.getSpec() == null || obj.getSpec().getContainers() == null) {
return null;
}

// Find container
return obj.getSpec().getContainers().stream().filter(cont ->
// Find by port
(cont.getPorts() != null &&
cont.getPorts().stream().anyMatch(port ->
(port.getName() != null && port.getName().equalsIgnoreCase("minecraft")) ||
(port.getContainerPort() != null && port.getContainerPort() == 25565))) ||
// Find by name
(cont.getName() != null && (cont.getName().equalsIgnoreCase("paper") ||
cont.getName().equalsIgnoreCase("spigot") ||
cont.getName().equalsIgnoreCase("bukkit")))
).findFirst().map(V1Container::getName).orElse(null);

}

public void start() {
factory.startAllRegisteredInformers();
}
Expand Down

0 comments on commit 7112329

Please sign in to comment.