Skip to content

Commit 2aa5064

Browse files
authored
Utility around curl in Cluster (#966)
1 parent 0c7110a commit 2aa5064

File tree

2 files changed

+57
-30
lines changed

2 files changed

+57
-30
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.javaoperatorsdk.operator.junit;
2+
3+
import java.util.UUID;
4+
5+
import io.fabric8.kubernetes.api.model.Pod;
6+
import io.fabric8.kubernetes.client.KubernetesClient;
7+
import io.fabric8.kubernetes.client.extended.run.RunConfigBuilder;
8+
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil;
9+
10+
import static java.util.concurrent.TimeUnit.MINUTES;
11+
import static org.awaitility.Awaitility.await;
12+
13+
public class InClusterCurl {
14+
15+
private final KubernetesClient client;
16+
private final String namespace;
17+
18+
public InClusterCurl(KubernetesClient client, String namespace) {
19+
this.client = client;
20+
this.namespace = namespace;
21+
}
22+
23+
public String checkUrl(String url) {
24+
String podName = KubernetesResourceUtil.sanitizeName("curl-" + UUID.randomUUID());
25+
try {
26+
Pod curlPod = client.run().inNamespace(namespace)
27+
.withRunConfig(new RunConfigBuilder()
28+
.withArgs("-s", "-o", "/dev/null", "-w", "%{http_code}", url)
29+
.withName(podName)
30+
.withImage("curlimages/curl:7.78.0")
31+
.withRestartPolicy("Never")
32+
.build())
33+
.done();
34+
await("wait-for-curl-pod-run").atMost(2, MINUTES)
35+
.until(() -> {
36+
String phase =
37+
client.pods().inNamespace(namespace).withName(podName).get()
38+
.getStatus().getPhase();
39+
return phase.equals("Succeeded") || phase.equals("Failed");
40+
});
41+
42+
String curlOutput =
43+
client.pods().inNamespace(namespace)
44+
.withName(curlPod.getMetadata().getName()).getLog();
45+
46+
return curlOutput;
47+
} finally {
48+
client.pods().inNamespace(namespace).withName(podName).delete();
49+
await("wait-for-curl-pod-stop").atMost(1, MINUTES)
50+
.until(() -> client.pods().inNamespace(namespace).withName(podName)
51+
.get() == null);
52+
}
53+
}
54+
}

sample-operators/tomcat-operator/src/test/java/io/javaoperatorsdk/operator/sample/TomcatOperatorE2E.java

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
import io.fabric8.kubernetes.api.model.*;
1212
import io.fabric8.kubernetes.client.*;
13-
import io.fabric8.kubernetes.client.extended.run.RunConfigBuilder;
1413
import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService;
1514
import io.javaoperatorsdk.operator.junit.AbstractOperatorExtension;
1615
import io.javaoperatorsdk.operator.junit.E2EOperatorExtension;
16+
import io.javaoperatorsdk.operator.junit.InClusterCurl;
1717
import io.javaoperatorsdk.operator.junit.OperatorExtension;
1818

1919
import static java.util.concurrent.TimeUnit.MINUTES;
@@ -107,42 +107,15 @@ public void test() {
107107

108108
String url =
109109
"http://" + tomcat.getMetadata().getName() + "/" + webapp1.getSpec().getContextPath() + "/";
110+
var inClusterCurl = new InClusterCurl(client, operator.getNamespace());
110111
log.info("Starting curl Pod and waiting 5 minutes for GET of {} to return 200", url);
111112

112113
await("wait-for-webapp").atMost(6, MINUTES).untilAsserted(() -> {
113114
try {
114-
115-
log.info("Starting curl Pod to test if webapp was deployed correctly");
116-
Pod curlPod = client.run().inNamespace(operator.getNamespace())
117-
.withRunConfig(new RunConfigBuilder()
118-
.withArgs("-s", "-o", "/dev/null", "-w", "%{http_code}", url)
119-
.withName("curl")
120-
.withImage("curlimages/curl:7.78.0")
121-
.withRestartPolicy("Never")
122-
.build())
123-
.done();
124-
log.info("Waiting for curl Pod to finish running");
125-
await("wait-for-curl-pod-run").atMost(2, MINUTES)
126-
.until(() -> {
127-
String phase =
128-
client.pods().inNamespace(operator.getNamespace()).withName("curl").get()
129-
.getStatus().getPhase();
130-
return phase.equals("Succeeded") || phase.equals("Failed");
131-
});
132-
133-
String curlOutput =
134-
client.pods().inNamespace(operator.getNamespace())
135-
.withName(curlPod.getMetadata().getName()).getLog();
136-
log.info("Output from curl: '{}'", curlOutput);
115+
var curlOutput = inClusterCurl.checkUrl(url);
137116
assertThat(curlOutput, equalTo("200"));
138117
} catch (KubernetesClientException ex) {
139118
throw new AssertionError(ex);
140-
} finally {
141-
log.info("Deleting curl Pod");
142-
client.pods().inNamespace(operator.getNamespace()).withName("curl").delete();
143-
await("wait-for-curl-pod-stop").atMost(1, MINUTES)
144-
.until(() -> client.pods().inNamespace(operator.getNamespace()).withName("curl")
145-
.get() == null);
146119
}
147120
});
148121
}

0 commit comments

Comments
 (0)