Search in sources :

Example 1 with ContainerStateTerminated

use of io.fabric8.kubernetes.api.model.ContainerStateTerminated in project che-server by eclipse-che.

the class KubernetesDeployments method handleStartingPodStatus.

private void handleStartingPodStatus(CompletableFuture<Void> podRunningFuture, Pod pod) {
    PodStatus status = pod.getStatus();
    String podPhase = status.getPhase();
    if (POD_STATUS_PHASE_RUNNING.equals(podPhase)) {
        // check that all the containers are ready...
        Map<String, String> terminatedContainers = new HashMap<>();
        List<String> restartingContainers = new ArrayList<>();
        for (ContainerStatus cs : status.getContainerStatuses()) {
            ContainerStateTerminated terminated = cs.getState().getTerminated();
            if (terminated == null) {
                ContainerStateWaiting waiting = cs.getState().getWaiting();
                // we've caught the container waiting for a restart after a failure
                if (waiting != null) {
                    terminated = cs.getLastState().getTerminated();
                }
            }
            if (terminated != null) {
                terminatedContainers.put(cs.getName(), format("reason = '%s', exit code = %d, message = '%s'", terminated.getReason(), terminated.getExitCode(), terminated.getMessage()));
            }
            if (cs.getRestartCount() != null && cs.getRestartCount() > 0) {
                restartingContainers.add(cs.getName());
            }
        }
        if (terminatedContainers.isEmpty() && restartingContainers.isEmpty()) {
            podRunningFuture.complete(null);
        } else {
            StringBuilder errorMessage = new StringBuilder();
            if (!restartingContainers.isEmpty()) {
                errorMessage.append("The following containers have restarted during the startup:\n");
                errorMessage.append(String.join(", ", restartingContainers));
            }
            if (!terminatedContainers.isEmpty()) {
                if (errorMessage.length() > 0) {
                    errorMessage.append("\n");
                }
                errorMessage.append("The following containers have terminated:\n");
                errorMessage.append(terminatedContainers.entrySet().stream().map(e -> e.getKey() + ": " + e.getValue()).collect(Collectors.joining("" + "\n")));
            }
            podRunningFuture.completeExceptionally(new InfrastructureException(errorMessage.toString()));
        }
        return;
    }
    if (POD_STATUS_PHASE_SUCCEEDED.equals(podPhase)) {
        podRunningFuture.completeExceptionally(new InfrastructureException("Pod container has been terminated. Container must be configured to use a non-terminating command."));
        return;
    }
    if (POD_STATUS_PHASE_FAILED.equals(podPhase)) {
        String exceptionMessage = "Pod '" + pod.getMetadata().getName() + "' failed to start.";
        String reason = pod.getStatus().getReason();
        if (Strings.isNullOrEmpty(reason)) {
            try {
                String podLog = clientFactory.create(workspaceId).pods().inNamespace(namespace).withName(pod.getMetadata().getName()).getLog();
                exceptionMessage = exceptionMessage.concat(" Pod logs: ").concat(podLog);
            } catch (InfrastructureException | KubernetesClientException e) {
                exceptionMessage = exceptionMessage.concat(" Error occurred while fetching pod logs: " + e.getMessage());
            }
        } else {
            exceptionMessage = exceptionMessage.concat(" Reason: ").concat(reason);
        }
        podRunningFuture.completeExceptionally(new InfrastructureException(exceptionMessage));
        LOG.warn(exceptionMessage);
    }
}
Also used : ContainerStateWaiting(io.fabric8.kubernetes.api.model.ContainerStateWaiting) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ContainerStateTerminated(io.fabric8.kubernetes.api.model.ContainerStateTerminated) PodStatus(io.fabric8.kubernetes.api.model.PodStatus) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 2 with ContainerStateTerminated

use of io.fabric8.kubernetes.api.model.ContainerStateTerminated in project devspaces-images by redhat-developer.

the class KubernetesDeployments method handleStartingPodStatus.

