use of io.fabric8.kubernetes.api.model.Status in project devspaces-images by redhat-developer.
the class OpenShiftProjectFactoryTest method throwAnExceptionWhenErrorListingNamespaces.
@Test(expectedExceptions = InfrastructureException.class)
public void throwAnExceptionWhenErrorListingNamespaces() throws Exception {
// given
doThrow(new KubernetesClientException("Not allowed.", 500, new Status())).when(projectList).getItems();
projectFactory = new OpenShiftProjectFactory("<userid>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, true, emptySet(), clientFactory, cheClientFactory, cheServerOpenshiftClientFactory, userManager, preferenceManager, pool, NO_OAUTH_IDENTITY_PROVIDER);
// when
projectFactory.list();
// then throw
}
use of io.fabric8.kubernetes.api.model.Status in project devspaces-images by redhat-developer.
the class OpenShiftProjectFactoryTest method shouldNotThrowAnExceptionWhenNotAllowedToListNamespaces.
@Test
public void shouldNotThrowAnExceptionWhenNotAllowedToListNamespaces() throws Exception {
// given
Project p = createProject("ns1", "project1", "desc1", "Active");
doThrow(new KubernetesClientException("Not allowed.", 403, new Status())).when(projectList).getItems();
prepareNamespaceToBeFoundByName("u123-che", p);
projectFactory = new OpenShiftProjectFactory("<userid>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, true, emptySet(), clientFactory, cheClientFactory, cheServerOpenshiftClientFactory, userManager, preferenceManager, pool, NO_OAUTH_IDENTITY_PROVIDER);
EnvironmentContext.getCurrent().setSubject(new SubjectImpl("jondoe", "u123", null, false));
// when
List<KubernetesNamespaceMeta> availableNamespaces = projectFactory.list();
// then
assertEquals(availableNamespaces.get(0).getName(), "ns1");
}
use of io.fabric8.kubernetes.api.model.Status in project devspaces-images by redhat-developer.
the class OpenShiftProjectTest method testFailWhenFailToUpdateNamespace.
@Test(expectedExceptions = InfrastructureException.class)
public void testFailWhenFailToUpdateNamespace() throws InfrastructureException {
// given
Map<String, String> labels = Map.of("label.with.this", "yes", "and.this", "of courese");
prepareProject(PROJECT_NAME);
Namespace namespace = prepareNamespaceGet(PROJECT_NAME);
OpenShiftProject openShiftProject = new OpenShiftProject(clientFactory, cheClientFactory, cheServerOpenshiftClientFactory, executor, PROJECT_NAME, WORKSPACE_ID);
KubernetesClient cheKubeClient = mock(KubernetesClient.class);
lenient().doReturn(cheKubeClient).when(cheClientFactory).create();
NonNamespaceOperation nonNamespaceOperation = mock(NonNamespaceOperation.class);
lenient().doReturn(nonNamespaceOperation).when(cheKubeClient).namespaces();
lenient().doThrow(new KubernetesClientException("Some other error", 500, new Status())).when(nonNamespaceOperation).createOrReplace(any(Namespace.class));
// when
openShiftProject.prepare(true, true, labels, Map.of());
// then
verify(nonNamespaceOperation).createOrReplace(namespace);
}
use of io.fabric8.kubernetes.api.model.Status 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.Status in project devspaces-images by redhat-developer.
the class KubernetesNamespaceFactoryTest method throwAnExceptionWhenErrorListingNamespaces.
@Test(expectedExceptions = InfrastructureException.class)
public void throwAnExceptionWhenErrorListingNamespaces() throws Exception {
// given
doThrow(new KubernetesClientException("Not allowed.", 500, new Status())).when(namespaceList).getItems();
namespaceFactory = new KubernetesNamespaceFactory("<username>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, emptySet(), clientFactory, cheClientFactory, userManager, preferenceManager, pool);
// when
namespaceFactory.list();
// then throw
}
Aggregations