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;
}
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);
}
}
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);
}
}
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;
}
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);
}
}
Aggregations