use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace in project devspaces-images by redhat-developer.
the class PVCSubPathHelper method execute.
private void execute(RuntimeIdentity identity, String workspaceId, String namespace, String pvcName, String[] commandBase, Map<String, String> startOptions, boolean watchFailureEvents, String... arguments) {
final String jobName = commandBase[0];
final String podName = jobName + '-' + workspaceId;
final String[] command = buildCommand(commandBase, arguments);
final Pod pod = newPod(podName, pvcName, command);
securityContextProvisioner.provision(pod.getSpec());
nodeSelectorProvisioner.provision(pod.getSpec());
tolerationsProvisioner.provision(pod.getSpec());
KubernetesDeployments deployments = null;
try {
KubernetesNamespace ns = factory.access(workspaceId, namespace);
if (!checkPVCExistsAndNotTerminating(ns, pvcName)) {
return;
}
deployments = ns.deployments();
deployments.create(pod);
watchLogsIfDebugEnabled(deployments, pod, identity, startOptions);
PodStatus finishedStatus = waitPodStatus(podName, deployments, watchFailureEvents);
if (POD_PHASE_FAILED.equals(finishedStatus.getPhase())) {
String logs = deployments.getPodLogs(podName);
LOG.error("Job command '{}' execution is failed. Logs: {}", Arrays.toString(command), // Force logs onto one line
Strings.nullToEmpty(logs).replace("\n", " \\n"));
}
} catch (InfrastructureException ex) {
LOG.error("Unable to perform '{}' command for the workspace '{}' cause: '{}'", Arrays.toString(command), workspaceId, ex.getMessage());
deployments.stopWatch(true);
} finally {
if (deployments != null) {
deployments.stopWatch();
try {
deployments.delete(podName);
} catch (InfrastructureException ignored) {
}
}
}
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace in project devspaces-images by redhat-developer.
the class PluginBrokerManager method getTooling.
/**
* Deploys Che plugin brokers in a workspace, receives result of theirs execution and returns
* resolved workspace tooling or error of plugins brokering execution.
*
* <p>This API is in <b>Beta</b> and is subject to changes or removal.
*/
@Beta
@Traced
public List<ChePlugin> getTooling(RuntimeIdentity identity, StartSynchronizer startSynchronizer, Collection<PluginFQN> pluginFQNs, boolean isEphemeral, boolean mergePlugins, Map<String, String> startOptions) throws InfrastructureException {
String workspaceId = identity.getWorkspaceId();
KubernetesNamespace kubernetesNamespace = factory.getOrCreate(identity);
BrokersResult brokersResult = new BrokersResult();
E brokerEnvironment = brokerEnvironmentFactory.createForMetadataBroker(pluginFQNs, identity, mergePlugins);
if (isEphemeral) {
EphemeralWorkspaceUtility.makeEphemeral(brokerEnvironment.getAttributes());
}
environmentProvisioner.provision(brokerEnvironment, identity);
ListenBrokerEvents listenBrokerEvents = getListenEventPhase(workspaceId, brokersResult);
PrepareStorage prepareStorage = getPrepareStoragePhase(identity, startSynchronizer, brokerEnvironment, startOptions);
WaitBrokerResult waitBrokerResult = getWaitBrokerPhase(workspaceId, brokersResult);
DeployBroker deployBroker = getDeployBrokerPhase(identity, kubernetesNamespace, brokerEnvironment, brokersResult, startOptions);
LOG.debug("Entering plugin brokers deployment chain workspace '{}'", workspaceId);
listenBrokerEvents.then(prepareStorage).then(deployBroker).then(waitBrokerResult);
return listenBrokerEvents.execute();
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace in project che-server by eclipse-che.
the class CommonPVCStrategy method prepare.
@Override
@Traced
public void prepare(KubernetesEnvironment k8sEnv, RuntimeIdentity identity, long timeoutMillis, Map<String, String> startOptions) throws InfrastructureException {
String workspaceId = identity.getWorkspaceId();
TracingTags.WORKSPACE_ID.set(workspaceId);
if (EphemeralWorkspaceUtility.isEphemeral(k8sEnv.getAttributes())) {
return;
}
log.debug("Preparing PVC started for workspace '{}'", workspaceId);
Map<String, PersistentVolumeClaim> claims = k8sEnv.getPersistentVolumeClaims();
if (claims.isEmpty()) {
return;
}
if (claims.size() > 1) {
throw new InfrastructureException(format("The only one PVC MUST be present in common strategy while it contains: %s.", claims.keySet().stream().collect(joining(", "))));
}
PersistentVolumeClaim commonPVC = claims.values().iterator().next();
final KubernetesNamespace namespace = factory.getOrCreate(identity);
final KubernetesPersistentVolumeClaims pvcs = namespace.persistentVolumeClaims();
final Set<String> existing = pvcs.get().stream().map(p -> p.getMetadata().getName()).collect(toSet());
if (!existing.contains(commonPVC.getMetadata().getName())) {
log.debug("Creating PVC for workspace '{}'", workspaceId);
pvcs.create(commonPVC);
if (waitBound) {
log.debug("Waiting for PVC for workspace '{}' to be bound", workspaceId);
pvcs.waitBound(commonPVC.getMetadata().getName(), timeoutMillis);
}
}
final String[] subpaths = (String[]) commonPVC.getAdditionalProperties().remove(format(SUBPATHS_PROPERTY_FMT, workspaceId));
if (preCreateDirs && subpaths != null) {
pvcSubPathHelper.createDirs(identity, workspaceId, commonPVC.getMetadata().getName(), startOptions, subpaths);
}
log.debug("Preparing PVC done for workspace '{}'", workspaceId);
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace in project che-server by eclipse-che.
the class PreviewUrlCommandProvisioner method injectsPreviewUrlToCommands.
/**
* Go through all commands, find matching service and exposed host. Then construct full preview
* url from this data and set it as Command's parameter under `previewUrl` key.
*
* @param env environment to get commands
* @param namespace current kubernetes namespace where we're looking for services and ingresses
*/
private void injectsPreviewUrlToCommands(E env, KubernetesNamespace namespace) throws InfrastructureException {
if (env.getCommands() == null) {
return;
}
List<T> exposureObjects = loadExposureObjects(namespace);
List<Service> services = namespace.services().get();
for (CommandImpl command : env.getCommands().stream().filter(c -> c.getPreviewUrl() != null).collect(Collectors.toList())) {
Optional<Service> foundService = Services.findServiceWithPort(services, command.getPreviewUrl().getPort());
if (!foundService.isPresent()) {
String message = String.format("unable to find service for port '%s' for command '%s'", command.getPreviewUrl().getPort(), command.getName());
LOG.warn(message);
env.addWarning(new WarningImpl(NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL, String.format(NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL_MESSAGE, message)));
continue;
}
Optional<String> foundHost = findHostForServicePort(exposureObjects, foundService.get(), command.getPreviewUrl().getPort());
if (foundHost.isPresent()) {
command.getAttributes().put(PREVIEW_URL_ATTRIBUTE, foundHost.get());
} else {
String message = String.format("unable to find ingress for service '%s' and port '%s'", foundService.get(), command.getPreviewUrl().getPort());
LOG.warn(message);
env.addWarning(new WarningImpl(NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL, String.format(NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL_MESSAGE, message)));
}
}
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace in project che-server by eclipse-che.
the class OpenShiftPreviewUrlCommandProvisionerTest method throwsInfrastructureExceptionWhenK8sNamespaces.
@Test(expectedExceptions = InternalInfrastructureException.class)
public void throwsInfrastructureExceptionWhenK8sNamespaces() throws InfrastructureException {
KubernetesNamespace namespace = Mockito.mock(KubernetesNamespace.class);
previewUrlCommandProvisioner.provision(mockEnvironment, namespace);
}
Aggregations