Search in sources :

Example 6 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.

the class KeycloakProviderConfigFactoryTest method testRethrowOnInvalidTokenBadRequestException.

@Test
public void testRethrowOnInvalidTokenBadRequestException() throws Exception {
    doThrow(new BadRequestException(DtoFactory.newDto(ServiceError.class).withMessage("Invalid token."))).when(keycloakServiceClient).getIdentityProviderToken(anyString());
    try {
        configBuilder.buildConfig(defaultConfig, A_WORKSPACE_ID);
    } catch (InfrastructureException e) {
        assertEquals(e.getMessage(), SESSION_EXPIRED_MESSAGE, "The exception message is wrong");
        return;
    }
    fail("Should have thrown an exception with the following message: " + SESSION_EXPIRED_MESSAGE);
}
Also used : ServiceError(org.eclipse.che.api.core.rest.shared.dto.ServiceError) BadRequestException(org.eclipse.che.api.core.BadRequestException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Test(org.testng.annotations.Test)

Example 7 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.

the class KeycloakProviderConfigFactoryTest method testRethrowOnUnauthorizedException.

@Test
public void testRethrowOnUnauthorizedException() throws Exception {
    doThrow(new UnauthorizedException(DtoFactory.newDto(ServiceError.class).withMessage("Any other message"))).when(keycloakServiceClient).getIdentityProviderToken(anyString());
    try {
        configBuilder.buildConfig(defaultConfig, A_WORKSPACE_ID);
    } catch (InfrastructureException e) {
        assertEquals(e.getMessage(), SHOULD_LINK_ERROR_MESSAGE, "The exception message is wrong");
        return;
    }
    fail("Should have thrown an exception with the following message: " + SHOULD_LINK_ERROR_MESSAGE);
}
Also used : ServiceError(org.eclipse.che.api.core.rest.shared.dto.ServiceError) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Test(org.testng.annotations.Test)

Example 8 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.

the class KubernetesDeployments method createDeployment.

private Pod createDeployment(Deployment deployment, String workspaceId) throws InfrastructureException {
    addPullSecretsOfSA(deployment);
    final String deploymentName = deployment.getMetadata().getName();
    final CompletableFuture<Pod> createFuture = new CompletableFuture<>();
    final Watch createWatch = clientFactory.create(workspaceId).pods().inNamespace(namespace).withLabels(Map.of(CHE_WORKSPACE_ID_LABEL, workspaceId, CHE_DEPLOYMENT_NAME_LABEL, deploymentName)).watch(new CreateWatcher(createFuture, workspaceId, deploymentName));
    try {
        clientFactory.create(workspaceId).apps().deployments().inNamespace(namespace).create(deployment);
        return createFuture.get(POD_CREATION_TIMEOUT_MIN, TimeUnit.MINUTES);
    } catch (KubernetesClientException e) {
        throw new KubernetesInfrastructureException(e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new InfrastructureException(format("Interrupted while waiting for Pod creation. -id: %s -message: %s", deploymentName, e.getMessage()));
    } catch (ExecutionException e) {
        throw new InfrastructureException(format("Error occured while waiting for Pod creation. -id: %s -message: %s", deploymentName, e.getCause().getMessage()));
    } catch (TimeoutException e) {
        throw new InfrastructureException(format("Pod creation timeout exceeded. -id: %s -message: %s", deploymentName, e.getMessage()));
    } finally {
        createWatch.close();
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Pod(io.fabric8.kubernetes.api.model.Pod) ExecWatch(io.fabric8.kubernetes.client.dsl.ExecWatch) Watch(io.fabric8.kubernetes.client.Watch) ExecutionException(java.util.concurrent.ExecutionException) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) TimeoutException(java.util.concurrent.TimeoutException)

Example 9 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.

the class KubernetesDeployments method findPod.

private Optional<Pod> findPod(String name) throws InfrastructureException {
    Pod pod = clientFactory.create(workspaceId).pods().inNamespace(namespace).withName(name).get();
    if (pod != null) {
        return Optional.of(pod);
    }
    Deployment deployment = clientFactory.create(workspaceId).apps().deployments().inNamespace(namespace).withName(name).get();
    if (deployment == null) {
        return Optional.empty();
    }
    Map<String, String> selector = deployment.getSpec().getSelector().getMatchLabels();
    List<Pod> pods = clientFactory.create(workspaceId).pods().inNamespace(namespace).withLabels(selector).list().getItems();
    if (pods.isEmpty()) {
        return Optional.empty();
    } else if (pods.size() > 1) {
        throw new InfrastructureException(format("Found multiple pods in Deployment '%s'", name));
    }
    return Optional.of(pods.get(0));
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 10 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.

the class KubernetesNamespace method label.

/**
 * Applies given `ensureLabels` into given `namespace` and update the `namespace` in the
 * Kubernetes.
 *
 * <p>If we do not have permissions to do so (code=403), this method does not throw any exception.
 *
 * @param namespace namespace to label
 * @param ensureLabels these labels should be applied on given `namespace`
 * @throws InfrastructureException if something goes wrong with update, except lack of permissions
 */
protected void label(Namespace namespace, Map<String, String> ensureLabels) throws InfrastructureException {
    if (ensureLabels.isEmpty()) {
        return;
    }
    Map<String, String> currentLabels = namespace.getMetadata().getLabels();
    Map<String, String> newLabels = currentLabels != null ? new HashMap<>(currentLabels) : new HashMap<>();
    if (newLabels.entrySet().containsAll(ensureLabels.entrySet())) {
        LOG.debug("Nothing to do, namespace [{}] already has all required labels.", namespace.getMetadata().getName());
        return;
    }
    try {
        // update the namespace with new labels
        cheSAClientFactory.create().namespaces().createOrReplace(new NamespaceBuilder(namespace).editMetadata().addToLabels(ensureLabels).endMetadata().build());
    } catch (KubernetesClientException kce) {
        if (kce.getCode() == 403) {
            LOG.warn("Can't label the namespace due to lack of permissions. Grant cluster-wide permissions " + "to `get` and `update` the `namespaces` to the `che` service account " + "(Che operator might have already prepared a cluster role called " + "`che-namespace-editor` for this, depending on its configuration). " + "Alternatively, consider disabling the feature by setting " + "`che.infra.kubernetes.namepsace.label` to `false`.");
            return;
        }
        throw new InfrastructureException(kce);
    }
}
Also used : NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)242 InternalInfrastructureException (org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)64 Test (org.testng.annotations.Test)56 KubernetesInfrastructureException (org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException)44 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)42 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)38 CompletableFuture (java.util.concurrent.CompletableFuture)36 ExecutionException (java.util.concurrent.ExecutionException)36 TimeoutException (java.util.concurrent.TimeoutException)32 ServerException (org.eclipse.che.api.core.ServerException)32 Pod (io.fabric8.kubernetes.api.model.Pod)30 Map (java.util.Map)26 ValidationException (org.eclipse.che.api.core.ValidationException)22 Traced (org.eclipse.che.commons.annotation.Traced)20 Container (io.fabric8.kubernetes.api.model.Container)18 List (java.util.List)18 Set (java.util.Set)18 Inject (javax.inject.Inject)18 RuntimeStartInterruptedException (org.eclipse.che.api.workspace.server.spi.RuntimeStartInterruptedException)18 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)18