Skip to content
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.time.Duration;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Optional;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -31,6 +32,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
Expand Down Expand Up @@ -81,15 +83,14 @@ class MetricsHandlingE2E {
.build();

@BeforeAll
void setupObservability() throws InterruptedException {
void setupObservability() {
log.info("Setting up observability stack...");
installObservabilityServices();
prometheusPortForward = portForward(NAME_LABEL_KEY, "prometheus", PROMETHEUS_PORT);
if (isLocal()) {
otelCollectorPortForward =
portForward(NAME_LABEL_KEY, "otel-collector-collector", OTEL_COLLECTOR_PORT);
}
Thread.sleep(2000);
}

@AfterAll
Expand All @@ -99,25 +100,41 @@ void cleanup() throws IOException {
}

private LocalPortForward portForward(String labelKey, String labelValue, int port) {
log.info("Waiting for pod with label {}={} to be ready...", labelKey, labelValue);
await()
.atMost(Duration.ofMinutes(15))
.pollInterval(Duration.ofSeconds(10))
.until(() -> findReadyPod(labelKey, labelValue).isPresent());
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there is an API that can return the until result directly.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not sure, but can use AtomicRefernce for storing that.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

var pod =
findReadyPod(labelKey, labelValue)
.orElseThrow(
() ->
new IllegalStateException(
"Pod not found for label " + labelKey + "=" + labelValue));
log.info(
"Pod {} is ready, establishing port-forward on port {}", pod.getMetadata().getName(), port);
return client
.pods()
.inNamespace(OBSERVABILITY_NAMESPACE)
.withName(pod.getMetadata().getName())
.portForward(port, port);
}

private Optional<Pod> findReadyPod(String labelKey, String labelValue) {
return client
.pods()
.inNamespace(OBSERVABILITY_NAMESPACE)
.withLabel(labelKey, labelValue)
.list()
.getItems()
.stream()
.findFirst()
.map(
.filter(
pod ->
client
.pods()
.inNamespace(OBSERVABILITY_NAMESPACE)
.withName(pod.getMetadata().getName())
.portForward(port, port))
.orElseThrow(
() ->
new IllegalStateException(
"Pod not found for label " + labelKey + "=" + labelValue));
pod.getStatus() != null
&& pod.getStatus().getConditions() != null
&& pod.getStatus().getConditions().stream()
.anyMatch(c -> "Ready".equals(c.getType()) && "True".equals(c.getStatus())))
.findFirst();
}

private void closePortForward(LocalPortForward pf) throws IOException {
Expand Down