Skip to content

Commit 609e051

Browse files
csviriclaude
andcommitted
fix: wait for pods to be ready before port-forwarding in MetricsHandlingE2E
Replace one-shot pod lookup with an awaitility-based readiness wait (up to 3 minutes) to avoid IllegalStateException when pods are not yet scheduled after observability stack installation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 78eecc0 commit 609e051

File tree

1 file changed

+41
-18
lines changed
  • sample-operators/metrics-processing/src/test/java/io/javaoperatorsdk/operator/sample/metrics

1 file changed

+41
-18
lines changed

sample-operators/metrics-processing/src/test/java/io/javaoperatorsdk/operator/sample/metrics/MetricsHandlingE2E.java

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,14 @@ class MetricsHandlingE2E {
8181
.build();
8282

8383
@BeforeAll
84-
void setupObservability() throws InterruptedException {
84+
void setupObservability() {
8585
log.info("Setting up observability stack...");
8686
installObservabilityServices();
8787
prometheusPortForward = portForward(NAME_LABEL_KEY, "prometheus", PROMETHEUS_PORT);
8888
if (isLocal()) {
8989
otelCollectorPortForward =
9090
portForward(NAME_LABEL_KEY, "otel-collector-collector", OTEL_COLLECTOR_PORT);
9191
}
92-
Thread.sleep(2000);
9392
}
9493

9594
@AfterAll
@@ -99,25 +98,49 @@ void cleanup() throws IOException {
9998
}
10099

101100
private LocalPortForward portForward(String labelKey, String labelValue, int port) {
101+
log.info("Waiting for pod with label {}={} to be ready...", labelKey, labelValue);
102+
await()
103+
.atMost(Duration.ofMinutes(3))
104+
.pollInterval(Duration.ofSeconds(5))
105+
.until(
106+
() -> {
107+
var pods =
108+
client
109+
.pods()
110+
.inNamespace(OBSERVABILITY_NAMESPACE)
111+
.withLabel(labelKey, labelValue)
112+
.list()
113+
.getItems();
114+
return pods.stream()
115+
.anyMatch(
116+
pod ->
117+
pod.getStatus() != null
118+
&& pod.getStatus().getConditions() != null
119+
&& pod.getStatus().getConditions().stream()
120+
.anyMatch(
121+
c ->
122+
"Ready".equals(c.getType())
123+
&& "True".equals(c.getStatus())));
124+
});
125+
var pod =
126+
client
127+
.pods()
128+
.inNamespace(OBSERVABILITY_NAMESPACE)
129+
.withLabel(labelKey, labelValue)
130+
.list()
131+
.getItems()
132+
.stream()
133+
.findFirst()
134+
.orElseThrow(
135+
() ->
136+
new IllegalStateException(
137+
"Pod not found for label " + labelKey + "=" + labelValue));
138+
log.info("Pod {} is ready, establishing port-forward on port {}", pod.getMetadata().getName(), port);
102139
return client
103140
.pods()
104141
.inNamespace(OBSERVABILITY_NAMESPACE)
105-
.withLabel(labelKey, labelValue)
106-
.list()
107-
.getItems()
108-
.stream()
109-
.findFirst()
110-
.map(
111-
pod ->
112-
client
113-
.pods()
114-
.inNamespace(OBSERVABILITY_NAMESPACE)
115-
.withName(pod.getMetadata().getName())
116-
.portForward(port, port))
117-
.orElseThrow(
118-
() ->
119-
new IllegalStateException(
120-
"Pod not found for label " + labelKey + "=" + labelValue));
142+
.withName(pod.getMetadata().getName())
143+
.portForward(port, port);
121144
}
122145

123146
private void closePortForward(LocalPortForward pf) throws IOException {

0 commit comments

Comments
 (0)