Search in sources :

Example 96 with Action

use of io.fabric8.kubernetes.client.Watcher.Action in project che-server by eclipse-che.

the class KubernetesDeployments method waitRunningAsync.

/**
 * Subscribes to pod events and returns the resulting future, which completes when pod becomes
 * running.
 *
 * <p>Note that the resulting future must be explicitly cancelled when its completion no longer
 * important because of finalization allocated resources.
 *
 * @param name the pod or deployment (that contains pod) name that should be watched
 * @return completable future that is completed when one of the following conditions is met:
 *     <ul>
 *       <li>complete successfully in case of "Running" pod state.
 *       <li>complete exceptionally in case of "Failed" pod state. Exception will contain pod
 *           status reason value, or if absent, it will attempt to retrieve pod logs.
 *       <li>complete exceptionally in case of "Succeeded" pod state. (workspace container has
 *           been terminated).
 *       <li>complete exceptionally when exception while getting pod resource occurred.
 *       <li>complete exceptionally when connection problem occurred.
 *     </ul>
 *     otherwise, it must be explicitly closed
 */
public CompletableFuture<Void> waitRunningAsync(String name) {
    final CompletableFuture<Void> podRunningFuture = new CompletableFuture<>();
    try {
        final String podName = getPodName(name);
        final PodResource<Pod> podResource = clientFactory.create(workspaceId).pods().inNamespace(namespace).withName(podName);
        final Watch watch = podResource.watch(new Watcher<>() {

            @Override
            public void eventReceived(Action action, Pod pod) {
                handleStartingPodStatus(podRunningFuture, pod);
            }

            @Override
            public void onClose(WatcherException cause) {
                podRunningFuture.completeExceptionally(new InfrastructureException("Waiting for pod '" + podName + "' was interrupted"));
            }
        });
        podRunningFuture.whenComplete((ok, ex) -> watch.close());
        final Pod pod = podResource.get();
        if (pod == null) {
            InfrastructureException ex;
            if (name.equals(podName)) {
                // `name` refers to bare pod
                ex = new InfrastructureException("Specified pod " + podName + " doesn't exist");
            } else {
                ex = new InfrastructureException("No pod in deployment " + name + " found.");
            }
            podRunningFuture.completeExceptionally(ex);
        } else {
            handleStartingPodStatus(podRunningFuture, pod);
        }
    } catch (KubernetesClientException | InfrastructureException ex) {
        podRunningFuture.completeExceptionally(ex);
    }
    return podRunningFuture;
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) 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 97 with Action

use of io.fabric8.kubernetes.client.Watcher.Action 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 98 with Action

use of io.fabric8.kubernetes.client.Watcher.Action 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 99 with Action

use of io.fabric8.kubernetes.client.Watcher.Action in project devspaces-images by redhat-developer.

the class KubernetesDeployments method waitRunningAsync.

/**
 * Subscribes to pod events and returns the resulting future, which completes when pod becomes
 * running.
 *
 * <p>Note that the resulting future must be explicitly cancelled when its completion no longer
 * important because of finalization allocated resources.
 *
 * @param name the pod or deployment (that contains pod) name that should be watched
 * @return completable future that is completed when one of the following conditions is met:
 *     <ul>
 *       <li>complete successfully in case of "Running" pod state.
 *       <li>complete exceptionally in case of "Failed" pod state. Exception will contain pod
 *           status reason value, or if absent, it will attempt to retrieve pod logs.
 *       <li>complete exceptionally in case of "Succeeded" pod state. (workspace container has
 *           been terminated).
 *       <li>complete exceptionally when exception while getting pod resource occurred.
 *       <li>complete exceptionally when connection problem occurred.
 *     </ul>
 *     otherwise, it must be explicitly closed
 */
