Search in sources :

Example 21 with Status

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
}
Also used : Status(io.fabric8.kubernetes.api.model.Status) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Test(org.testng.annotations.Test)

Example 22 with Status

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");
}
Also used : Status(io.fabric8.kubernetes.api.model.Status) Project(io.fabric8.openshift.api.model.Project) KubernetesNamespaceMeta(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta) SubjectImpl(org.eclipse.che.commons.subject.SubjectImpl) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Test(org.testng.annotations.Test)

Example 23 with Status

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);
}
Also used : Status(io.fabric8.kubernetes.api.model.Status) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Namespace(io.fabric8.kubernetes.api.model.Namespace) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Test(org.testng.annotations.Test)

Example 24 with Status

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);
    }
}
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 25 with Status

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
}
Also used : Status(io.fabric8.kubernetes.api.model.Status) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Test(org.testng.annotations.Test)

Aggregations

Test (org.junit.jupiter.api.Test)210 Pod (io.fabric8.kubernetes.api.model.Pod)147 Reconciliation (io.strimzi.operator.common.Reconciliation)146 Checkpoint (io.vertx.junit5.Checkpoint)128 List (java.util.List)128 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)126 PlatformFeaturesAvailability (io.strimzi.operator.PlatformFeaturesAvailability)118 ResourceOperatorSupplier (io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier)118 Future (io.vertx.core.Future)111 SecretOperator (io.strimzi.operator.common.operator.resource.SecretOperator)104 Vertx (io.vertx.core.Vertx)102 Optional (java.util.Optional)98 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)97 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)97 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)94 VertxExtension (io.vertx.junit5.VertxExtension)92 VertxTestContext (io.vertx.junit5.VertxTestContext)92 Map (java.util.Map)89 CoreMatchers.is (org.hamcrest.CoreMatchers.is)86 ArrayList (java.util.ArrayList)84