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);
}
}
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);
}
}
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();
}
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;
}
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;
}
Aggregations