private void handleStartingPodStatus(CompletableFuture<Void> podRunningFuture, Pod pod) {
    PodStatus status = pod.getStatus();
    String podPhase = status.getPhase();
    if (POD_STATUS_PHASE_RUNNING.equals(podPhase)) {
        // check that all the containers are ready...
        Map<String, String> terminatedContainers = new HashMap<>();
        List<String> restartingContainers = new ArrayList<>();
        for (ContainerStatus cs : status.getContainerStatuses()) {
            ContainerStateTerminated terminated = cs.getState().getTerminated();
            if (terminated == null) {
                ContainerStateWaiting waiting = cs.getState().getWaiting();
                // we've caught the container waiting for a restart after a failure
                if (waiting != null) {
                    terminated = cs.getLastState().getTerminated();
                }
            }
            if (terminated != null) {
                terminatedContainers.put(cs.getName(), format("reason = '%s', exit code = %d, message = '%s'", terminated.getReason(), terminated.getExitCode(), terminated.getMessage()));
            }
            if (cs.getRestartCount() != null && cs.getRestartCount() > 0) {
                restartingContainers.add(cs.getName());
            }
        }
        if (terminatedContainers.isEmpty() && restartingContainers.isEmpty()) {
            podRunningFuture.complete(null);
        } else {
            StringBuilder errorMessage = new StringBuilder();
            if (!restartingContainers.isEmpty()) {
                errorMessage.append("The following containers have restarted during the startup:\n");
                errorMessage.append(String.join(", ", restartingContainers));
            }
            if (!terminatedContainers.isEmpty()) {
                if (errorMessage.length() > 0) {
                    errorMessage.append("\n");
                }
                errorMessage.append("The following containers have terminated:\n");
                errorMessage.append(terminatedContainers.entrySet().stream().map(e -> e.getKey() + ": " + e.getValue()).collect(Collectors.joining("" + "\n")));
            }
            podRunningFuture.completeExceptionally(new InfrastructureException(errorMessage.toString()));
        }
        return;
    }
    if (POD_STATUS_PHASE_SUCCEEDED.equals(podPhase)) {
        podRunningFuture.completeExceptionally(new InfrastructureException("Pod container has been terminated. Container must be configured to use a non-terminating command."));
        return;
    }
    if (POD_STATUS_PHASE_FAILED.equals(podPhase)) {
        String exceptionMessage = "Pod '" + pod.getMetadata().getName() + "' failed to start.";
        String reason = pod.getStatus().getReason();
        if (Strings.isNullOrEmpty(reason)) {
            try {
                String podLog = clientFactory.create(workspaceId).pods().inNamespace(namespace).withName(pod.getMetadata().getName()).getLog();
                exceptionMessage = exceptionMessage.concat(" Pod logs: ").concat(podLog);
            } catch (InfrastructureException | KubernetesClientException e) {
                exceptionMessage = exceptionMessage.concat(" Error occurred while fetching pod logs: " + e.getMessage());
            }
        } else {
            exceptionMessage = exceptionMessage.concat(" Reason: ").concat(reason);
        }
        podRunningFuture.completeExceptionally(new InfrastructureException(exceptionMessage));
        LOG.warn(exceptionMessage);
    }
}
Also used : ContainerStateWaiting(io.fabric8.kubernetes.api.model.ContainerStateWaiting) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ContainerStateTerminated(io.fabric8.kubernetes.api.model.ContainerStateTerminated) PodStatus(io.fabric8.kubernetes.api.model.PodStatus) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 3 with ContainerStateTerminated

use of io.fabric8.kubernetes.api.model.ContainerStateTerminated in project kubernetes-client by fabric8io.

the class PodStatusUtilTest method isRunning_should_return_true_if_pod_has_running_container_and_another_that_is_terminated_for_other_reason_than_completed.

@Test
public void isRunning_should_return_true_if_pod_has_running_container_and_another_that_is_terminated_for_other_reason_than_completed() {
    // given
    Pod pod = pod("some pod").statusBuilder().containerStatuses(runningReady, containerStatus(false, containerState(containerStateTerminated(null, "I was bored"), null, null))).build().build();
    // when
    boolean running = PodStatusUtil.isRunning(pod);
    // then
    assertThat(running).isTrue();
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) Test(org.junit.Test)

Example 4 with ContainerStateTerminated

use of io.fabric8.kubernetes.api.model.ContainerStateTerminated in project kubernetes-client by fabric8io.

the class PodMockUtils method containerState.

/**
 * Returns a {@link ContainerState} for the given terminated, waiting and running state.
 *
 * @param terminated the terminated state
 * @param waiting the waiting state
 * @param running the running state
 *
 * @return a container state
 *
 * @see ContainerStatus#getState()
 */
public static ContainerState containerState(ContainerStateTerminated terminated, ContainerStateWaiting waiting, ContainerStateRunning running) {
    ContainerState mock = mock(ContainerState.class);
    when(mock.getTerminated()).thenReturn(terminated);
    when(mock.getWaiting()).thenReturn(waiting);
    when(mock.getRunning()).thenReturn(running);
    return mock;
}
Also used : ContainerState(io.fabric8.kubernetes.api.model.ContainerState)

Example 5 with ContainerStateTerminated

use of io.fabric8.kubernetes.api.model.ContainerStateTerminated in project kubernetes-client by fabric8io.

the class PodMockUtils method containerStateTerminated.

/**
 * Returns a {@link ContainerStateTerminated} for the given exit code and reason.
 *
 * @param exitCode the exist code
 * @param reason the reason
 *
 * @return the container state terminated
 *
 * @see ContainerState#getTerminated()
 */
public static ContainerStateTerminated containerStateTerminated(Integer exitCode, String reason) {
    ContainerStateTerminated mock = mock(ContainerStateTerminated.class);
    when(mock.getExitCode()).thenReturn(exitCode);
    when(mock.getReason()).thenReturn(reason);
    return mock;
}
Also used : ContainerStateTerminated(io.fabric8.kubernetes.api.model.ContainerStateTerminated)

Aggregations

ContainerStateTerminated (io.fabric8.kubernetes.api.model.ContainerStateTerminated)12 Pod (io.fabric8.kubernetes.api.model.Pod)12 Test (org.junit.Test)8 ContainerStatus (io.fabric8.kubernetes.api.model.ContainerStatus)6 ContainerState (io.fabric8.kubernetes.api.model.ContainerState)5 RunState (com.spotify.styx.state.RunState)4 PodStatus (io.fabric8.kubernetes.api.model.PodStatus)4 Objects (java.util.Objects)4 KubernetesPodEventTranslatorTest.terminatedContainerState (com.spotify.styx.docker.KubernetesPodEventTranslatorTest.terminatedContainerState)3 State (com.spotify.styx.state.RunState.State)3 LocalObjectReference (io.fabric8.kubernetes.api.model.LocalObjectReference)3 List (java.util.List)3 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)2 ContainerStateWaiting (io.fabric8.kubernetes.api.model.ContainerStateWaiting)2 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)2 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)2 Build (io.fabric8.openshift.api.model.Build)2 KafkaConnectResources (io.strimzi.api.kafka.model.KafkaConnectResources)2 PlatformFeaturesAvailability (io.strimzi.operator.PlatformFeaturesAvailability)2 ClusterOperatorConfig (io.strimzi.operator.cluster.ClusterOperatorConfig)2