Search in sources :

Example 16 with KubernetesInfrastructureException

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

the class KubernetesIngresses method wait.

public Ingress wait(String name, long timeout, TimeUnit timeoutUnit, Predicate<Ingress> predicate) throws InfrastructureException {
    CompletableFuture<Ingress> future = new CompletableFuture<>();
    Watch watch = null;
    try {
        Resource<Ingress> ingressResource = clientFactory.create(workspaceId).network().v1().ingresses().inNamespace(namespace).withName(name);
        watch = ingressResource.watch(new Watcher<>() {

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

            @Override
            public void onClose(WatcherException cause) {
                future.completeExceptionally(new InfrastructureException("Waiting for ingress '" + name + "' was interrupted"));
            }
        });
        Ingress actualIngress = ingressResource.get();
        if (actualIngress == null) {
            throw new InfrastructureException("Specified ingress " + name + " doesn't exist");
        }
        if (predicate.test(actualIngress)) {
            return actualIngress;
        }
        try {
            return future.get(timeout, timeoutUnit);
        } catch (ExecutionException e) {
            throw new InfrastructureException(e.getCause().getMessage(), e);
        } catch (TimeoutException e) {
            throw new InfrastructureException("Waiting for ingress '" + name + "' reached timeout");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new InfrastructureException("Waiting for ingress '" + name + "' was interrupted");
        }
    } catch (KubernetesClientException e) {
        throw new KubernetesInfrastructureException(e);
    } finally {
        if (watch != null) {
            watch.close();
        }
    }
}
Also used : Ingress(io.fabric8.kubernetes.api.model.networking.v1.Ingress) Watcher(io.fabric8.kubernetes.client.Watcher) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) WatcherException(io.fabric8.kubernetes.client.WatcherException) CompletableFuture(java.util.concurrent.CompletableFuture) Watch(io.fabric8.kubernetes.client.Watch) ExecutionException(java.util.concurrent.ExecutionException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) TimeoutException(java.util.concurrent.TimeoutException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 17 with KubernetesInfrastructureException

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

the class KubernetesNamespace method waitDefaultServiceAccount.

/**
 * Waits few seconds until 'default' service account become available otherwise an infrastructure
 * exception will be thrown.
 */
protected void waitDefaultServiceAccount(String namespaceName, KubernetesClient client) throws InfrastructureException {
    final Predicate<ServiceAccount> predicate = Objects::nonNull;
    final CompletableFuture<ServiceAccount> future = new CompletableFuture<>();
    Watch watch = null;
    try {
        final Resource<ServiceAccount> saResource = client.serviceAccounts().inNamespace(namespaceName).withName(DEFAULT_SERVICE_ACCOUNT_NAME);
        watch = saResource.watch(new Watcher<>() {

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

            @Override
            public void onClose(WatcherException cause) {
                future.completeExceptionally(new InfrastructureException("Waiting for service account '" + DEFAULT_SERVICE_ACCOUNT_NAME + "' was interrupted"));
            }
        });
        if (predicate.test(saResource.get())) {
            return;
        }
        try {
            future.get(SERVICE_ACCOUNT_READINESS_TIMEOUT_SEC, TimeUnit.SECONDS);
        } catch (ExecutionException ex) {
            throw new InfrastructureException(ex.getCause().getMessage(), ex);
        } catch (TimeoutException ex) {
            throw new InfrastructureException("Waiting for service account '" + DEFAULT_SERVICE_ACCOUNT_NAME + "' reached timeout");
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw new InfrastructureException("Waiting for service account '" + DEFAULT_SERVICE_ACCOUNT_NAME + "' was interrupted");
        }
    } catch (KubernetesClientException ex) {
        throw new KubernetesInfrastructureException(ex);
    } finally {
        if (watch != null) {
            watch.close();
        }
    }
}
Also used : ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) Watcher(io.fabric8.kubernetes.client.Watcher) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) WatcherException(io.fabric8.kubernetes.client.WatcherException) CompletableFuture(java.util.concurrent.CompletableFuture) Watch(io.fabric8.kubernetes.client.Watch) 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) TimeoutException(java.util.concurrent.TimeoutException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 18 with KubernetesInfrastructureException

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

the class KubernetesDeployments method doDeletePod.

protected CompletableFuture<Void> doDeletePod(String podName) throws InfrastructureException {
    Watch toCloseOnException = null;
    try {
        PodResource<Pod> podResource = clientFactory.create(workspaceId).pods().inNamespace(namespace).withName(podName);
        if (podResource.get() == null) {
            throw new InfrastructureException(format("No pod found to delete for name %s", podName));
        }
        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 {}", podName, 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) Pod(io.fabric8.kubernetes.api.model.Pod) ExecWatch(io.fabric8.kubernetes.client.dsl.ExecWatch) Watch(io.fabric8.kubernetes.client.Watch) 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) 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) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 19 with KubernetesInfrastructureException

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

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
 * @param outputConsumer command output biconsumer, that is accepts stream type and message
 * @throws InfrastructureException when specified timeout is reached
 * @throws InfrastructureException when {@link Thread} is interrupted while command executing
 * @throws InfrastructureException when command error stream is not empty
 * @throws InfrastructureException when any other exception occurs
 */
public void exec(String name, String containerName, int timeoutMin, String[] command, BiConsumer<String, String> outputConsumer) throws InfrastructureException {
    final String podName = getPodName(name);
    final ExecWatchdog watchdog = new ExecWatchdog();
    final ByteArrayOutputStream errStream = new ByteArrayOutputStream(ERROR_BUFF_INITIAL_CAP);
    try (ExecWatch watch = clientFactory.create(workspaceId).pods().inNamespace(namespace).withName(podName).inContainer(containerName).writingError(errStream).usingListener(watchdog).exec(encode(command))) {
        try {
            watchdog.wait(timeoutMin, TimeUnit.MINUTES);
            final byte[] error = errStream.toByteArray();
            if (error.length > 0) {
                final String cmd = Arrays.stream(command).collect(Collectors.joining(" ", "", "\n"));
                final String err = new String(error, UTF_8);
                outputConsumer.accept(STDOUT, cmd);
                outputConsumer.accept(STDERR, err);
                throw new InfrastructureException(err);
            }
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw new InfrastructureException(ex.getMessage(), ex);
        }
    } catch (KubernetesClientException ex) {
        throw new KubernetesInfrastructureException(ex);
    }
}
Also used : ExecWatch(io.fabric8.kubernetes.client.dsl.ExecWatch) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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 20 with KubernetesInfrastructureException

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

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