use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.
the class KeycloakProviderConfigFactoryTest method testRethrowOnInvalidTokenBadRequestException.
@Test
public void testRethrowOnInvalidTokenBadRequestException() throws Exception {
doThrow(new BadRequestException(DtoFactory.newDto(ServiceError.class).withMessage("Invalid token."))).when(keycloakServiceClient).getIdentityProviderToken(anyString());
try {
configBuilder.buildConfig(defaultConfig, A_WORKSPACE_ID);
} catch (InfrastructureException e) {
assertEquals(e.getMessage(), SESSION_EXPIRED_MESSAGE, "The exception message is wrong");
return;
}
fail("Should have thrown an exception with the following message: " + SESSION_EXPIRED_MESSAGE);
}
use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.
the class KeycloakProviderConfigFactoryTest method testRethrowOnUnauthorizedException.
@Test
public void testRethrowOnUnauthorizedException() throws Exception {
doThrow(new UnauthorizedException(DtoFactory.newDto(ServiceError.class).withMessage("Any other message"))).when(keycloakServiceClient).getIdentityProviderToken(anyString());
try {
configBuilder.buildConfig(defaultConfig, A_WORKSPACE_ID);
} catch (InfrastructureException e) {
assertEquals(e.getMessage(), SHOULD_LINK_ERROR_MESSAGE, "The exception message is wrong");
return;
}
fail("Should have thrown an exception with the following message: " + SHOULD_LINK_ERROR_MESSAGE);
}
use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.
the class KubernetesDeployments method createDeployment.
private Pod createDeployment(Deployment deployment, String workspaceId) throws InfrastructureException {
addPullSecretsOfSA(deployment);
final String deploymentName = deployment.getMetadata().getName();
final CompletableFuture<Pod> createFuture = new CompletableFuture<>();
final Watch createWatch = clientFactory.create(workspaceId).pods().inNamespace(namespace).withLabels(Map.of(CHE_WORKSPACE_ID_LABEL, workspaceId, CHE_DEPLOYMENT_NAME_LABEL, deploymentName)).watch(new CreateWatcher(createFuture, workspaceId, deploymentName));
try {
clientFactory.create(workspaceId).apps().deployments().inNamespace(namespace).create(deployment);
return createFuture.get(POD_CREATION_TIMEOUT_MIN, TimeUnit.MINUTES);
} catch (KubernetesClientException e) {
throw new KubernetesInfrastructureException(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new InfrastructureException(format("Interrupted while waiting for Pod creation. -id: %s -message: %s", deploymentName, e.getMessage()));
} catch (ExecutionException e) {
throw new InfrastructureException(format("Error occured while waiting for Pod creation. -id: %s -message: %s", deploymentName, e.getCause().getMessage()));
} catch (TimeoutException e) {
throw new InfrastructureException(format("Pod creation timeout exceeded. -id: %s -message: %s", deploymentName, e.getMessage()));
} finally {
createWatch.close();
}
}
use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.
the class KubernetesDeployments method findPod.
private Optional<Pod> findPod(String name) throws InfrastructureException {
Pod pod = clientFactory.create(workspaceId).pods().inNamespace(namespace).withName(name).get();
if (pod != null) {
return Optional.of(pod);
}
Deployment deployment = clientFactory.create(workspaceId).apps().deployments().inNamespace(namespace).withName(name).get();
if (deployment == null) {
return Optional.empty();
}
Map<String, String> selector = deployment.getSpec().getSelector().getMatchLabels();
List<Pod> pods = clientFactory.create(workspaceId).pods().inNamespace(namespace).withLabels(selector).list().getItems();
if (pods.isEmpty()) {
return Optional.empty();
} else if (pods.size() > 1) {
throw new InfrastructureException(format("Found multiple pods in Deployment '%s'", name));
}
return Optional.of(pods.get(0));
}
use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.
the class KubernetesNamespace method label.
/**
* Applies given `ensureLabels` into given `namespace` and update the `namespace` in the
* Kubernetes.
*
* <p>If we do not have permissions to do so (code=403), this method does not throw any exception.
*
* @param namespace namespace to label
* @param ensureLabels these labels should be applied on given `namespace`
* @throws InfrastructureException if something goes wrong with update, except lack of permissions
*/
protected void label(Namespace namespace, Map<String, String> ensureLabels) throws InfrastructureException {
if (ensureLabels.isEmpty()) {
return;
}
Map<String, String> currentLabels = namespace.getMetadata().getLabels();
Map<String, String> newLabels = currentLabels != null ? new HashMap<>(currentLabels) : new HashMap<>();
if (newLabels.entrySet().containsAll(ensureLabels.entrySet())) {
LOG.debug("Nothing to do, namespace [{}] already has all required labels.", namespace.getMetadata().getName());
return;
}
try {
// update the namespace with new labels
cheSAClientFactory.create().namespaces().createOrReplace(new NamespaceBuilder(namespace).editMetadata().addToLabels(ensureLabels).endMetadata().build());
} catch (KubernetesClientException kce) {
if (kce.getCode() == 403) {
LOG.warn("Can't label the namespace due to lack of permissions. Grant cluster-wide permissions " + "to `get` and `update` the `namespaces` to the `che` service account " + "(Che operator might have already prepared a cluster role called " + "`che-namespace-editor` for this, depending on its configuration). " + "Alternatively, consider disabling the feature by setting " + "`che.infra.kubernetes.namepsace.label` to `false`.");
return;
}
throw new InfrastructureException(kce);
}
}
Aggregations