Skip to content

Commit 908a4d8

Browse files
k-wallrobobario
authored andcommitted
Operator ITs - log pod status if operand preload fails
Contributes towards kroxylicious#1952 Signed-off-by: Keith Wall <kwall@apache.org>
1 parent 3a1987b commit 908a4d8

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

kroxylicious-operator/src/test/java/io/kroxylicious/kubernetes/operator/OperatorTestUtils.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,28 @@
66

77
package io.kroxylicious.kubernetes.operator;
88

9+
import java.util.Objects;
10+
import java.util.concurrent.TimeUnit;
11+
import java.util.stream.Collectors;
12+
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
import io.fabric8.kubernetes.api.model.ContainerStatus;
917
import io.fabric8.kubernetes.client.KubernetesClient;
1018
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
1119
import io.fabric8.kubernetes.client.KubernetesClientException;
20+
import io.fabric8.kubernetes.client.readiness.Readiness;
1221

22+
import edu.umd.cs.findbugs.annotations.NonNull;
1323
import edu.umd.cs.findbugs.annotations.Nullable;
1424

25+
import static org.assertj.core.api.Assertions.assertThat;
26+
1527
public class OperatorTestUtils {
1628

29+
private static final Logger LOGGER = LoggerFactory.getLogger(OperatorTestUtils.class);
30+
1731
/**
1832
* The timeouts etc of this client build are tuned to handle the case where Kubernetes isn't present.
1933
* As might be the case on a developer's machine where minikube isn't running.
@@ -28,6 +42,12 @@ public class OperatorTestUtils {
2842
return kubeClientIfAvailable(new KubernetesClientBuilder());
2943
}
3044

45+
static @NonNull KubernetesClient kubeClient() {
46+
KubernetesClient kubernetesClient = kubeClientIfAvailable(new KubernetesClientBuilder());
47+
assertThat(kubernetesClient).isNotNull();
48+
return kubernetesClient;
49+
}
50+
3151
static @Nullable KubernetesClient kubeClientIfAvailable(KubernetesClientBuilder kubernetesClientBuilder) {
3252
var client = kubernetesClientBuilder.build();
3353
try {
@@ -45,4 +65,27 @@ static boolean isKubeClientAvailable() {
4565
return client != null;
4666
}
4767
}
68+
69+
public static void preloadOperandImage() {
70+
try (var client = kubeClient()) {
71+
String operandImage = ProxyDeployment.getOperandImage();
72+
var pod = client.run().withName("preload-operand-image")
73+
.withNewRunConfig()
74+
.withImage(operandImage)
75+
.withRestartPolicy("Never")
76+
.withCommand("ls").done();
77+
try {
78+
client.resource(pod).waitUntilCondition(Readiness::isPodSucceeded, 2, TimeUnit.MINUTES);
79+
}
80+
finally {
81+
var reread = client.resource(pod).get();
82+
if (!Readiness.isPodSucceeded(reread)) {
83+
var reasons = reread.getStatus().getContainerStatuses().stream().map(ContainerStatus::getState).map(Objects::toString)
84+
.collect(Collectors.joining(","));
85+
LOGGER.error("Preloading operand image failed, phase: {}, container state: {}", reread.getStatus().getPhase(), reasons);
86+
}
87+
client.resource(pod).delete();
88+
}
89+
}
90+
}
4891
}

kroxylicious-operator/src/test/java/io/kroxylicious/kubernetes/operator/ProxyReconcilerIT.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,20 @@
99
import java.time.Duration;
1010
import java.util.List;
1111
import java.util.Set;
12-
import java.util.concurrent.TimeUnit;
1312

1413
import org.assertj.core.api.AbstractStringAssert;
15-
import org.assertj.core.api.Assumptions;
1614
import org.assertj.core.api.InstanceOfAssertFactories;
1715
import org.awaitility.core.ConditionFactory;
1816
import org.junit.jupiter.api.AfterEach;
17+
import org.junit.jupiter.api.BeforeAll;
1918
import org.junit.jupiter.api.BeforeEach;
2019
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.condition.EnabledIf;
2121
import org.junit.jupiter.api.extension.RegisterExtension;
2222
import org.slf4j.Logger;
2323
import org.slf4j.LoggerFactory;
2424

2525
import io.fabric8.kubernetes.api.model.ConfigMap;
26-
import io.fabric8.kubernetes.api.model.Pod;
2726
import io.fabric8.kubernetes.api.model.Service;
2827
import io.fabric8.kubernetes.api.model.Volume;
2928
import io.fabric8.kubernetes.api.model.apps.Deployment;
@@ -49,6 +48,7 @@
4948
import static org.assertj.core.api.Assertions.assertThat;
5049
import static org.awaitility.Awaitility.await;
5150

51+
@EnabledIf(value = "io.kroxylicious.kubernetes.operator.OperatorTestUtils#isKubeClientAvailable", disabledReason = "no viable kube client available")
5252
class ProxyReconcilerIT {
5353

5454
private static final Logger LOGGER = LoggerFactory.getLogger(ProxyReconcilerIT.class);
@@ -68,23 +68,15 @@ class ProxyReconcilerIT {
6868
private KubernetesClient client;
6969
private final ConditionFactory AWAIT = await().timeout(Duration.ofSeconds(60));
7070

71-
@BeforeEach
72-
void checkKubeAvailable() {
73-
client = OperatorTestUtils.kubeClientIfAvailable();
74-
Assumptions.assumeThat(client).describedAs("Test requires a viable kube client").isNotNull();
75-
preloadOperatorImage();
71+
// the initial operator image pull can take a long time and interfere with the tests
72+
@BeforeAll
73+
public static void preloadOperandImage() {
74+
OperatorTestUtils.preloadOperandImage();
7675
}
7776

78-
// the initial operator image pull can take a long time and interfere with the tests
79-
private void preloadOperatorImage() {
80-
String operandImage = ProxyDeployment.getOperandImage();
81-
Pod pod = client.run().withName("preload-operator-image")
82-
.withNewRunConfig()
83-
.withImage(operandImage)
84-
.withRestartPolicy("Never")
85-
.withCommand("ls").done();
86-
client.resource(pod).waitUntilCondition(it -> it.getStatus().getPhase().equals("Succeeded"), 2, TimeUnit.MINUTES);
87-
client.resource(pod).delete();
77+
@BeforeEach
78+
void beforeEach() {
79+
client = OperatorTestUtils.kubeClient();
8880
}
8981

9082
@RegisterExtension

0 commit comments

Comments
 (0)