Search in sources :

Example 1 with KubernetesInfrastructureException

use of org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException 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 2 with KubernetesInfrastructureException

use of org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException in project che-server by eclipse-che.

the class KubernetesNamespace method create.

private Namespace create(String namespaceName, KubernetesClient client) throws InfrastructureException {
    try {
        Namespace namespace = client.namespaces().create(new NamespaceBuilder().withNewMetadata().withName(namespaceName).endMetadata().build());
        waitDefaultServiceAccount(namespaceName, client);
        return namespace;
    } catch (KubernetesClientException e) {
        if (e.getCode() == 403) {
            LOG.error("Unable to create new Kubernetes project due to lack of permissions." + "When using workspace namespace placeholders, service account with lenient permissions (cluster-admin) must be used.");
        }
        throw new KubernetesInfrastructureException(e);
    }
}
Also used : KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) Namespace(io.fabric8.kubernetes.api.model.Namespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 3 with KubernetesInfrastructureException

use of org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException in project che-server by eclipse-che.

the class KubernetesPersistentVolumeClaims method waitBound.

/**
 * Waits until persistent volume claim state is bound. If used k8s Storage Class has
 * 'volumeBindingMode: WaitForFirstConsumer', we don't wait to avoid deadlock.
 *
 * @param name name of persistent volume claim that should be watched
 * @param timeoutMillis waiting timeout in milliseconds
 * @return persistent volume claim that is bound or in waiting for consumer state
 * @throws InfrastructureException when specified timeout is reached
 * @throws InfrastructureException when {@link Thread} is interrupted while waiting
 * @throws InfrastructureException when any other exception occurs
 */
public PersistentVolumeClaim waitBound(String name, long timeoutMillis) throws InfrastructureException {
    try {
        Resource<PersistentVolumeClaim> pvcResource = clientFactory.create(workspaceId).persistentVolumeClaims().inNamespace(namespace).withName(name);
        PersistentVolumeClaim actualPvc = pvcResource.get();
        if (actualPvc.getStatus().getPhase().equals(PVC_BOUND_PHASE)) {
            return actualPvc;
        }
        CompletableFuture<PersistentVolumeClaim> future = new CompletableFuture<>();
        // any of these watchers can finish the operation resolving the future
        try (Watch boundWatcher = pvcIsBoundWatcher(future, pvcResource);
            Watch waitingWatcher = pvcIsWaitingForConsumerWatcher(future, actualPvc)) {
            return future.get(timeoutMillis, TimeUnit.MILLISECONDS);
        } catch (ExecutionException e) {
            // should take a look.
            throw new InternalInfrastructureException(e.getCause().getMessage(), e);
        } catch (TimeoutException e) {
            // issues that admin should take a look.
            throw new InternalInfrastructureException("Waiting for persistent volume claim '" + name + "' reached timeout");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new InfrastructureException("Waiting for persistent volume claim '" + name + "' was interrupted");
        }
    } catch (KubernetesClientException e) {
        throw new KubernetesInfrastructureException(e);
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Watch(io.fabric8.kubernetes.client.Watch) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) ExecutionException(java.util.concurrent.ExecutionException) 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) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) TimeoutException(java.util.concurrent.TimeoutException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 4 with KubernetesInfrastructureException

use of org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException in project che-server by eclipse-che.

the class KubernetesDeploymentsTest method testDeleteDeploymentThrowingKubernetesClientExceptionShouldCloseWatch.

@Test
public void testDeleteDeploymentThrowingKubernetesClientExceptionShouldCloseWatch() throws Exception {
    final String DEPLOYMENT_NAME = "nonExistingPod";
    doThrow(KubernetesClientException.class).when(deploymentResource).delete();
    doReturn(deploymentResource).when(deploymentResource).withPropagationPolicy(eq(BACKGROUND));
    Watch watch = mock(Watch.class);
    doReturn(watch).when(podResource).watch(any());
    try {
        new KubernetesDeployments("", "", clientFactory, executor).doDeleteDeployment(DEPLOYMENT_NAME).get(5, TimeUnit.SECONDS);
    } catch (KubernetesInfrastructureException e) {
        assertTrue(e.getCause() instanceof KubernetesClientException);
        verify(watch).close();
        return;
    }
    fail("The exception should have been rethrown");
}
Also used : Watch(io.fabric8.kubernetes.client.Watch) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Test(org.testng.annotations.Test)

Example 5 with KubernetesInfrastructureException

use of org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException in project devspaces-images by redhat-developer.

the class AsyncStoragePodInterceptor method deleteAsyncStorageDeployment.

private CompletableFuture<Void> deleteAsyncStorageDeployment(RollableScalableResource<Deployment> resource) throws InfrastructureException {
    Watch toCloseOnException = null;
    try {
        final CompletableFuture<Void> deleteFuture = new CompletableFuture<>();
        final Watch watch = resource.watch(new DeleteWatcher<>(deleteFuture));
        toCloseOnException = watch;
        Boolean deleteSucceeded = resource.withPropagationPolicy(BACKGROUND).delete();
        if (deleteSucceeded == null || !deleteSucceeded) {
            deleteFuture.complete(null);
        }
        return deleteFuture.whenComplete((v, e) -> {
            if (e != null) {
                LOG.warn("Failed to remove deployment {} cause {}", ASYNC_STORAGE, e.getMessage());
            }
            watch.close();
        });
    } catch (KubernetesClientException e) {
        if (toCloseOnException != null) {
            toCloseOnException.close();
        }
        throw new KubernetesInfrastructureException(e);
    } catch (Exception e) {
        if (toCloseOnException != null) {
            toCloseOnException.close();
        }
        throw e;
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Watch(io.fabric8.kubernetes.client.Watch) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) TimeoutException(java.util.concurrent.TimeoutException) WatcherException(io.fabric8.kubernetes.client.WatcherException) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) ExecutionException(java.util.concurrent.ExecutionException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)36 KubernetesInfrastructureException (org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException)36 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)26 Watch (io.fabric8.kubernetes.client.Watch)22 CompletableFuture (java.util.concurrent.CompletableFuture)20 ExecutionException (java.util.concurrent.ExecutionException)18 TimeoutException (java.util.concurrent.TimeoutException)18 WatcherException (io.fabric8.kubernetes.client.WatcherException)16 ExecWatch (io.fabric8.kubernetes.client.dsl.ExecWatch)12 Pod (io.fabric8.kubernetes.api.model.Pod)10 Watcher (io.fabric8.kubernetes.client.Watcher)6 ParseException (java.text.ParseException)6 InternalInfrastructureException (org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)6 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 Test (org.testng.annotations.Test)4 Event (io.fabric8.kubernetes.api.model.Event)2 LocalObjectReference (io.fabric8.kubernetes.api.model.LocalObjectReference)2 Namespace (io.fabric8.kubernetes.api.model.Namespace)2