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