Search in sources :

Example 26 with KubernetesInfrastructureException

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

the class KubernetesDeploymentsTest method testDeletePodThrowingKubernetesClientExceptionShouldCloseWatch.

@Test
public void testDeletePodThrowingKubernetesClientExceptionShouldCloseWatch() throws Exception {
    final String POD_NAME = "nonExistingPod";
    doReturn(podResource).when(podResource).withPropagationPolicy(eq(BACKGROUND));
    doThrow(KubernetesClientException.class).when(podResource).delete();
    Watch watch = mock(Watch.class);
    doReturn(watch).when(podResource).watch(any());
    try {
        new KubernetesDeployments("", "", clientFactory, executor).doDeletePod(POD_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 27 with KubernetesInfrastructureException

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

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 28 with KubernetesInfrastructureException

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

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 29 with KubernetesInfrastructureException

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

the class KubernetesDeployments method exec.

/**
 * Executes command in specified container.
 *
 * @param name pod name (or name of deployment containing pod) where command will be executed
 * @param containerName container name where command will be executed
 * @param timeoutMin timeout to wait until process will be done
 * @param command command to execute
 * @throws InfrastructureException when specified timeout is reached
 * @throws InfrastructureException when {@link Thread} is interrupted while command executing
 * @throws InfrastructureException when any other exception occurs
 */
public void exec(String name, String containerName, int timeoutMin, String[] command) throws InfrastructureException {
    final String podName = getPodName(name);
    final ExecWatchdog watchdog = new ExecWatchdog();
    try (ExecWatch watch = clientFactory.create(workspaceId).pods().inNamespace(namespace).withName(podName).inContainer(containerName).redirectingError().usingListener(watchdog).exec(encode(command))) {
        try {
            watchdog.wait(timeoutMin, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new InfrastructureException(e.getMessage(), e);
        }
    } catch (KubernetesClientException e) {
        throw new KubernetesInfrastructureException(e);
    }
}
Also used : ExecWatch(io.fabric8.kubernetes.client.dsl.ExecWatch) 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)

Example 30 with KubernetesInfrastructureException

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

the class KubernetesDeployments method doDeleteDeployment.

protected CompletableFuture<Void> doDeleteDeployment(String deploymentName) throws InfrastructureException {
    // Try to get pod name if it exists (it may not, if e.g. workspace config refers to
    // nonexistent service account).
    String podName;
    try {
        podName = getPodName(deploymentName);
    } catch (InfrastructureException e) {
        // Not an error, just means the Deployment has failed to create a pod.
        podName = null;
    }
    Watch toCloseOnException = null;
    try {
        ScalableResource<Deployment> deploymentResource = clientFactory.create(workspaceId).apps().deployments().inNamespace(namespace).withName(deploymentName);
        if (deploymentResource.get() == null) {
            throw new InfrastructureException(format("No deployment found to delete for name %s", deploymentName));
        }
        final CompletableFuture<Void> deleteFuture = new CompletableFuture<>();
        final Watch watch;
        // Deployment we are deleting.
        if (!Strings.isNullOrEmpty(podName)) {
            PodResource<Pod> podResource = clientFactory.create(workspaceId).pods().inNamespace(namespace).withName(podName);
            watch = podResource.watch(new DeleteWatcher<>(deleteFuture));
            toCloseOnException = watch;
        } else {
            watch = deploymentResource.watch(new DeleteWatcher<Deployment>(deleteFuture));
            toCloseOnException = watch;
        }
        Boolean deleteSucceeded = deploymentResource.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 {}", deploymentName, 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 : Pod(io.fabric8.kubernetes.api.model.Pod) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) TimeoutException(java.util.concurrent.TimeoutException) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) ParseException(java.text.ParseException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) WatcherException(io.fabric8.kubernetes.client.WatcherException) ExecutionException(java.util.concurrent.ExecutionException) 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)

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