Search in sources :

Example 1 with ContainerState

use of io.fabric8.kubernetes.api.model.ContainerState in project fabric8 by fabric8io.

the class SessionPodsAreReady method call.

@Override
public Boolean call() throws Exception {
    boolean result = true;
    List<Pod> pods = notNullList(kubernetesClient.pods().inNamespace(session.getNamespace()).list().getItems());
    if (pods.isEmpty()) {
        result = false;
        session.getLogger().warn("No pods are available yet, waiting...");
    }
    for (Pod pod : pods) {
        if (!KubernetesHelper.isPodReady(pod)) {
            PodStatus podStatus = pod.getStatus();
            int restartCount = 0;
            if (podStatus != null) {
                if ("Succeeded".equals(podStatus.getPhase())) {
                    // that have finished.  see: OSFUSE-317
                    continue;
                }
                List<ContainerStatus> containerStatuses = podStatus.getContainerStatuses();
                for (ContainerStatus containerStatus : containerStatuses) {
                    if (restartCount == 0) {
                        Integer restartCountValue = containerStatus.getRestartCount();
                        if (restartCountValue != null) {
                            restartCount = restartCountValue.intValue();
                        }
                    }
                    ContainerState state = containerStatus.getState();
                    if (state != null) {
                        ContainerStateWaiting waiting = state.getWaiting();
                        String containerName = containerStatus.getName();
                        if (waiting != null) {
                            session.getLogger().warn("Waiting for container:" + containerName + ". Reason:" + waiting.getReason());
                        } else {
                            session.getLogger().warn("Waiting for container:" + containerName + ".");
                        }
                    }
                }
            }
            result = false;
            String name = KubernetesHelper.getName(pod);
            File yamlFile = new File(session.getBaseDir(), "target/test-pod-status/" + name + ".yml");
            yamlFile.getParentFile().mkdirs();
            try {
                KubernetesHelper.saveYaml(pod, yamlFile);
            } catch (IOException e) {
                session.getLogger().warn("Failed to write " + yamlFile + ". " + e);
            }
            if (KubernetesHelper.isPodRunning(pod)) {
                List<Container> containers = pod.getSpec().getContainers();
                for (Container container : containers) {
                    File logFile = LogHelpers.getLogFileName(session.getBaseDir(), name, container, restartCount);
                    String log = kubernetesClient.pods().inNamespace(session.getNamespace()).withName(name).inContainer(container.getName()).getLog();
                    IOHelpers.writeFully(logFile, log);
                }
            }
        }
    }
    return result;
}
Also used : ContainerStateWaiting(io.fabric8.kubernetes.api.model.ContainerStateWaiting) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) ContainerState(io.fabric8.kubernetes.api.model.ContainerState) PodStatus(io.fabric8.kubernetes.api.model.PodStatus) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) Container(io.fabric8.kubernetes.api.model.Container) File(java.io.File)

Aggregations

Container (io.fabric8.kubernetes.api.model.Container)1 ContainerState (io.fabric8.kubernetes.api.model.ContainerState)1 ContainerStateWaiting (io.fabric8.kubernetes.api.model.ContainerStateWaiting)1 ContainerStatus (io.fabric8.kubernetes.api.model.ContainerStatus)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 PodStatus (io.fabric8.kubernetes.api.model.PodStatus)1 File (java.io.File)1 IOException (java.io.IOException)1