Search in sources :

Example 1 with V1ContainerState

use of io.kubernetes.client.openapi.models.V1ContainerState 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

TypeToken (com.google.gson.reflect.TypeToken)1 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)1 V1ContainerState (io.kubernetes.client.openapi.models.V1ContainerState)1 V1ContainerStatus (io.kubernetes.client.openapi.models.V1ContainerStatus)1 V1Pod (io.kubernetes.client.openapi.models.V1Pod)1 V1ServiceAccount (io.kubernetes.client.openapi.models.V1ServiceAccount)1 Watch (io.kubernetes.client.util.Watch)1 Cleanup (lombok.Cleanup)1 SneakyThrows (lombok.SneakyThrows)1