public CompletableFuture<Void> waitRunningAsync(String name) {
    final CompletableFuture<Void> podRunningFuture = new CompletableFuture<>();
    try {
        final String podName = getPodName(name);
        final PodResource<Pod> podResource = clientFactory.create(workspaceId).pods().inNamespace(namespace).withName(podName);
        final Watch watch = podResource.watch(new Watcher<>() {

            @Override
            public void eventReceived(Action action, Pod pod) {
                handleStartingPodStatus(podRunningFuture, pod);
            }

            @Override
            public void onClose(WatcherException cause) {
                podRunningFuture.completeExceptionally(new InfrastructureException("Waiting for pod '" + podName + "' was interrupted"));
            }
        });
        podRunningFuture.whenComplete((ok, ex) -> watch.close());
        final Pod pod = podResource.get();
        if (pod == null) {
            InfrastructureException ex;
            if (name.equals(podName)) {
                // `name` refers to bare pod
                ex = new InfrastructureException("Specified pod " + podName + " doesn't exist");
            } else {
                ex = new InfrastructureException("No pod in deployment " + name + " found.");
            }
            podRunningFuture.completeExceptionally(ex);
        } else {
            handleStartingPodStatus(podRunningFuture, pod);
        }
    } catch (KubernetesClientException | InfrastructureException ex) {
        podRunningFuture.completeExceptionally(ex);
    }
    return podRunningFuture;
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) 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 100 with Action

use of io.fabric8.kubernetes.client.Watcher.Action in project dubbo-spi-extensions by apache.

the class KubernetesMeshEnvListener method subscribeDr.

private void subscribeDr(String appName) {
    if (drAppWatch.containsKey(appName)) {
        return;
    }
    try {
        Watch watch = kubernetesClient.customResource(MeshConstant.getDrDefinition()).watch(namespace, appName, null, new ListOptionsBuilder().build(), new Watcher<String>() {

            @Override
            public void eventReceived(Action action, String resource) {
                logger.info("Received VS Rule notification. AppName: " + appName + " Action:" + action + " Resource:" + resource);
                if (action == Action.ADDED || action == Action.MODIFIED) {
                    Map drRuleMap = new Gson().fromJson(resource, Map.class);
                    String drRule = new Yaml(new SafeConstructor()).dump(drRuleMap);
                    drAppCache.put(appName, drRule);
                    if (vsAppCache.containsKey(appName)) {
                        notifyListener(vsAppCache.get(appName), appName, drRule);
                    }
                } else {
                    appRuleListenerMap.get(appName).receiveConfigInfo("");
                }
            }

            @Override
            public void onClose(WatcherException cause) {
            // ignore
            }
        });
        drAppWatch.put(appName, watch);
        try {
            Map<String, Object> drRule = kubernetesClient.customResource(MeshConstant.getDrDefinition()).get(namespace, appName);
            drAppCache.put(appName, new Yaml(new SafeConstructor()).dump(drRule));
        } catch (Throwable ignore) {
        }
    } catch (IOException e) {
        logger.error("Error occurred when listen kubernetes crd.", e);
    }
}
Also used : Gson(com.google.gson.Gson) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml) WatcherException(io.fabric8.kubernetes.client.WatcherException) Watch(io.fabric8.kubernetes.client.Watch) SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor) ListOptionsBuilder(io.fabric8.kubernetes.api.model.ListOptionsBuilder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Aggregations

WatcherException (io.fabric8.kubernetes.client.WatcherException)63 Watch (io.fabric8.kubernetes.client.Watch)48 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)42 Watcher (io.fabric8.kubernetes.client.Watcher)35 Pod (io.fabric8.kubernetes.api.model.Pod)34 CountDownLatch (java.util.concurrent.CountDownLatch)27 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)23 Test (org.junit.jupiter.api.Test)22 Map (java.util.Map)15 List (java.util.List)14 ArrayList (java.util.ArrayList)13 DisplayName (org.junit.jupiter.api.DisplayName)12 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)11 Future (io.vertx.core.Future)11 ReconciliationLogger (io.strimzi.operator.common.ReconciliationLogger)10 GenericKubernetesResource (io.fabric8.kubernetes.api.model.GenericKubernetesResource)9 WatchEvent (io.fabric8.kubernetes.api.model.WatchEvent)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 Service (io.fabric8.kubernetes.api.model.Service)8 Annotations (io.strimzi.operator.common.Annotations)8