Skip to content

Utility around curl in Cluster #966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,54 @@
package io.javaoperatorsdk.operator.junit;

import java.util.UUID;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.extended.run.RunConfigBuilder;
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil;

import static java.util.concurrent.TimeUnit.MINUTES;
import static org.awaitility.Awaitility.await;

public class InClusterCurl {

private final KubernetesClient client;
private final String namespace;

public InClusterCurl(KubernetesClient client, String namespace) {
this.client = client;
this.namespace = namespace;
}

public String checkUrl(String url) {
String podName = KubernetesResourceUtil.sanitizeName("curl-" + UUID.randomUUID());
try {
Pod curlPod = client.run().inNamespace(namespace)
.withRunConfig(new RunConfigBuilder()
.withArgs("-s", "-o", "/dev/null", "-w", "%{http_code}", url)
.withName(podName)
.withImage("curlimages/curl:7.78.0")
.withRestartPolicy("Never")
.build())
.done();
await("wait-for-curl-pod-run").atMost(2, MINUTES)
.until(() -> {
String phase =
client.pods().inNamespace(namespace).withName(podName).get()
.getStatus().getPhase();
return phase.equals("Succeeded") || phase.equals("Failed");
});

String curlOutput =
client.pods().inNamespace(namespace)
.withName(curlPod.getMetadata().getName()).getLog();

return curlOutput;
} finally {
client.pods().inNamespace(namespace).withName(podName).delete();
await("wait-for-curl-pod-stop").atMost(1, MINUTES)
.until(() -> client.pods().inNamespace(namespace).withName(podName)
.get() == null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.client.*;
import io.fabric8.kubernetes.client.extended.run.RunConfigBuilder;
import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService;
import io.javaoperatorsdk.operator.junit.AbstractOperatorExtension;
import io.javaoperatorsdk.operator.junit.E2EOperatorExtension;
import io.javaoperatorsdk.operator.junit.InClusterCurl;
import io.javaoperatorsdk.operator.junit.OperatorExtension;

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

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

await("wait-for-webapp").atMost(6, MINUTES).untilAsserted(() -> {
try {

log.info("Starting curl Pod to test if webapp was deployed correctly");
Pod curlPod = client.run().inNamespace(operator.getNamespace())
.withRunConfig(new RunConfigBuilder()
.withArgs("-s", "-o", "/dev/null", "-w", "%{http_code}", url)
.withName("curl")
.withImage("curlimages/curl:7.78.0")
.withRestartPolicy("Never")
.build())
.done();
log.info("Waiting for curl Pod to finish running");
await("wait-for-curl-pod-run").atMost(2, MINUTES)
.until(() -> {
String phase =
client.pods().inNamespace(operator.getNamespace()).withName("curl").get()
.getStatus().getPhase();
return phase.equals("Succeeded") || phase.equals("Failed");
});

String curlOutput =
client.pods().inNamespace(operator.getNamespace())
.withName(curlPod.getMetadata().getName()).getLog();
log.info("Output from curl: '{}'", curlOutput);
var curlOutput = inClusterCurl.checkUrl(url);
assertThat(curlOutput, equalTo("200"));
} catch (KubernetesClientException ex) {
throw new AssertionError(ex);
} finally {
log.info("Deleting curl Pod");
client.pods().inNamespace(operator.getNamespace()).withName("curl").delete();
await("wait-for-curl-pod-stop").atMost(1, MINUTES)
.until(() -> client.pods().inNamespace(operator.getNamespace()).withName("curl")
.get() == null);
}
});
}
Expand Down