Search in sources :

Example 1 with V1ContainerStatus

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);
        }
    });
}
Also used : TestExecutor(io.pravega.test.system.framework.TestExecutor) RandomStringUtils.randomAlphanumeric(org.apache.commons.lang.RandomStringUtils.randomAlphanumeric) CompletableFuture(java.util.concurrent.CompletableFuture) TestFrameworkException(io.pravega.test.system.framework.TestFrameworkException) AtomicReference(java.util.concurrent.atomic.AtomicReference) V1VolumeMountBuilder(io.kubernetes.client.openapi.models.V1VolumeMountBuilder) V1VolumeBuilder(io.kubernetes.client.openapi.models.V1VolumeBuilder) Map(java.util.Map) NotImplementedException(org.apache.commons.lang.NotImplementedException) V1beta1ClusterRoleBinding(io.kubernetes.client.openapi.models.V1beta1ClusterRoleBinding) V1ServiceAccount(io.kubernetes.client.openapi.models.V1ServiceAccount) V1ContainerStatus(io.kubernetes.client.openapi.models.V1ContainerStatus) V1SecretVolumeSourceBuilder(io.kubernetes.client.openapi.models.V1SecretVolumeSourceBuilder) Utils(io.pravega.test.system.framework.Utils) V1beta1RoleRefBuilder(io.kubernetes.client.openapi.models.V1beta1RoleRefBuilder) Method(java.lang.reflect.Method) ImmutableMap(com.google.common.collect.ImmutableMap) K8sClient(io.pravega.test.system.framework.kubernetes.K8sClient) V1ServiceAccountBuilder(io.kubernetes.client.openapi.models.V1ServiceAccountBuilder) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) ClientFactory(io.pravega.test.system.framework.kubernetes.ClientFactory) V1beta1ClusterRoleBindingBuilder(io.kubernetes.client.openapi.models.V1beta1ClusterRoleBindingBuilder) Slf4j(lombok.extern.slf4j.Slf4j) V1PersistentVolumeClaimVolumeSourceBuilder(io.kubernetes.client.openapi.models.V1PersistentVolumeClaimVolumeSourceBuilder) V1PodBuilder(io.kubernetes.client.openapi.models.V1PodBuilder) V1beta1SubjectBuilder(io.kubernetes.client.openapi.models.V1beta1SubjectBuilder) V1ObjectMetaBuilder(io.kubernetes.client.openapi.models.V1ObjectMetaBuilder) Futures(io.pravega.common.concurrent.Futures) V1Pod(io.kubernetes.client.openapi.models.V1Pod) CompletableFuture(java.util.concurrent.CompletableFuture) K8sClient(io.pravega.test.system.framework.kubernetes.K8sClient) V1ContainerStatus(io.kubernetes.client.openapi.models.V1ContainerStatus) CompletionException(java.util.concurrent.CompletionException) V1Pod(io.kubernetes.client.openapi.models.V1Pod) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 2 with V1ContainerStatus

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();
}
Also used : V1ContainerStatus(io.kubernetes.client.openapi.models.V1ContainerStatus) V1ServiceAccount(io.kubernetes.client.openapi.models.V1ServiceAccount) Cleanup(lombok.Cleanup) TypeToken(com.google.gson.reflect.TypeToken) V1ContainerState(io.kubernetes.client.openapi.models.V1ContainerState) Watch(io.kubernetes.client.util.Watch) V1Pod(io.kubernetes.client.openapi.models.V1Pod) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) SneakyThrows(lombok.SneakyThrows)

Aggregations

V1ContainerStatus (io.kubernetes.client.openapi.models.V1ContainerStatus)2 V1Pod (io.kubernetes.client.openapi.models.V1Pod)2 V1ServiceAccount (io.kubernetes.client.openapi.models.V1ServiceAccount)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 TypeToken (com.google.gson.reflect.TypeToken)1 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)1 V1ContainerState (io.kubernetes.client.openapi.models.V1ContainerState)1 V1ObjectMetaBuilder (io.kubernetes.client.openapi.models.V1ObjectMetaBuilder)1 V1PersistentVolumeClaimVolumeSourceBuilder (io.kubernetes.client.openapi.models.V1PersistentVolumeClaimVolumeSourceBuilder)1 V1PodBuilder (io.kubernetes.client.openapi.models.V1PodBuilder)1 V1SecretVolumeSourceBuilder (io.kubernetes.client.openapi.models.V1SecretVolumeSourceBuilder)1 V1ServiceAccountBuilder (io.kubernetes.client.openapi.models.V1ServiceAccountBuilder)1 V1VolumeBuilder (io.kubernetes.client.openapi.models.V1VolumeBuilder)1 V1VolumeMountBuilder (io.kubernetes.client.openapi.models.V1VolumeMountBuilder)1 V1beta1ClusterRoleBinding (io.kubernetes.client.openapi.models.V1beta1ClusterRoleBinding)1 V1beta1ClusterRoleBindingBuilder (io.kubernetes.client.openapi.models.V1beta1ClusterRoleBindingBuilder)1 V1beta1RoleRefBuilder (io.kubernetes.client.openapi.models.V1beta1RoleRefBuilder)1 V1beta1SubjectBuilder (io.kubernetes.client.openapi.models.V1beta1SubjectBuilder)1 Watch (io.kubernetes.client.util.Watch)1 Futures (io.pravega.common.concurrent.Futures)1