use of io.kubernetes.client.openapi.models.V1ContainerStatus in project pravega by pravega.
the class K8SequentialExecutor method startTestExecution.
@Override
public CompletableFuture<Void> startTestExecution(Method testMethod) {
final String className = testMethod.getDeclaringClass().getName();
final String methodName = testMethod.getName();
// pod name is the combination of a test method name and random Alphanumeric. It cannot be more than 63 characters.
final String podName = (methodName + "-" + randomAlphanumeric(5)).toLowerCase();
log.info("Start execution of test {}#{} on the KUBERNETES Cluster", className, methodName);
final K8sClient client = ClientFactory.INSTANCE.getK8sClient();
Map<String, V1ContainerStatus> podStatusBeforeTest = getPravegaPodStatus(client);
final V1Pod pod = getTestPod(className, methodName, podName.toLowerCase());
final AtomicReference<CompletableFuture<Void>> logDownload = new AtomicReference<>(CompletableFuture.completedFuture(null));
return // create service Account, ignore if already present.
client.createServiceAccount(NAMESPACE, getServiceAccount()).thenCompose(// ensure test pod has cluster admin rights.
v -> client.createClusterRoleBinding(getClusterRoleBinding())).thenCompose(// deploy test pod.
v -> client.deployPod(NAMESPACE, pod)).thenCompose(v -> {
// start download of logs.
if (!Utils.isSkipLogDownloadEnabled()) {
logDownload.set(client.downloadLogs(pod, "./build/test-results/" + podName));
}
return client.waitUntilPodCompletes(NAMESPACE, podName);
}).handle((s, t) -> {
Futures.getAndHandleExceptions(logDownload.get(), t1 -> {
log.error("Failed to download logs for {}#{}", className, methodName, t1);
return null;
});
if (t == null) {
log.info("Test {}#{} execution completed with status {}", className, methodName, s);
verifyPravegaPodRestart(podStatusBeforeTest, getPravegaPodStatus(client));
if (s.getExitCode() != 0) {
log.error("Test {}#{} failed. Details: {}", className, methodName, s);
throw new AssertionError(methodName + " test failed due to " + s.getReason() + " with message " + s.getMessage());
} else {
return null;
}
} else {
throw new CompletionException("Error while invoking the test " + podName, t);
}
});
}
use of io.kubernetes.client.openapi.models.V1ContainerStatus in project pravega by pravega.
the class K8sClient method createAWatchAndReturnOnTermination.
/**
* Create a Watch for a pod and return once the pod has terminated.
* @param namespace Namespace.
* @param podName Name of the pod.
* @return V1ContainerStateTerminated.
*/
@SneakyThrows({ ApiException.class, IOException.class })
private Optional<V1ContainerStateTerminated> createAWatchAndReturnOnTermination(String namespace, String podName) {
log.debug("Creating a watch for pod {}/{}", namespace, podName);
CoreV1Api api = new CoreV1Api();
K8AsyncCallback<V1ServiceAccount> callback = new K8AsyncCallback<>("createAWatchAndReturnOnTermination-" + podName);
@Cleanup Watch<V1Pod> watch = Watch.createWatch(client, api.listNamespacedPodCall(namespace, PRETTY_PRINT, ALLOW_WATCH_BOOKMARKS, null, null, "POD_NAME=" + podName, null, null, null, Boolean.TRUE, callback), new TypeToken<Watch.Response<V1Pod>>() {
}.getType());
for (Watch.Response<V1Pod> v1PodResponse : watch) {
List<V1ContainerStatus> containerStatuses = v1PodResponse.object.getStatus().getContainerStatuses();
log.debug("Container status for the pod {} is {}", podName, containerStatuses);
if (containerStatuses == null || containerStatuses.size() == 0) {
log.debug("Container status is not part of the pod {}/{}, wait for the next update from KUBERNETES Cluster", namespace, podName);
continue;
}
// We check only the first container as there is only one container in the pod.
V1ContainerState containerStatus = containerStatuses.get(0).getState();
log.debug("Current container status is {}", containerStatus);
if (containerStatus.getTerminated() != null) {
log.info("Pod {}/{} has terminated", namespace, podName);
return Optional.of(containerStatus.getTerminated());
}
}
return Optional.empty();
}
Aggregations