Search in sources :

Example 21 with KubernetesInfrastructureException

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

the class KubernetesDeployments method waitAsync.

/**
 * Returns a future which completes when pod state satisfies the specified predicate.
 *
 * <p>Note that for resource cleanup, the resulting future must be explicitly cancelled when its
 * completion no longer important.
 *
 * @param name name of pod or deployment containing pod that should be watched
 * @param predicate predicate to perform state check
 * @return pod that satisfies the specified predicate
 * @throws InfrastructureException when specified pod or deployment does not exist
 * @throws InfrastructureException when any other exception occurs
 */
public CompletableFuture<Pod> waitAsync(String name, Predicate<Pod> predicate) throws InfrastructureException {
    String podName = getPodName(name);
    CompletableFuture<Pod> future = new CompletableFuture<>();
    try {
        PodResource<Pod> podResource = clientFactory.create(workspaceId).pods().inNamespace(namespace).withName(podName);
        Watch watch = podResource.watch(new Watcher<>() {

            @Override
            public void eventReceived(Action action, Pod pod) {
                if (predicate.test(pod)) {
                    future.complete(pod);
                }
            }

            @Override
            public void onClose(WatcherException cause) {
                future.completeExceptionally(new InfrastructureException("Waiting for pod '" + podName + "' was interrupted"));
            }
        });
        Pod actualPod = podResource.get();
        if (actualPod == null) {
            if (name.equals(podName)) {
                // `name` refers to a bare pod
                throw new InfrastructureException("Specified pod " + podName + " doesn't exist");
            } else {
                // `name` refers to a deployment
                throw new InfrastructureException("No pod in deployment " + name + " found.");
            }
        }
        if (predicate.test(actualPod)) {
            return CompletableFuture.completedFuture(actualPod);
        }
        future.whenComplete((ok, ex) -> watch.close());
        return future;
    } catch (KubernetesClientException e) {
        throw new KubernetesInfrastructureException(e);
    }
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) WatcherException(io.fabric8.kubernetes.client.WatcherException) CompletableFuture(java.util.concurrent.CompletableFuture) ExecWatch(io.fabric8.kubernetes.client.dsl.ExecWatch) Watch(io.fabric8.kubernetes.client.Watch) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 22 with KubernetesInfrastructureException

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

the class KubernetesServices method create.

/**
 * Creates specified service.
 *
 * @param service service to create
 * @return created service
 * @throws InfrastructureException when any exception occurs
 */
public Service create(Service service) throws InfrastructureException {
    putLabel(service, CHE_WORKSPACE_ID_LABEL, workspaceId);
    putSelector(service, CHE_WORKSPACE_ID_LABEL, workspaceId);
    try {
        return clientFactory.create(workspaceId).services().inNamespace(namespace).create(service);
    } catch (KubernetesClientException e) {
        throw new KubernetesInfrastructureException(e);
    }
}
Also used : KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 23 with KubernetesInfrastructureException

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

the class AsyncStoragePodInterceptor method deleteAsyncStoragePod.

private CompletableFuture<Void> deleteAsyncStoragePod(PodResource<Pod> podResource) throws InfrastructureException {
    Watch toCloseOnException = null;
    try {
        final CompletableFuture<Void> deleteFuture = new CompletableFuture<>();
        final Watch watch = podResource.watch(new DeleteWatcher<>(deleteFuture));
        toCloseOnException = watch;
        Boolean deleteSucceeded = podResource.withPropagationPolicy(BACKGROUND).delete();
        if (deleteSucceeded == null || !deleteSucceeded) {
            deleteFuture.complete(null);
        }
        return deleteFuture.whenComplete((v, e) -> {
            if (e != null) {
                LOG.warn("Failed to remove pod {} 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)

Example 24 with KubernetesInfrastructureException

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

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)

Example 25 with KubernetesInfrastructureException

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

the class OpenShiftProject method delete.

/**
 * Deletes the project. Deleting a non-existent projects is not an error as is not an attempt to
 * delete a project that is already being deleted.
 *
 * @throws InfrastructureException if any unexpected exception occurs during project deletion
 */
void delete() throws InfrastructureException {
    String workspaceId = getWorkspaceId();
    String projectName = getName();
    OpenShiftClient osClient = clientFactory.createOC(workspaceId);
    try {
        delete(projectName, osClient);
    } catch (KubernetesClientException e) {
        if (e.getCode() == 403) {
            throw new InfrastructureException(format("Could not access the project %s when deleting it for workspace %s", projectName, workspaceId), e);
        }
        throw new KubernetesInfrastructureException(e);
    }
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) 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

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