Search in sources :

Example 76 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project devspaces-images by redhat-developer.

the class KubernetesPluginsToolingApplier method apply.

@Override
public void apply(RuntimeIdentity runtimeIdentity, InternalEnvironment internalEnvironment, Collection<ChePlugin> chePlugins) throws InfrastructureException {
    if (chePlugins.isEmpty()) {
        return;
    }
    KubernetesEnvironment k8sEnv = (KubernetesEnvironment) internalEnvironment;
    Map<String, PodData> pods = k8sEnv.getPodsData();
    switch(pods.size()) {
        case 0:
            addToolingPod(k8sEnv);
            pods = k8sEnv.getPodsData();
            break;
        case 1:
            break;
        default:
            throw new InfrastructureException("Che plugins tooling configuration can be applied to a workspace with one pod only");
    }
    PodData pod = pods.values().iterator().next();
    CommandsResolver commandsResolver = new CommandsResolver(k8sEnv);
    for (ChePlugin chePlugin : chePlugins) {
        Map<String, ComponentImpl> devfilePlugins = k8sEnv.getDevfile().getComponents().stream().filter(c -> c.getType().equals("cheEditor") || c.getType().equals("chePlugin")).collect(Collectors.toMap(ComponentImpl::getId, Function.identity()));
        if (!devfilePlugins.containsKey(chePlugin.getId())) {
            throw new InfrastructureException(String.format("The downloaded plugin '%s' configuration does not have the " + "corresponding component in devfile. Devfile contains the following cheEditor/chePlugins: %s", chePlugin.getId(), devfilePlugins.keySet()));
        }
        ComponentImpl pluginRelatedComponent = devfilePlugins.get(chePlugin.getId());
        for (CheContainer container : chePlugin.getInitContainers()) {
            Container k8sInitContainer = toK8sContainer(container);
            envVars.apply(k8sInitContainer, pluginRelatedComponent.getEnv());
            chePluginsVolumeApplier.applyVolumes(pod, k8sInitContainer, container.getVolumes(), k8sEnv);
            pod.getSpec().getInitContainers().add(k8sInitContainer);
        }
        Collection<CommandImpl> pluginRelatedCommands = commandsResolver.resolve(chePlugin);
        for (CheContainer container : chePlugin.getContainers()) {
            addSidecar(pod, container, chePlugin, k8sEnv, pluginRelatedCommands, pluginRelatedComponent, runtimeIdentity);
        }
    }
    chePlugins.forEach(chePlugin -> populateWorkspaceEnvVars(chePlugin, k8sEnv));
}
Also used : PLUGIN_MACHINE_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.PLUGIN_MACHINE_ATTRIBUTE) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) ChePluginEndpoint(org.eclipse.che.api.workspace.server.wsplugins.model.ChePluginEndpoint) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) EnvVars(org.eclipse.che.workspace.infrastructure.kubernetes.util.EnvVars) Singleton(javax.inject.Singleton) CONTAINER_SOURCE_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.CONTAINER_SOURCE_ATTRIBUTE) Function(java.util.function.Function) WORKING_DIRECTORY_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.WORKING_DIRECTORY_ATTRIBUTE) Inject(javax.inject.Inject) Names(org.eclipse.che.workspace.infrastructure.kubernetes.Names) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) Map(java.util.Map) MACHINE_NAME_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.MACHINE_NAME_ATTRIBUTE) Service(io.fabric8.kubernetes.api.model.Service) Named(javax.inject.Named) CheContainer(org.eclipse.che.api.workspace.server.wsplugins.model.CheContainer) Command(org.eclipse.che.api.workspace.server.wsplugins.model.Command) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) ChePluginsApplier(org.eclipse.che.api.workspace.server.wsplugins.ChePluginsApplier) TOOL_CONTAINER_SOURCE(org.eclipse.che.api.workspace.shared.Constants.TOOL_CONTAINER_SOURCE) WarningImpl(org.eclipse.che.api.workspace.server.model.impl.WarningImpl) Component(org.eclipse.che.api.core.model.workspace.devfile.Component) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Pod(io.fabric8.kubernetes.api.model.Pod) Set(java.util.Set) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Sets(com.google.common.collect.Sets) Beta(com.google.common.annotations.Beta) ChePlugin(org.eclipse.che.api.workspace.server.wsplugins.model.ChePlugin) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) Warnings(org.eclipse.che.workspace.infrastructure.kubernetes.Warnings) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) ProjectsRootEnvVariableProvider(org.eclipse.che.api.workspace.server.spi.provision.env.ProjectsRootEnvVariableProvider) KubernetesSize(org.eclipse.che.workspace.infrastructure.kubernetes.util.KubernetesSize) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) CheContainer(org.eclipse.che.api.workspace.server.wsplugins.model.CheContainer) Container(io.fabric8.kubernetes.api.model.Container) CheContainer(org.eclipse.che.api.workspace.server.wsplugins.model.CheContainer) ChePlugin(org.eclipse.che.api.workspace.server.wsplugins.model.ChePlugin) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 77 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project devspaces-images by redhat-developer.

the class BrokerService method handle.

private void handle(BrokerStatusChangedEvent event) {
    String encodedTooling = event.getTooling();
    RuntimeIdentity runtimeId = event.getRuntimeId();
    if (event.getStatus() == null || runtimeId == null || runtimeId.getWorkspaceId() == null) {
        LOG.error("Broker event skipped due to illegal content: {}", event);
        return;
    }
    eventService.publish(new BrokerEvent(event, parseTooling(encodedTooling)));
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)

Example 78 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project devspaces-images by redhat-developer.

the class WorkspaceRuntimes method startAsync.

/**
 * Starts all machines from specified workspace environment, creates workspace runtime instance
 * based on that environment.
 *
 * <p>During the start of the workspace its runtime is visible with {@link
 * WorkspaceStatus#STARTING} status.
 *
 * @param workspace workspace which environment should be started
 * @param envName optional environment name to run
 * @param options whether machines should be recovered(true) or not(false)
 * @return completable future of start execution.
 * @throws ConflictException when workspace is already running
 * @throws ConflictException when start is interrupted
 * @throws NotFoundException when any not found exception occurs during environment start
 * @throws ServerException other error occurs during environment start
 * @see WorkspaceStatus#STARTING
 * @see WorkspaceStatus#RUNNING
 */
@Traced
public CompletableFuture<Void> startAsync(WorkspaceImpl workspace, @Nullable String envName, Map<String, String> options) throws ConflictException, NotFoundException, ServerException {
    TracingTags.WORKSPACE_ID.set(workspace.getId());
    final String workspaceId = workspace.getId();
    if (isStartRefused.get()) {
        throw new ConflictException(format("Start of the workspace '%s' is rejected by the system, " + "no more workspaces are allowed to start", workspace.getName()));
    }
    WorkspaceConfigImpl config = workspace.getConfig();
    if (config == null) {
        config = devfileConverter.convert(workspace.getDevfile());
    }
    if (envName == null) {
        envName = config.getDefaultEnv();
    }
    String infraNamespace = workspace.getAttributes().get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE);
    if (isNullOrEmpty(infraNamespace)) {
        throw new ServerException(String.format("Workspace does not have infrastructure namespace " + "specified. Please set value of '%s' workspace attribute.", WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE));
    }
    final RuntimeIdentity runtimeId = new RuntimeIdentityImpl(workspaceId, envName, EnvironmentContext.getCurrent().getSubject().getUserId(), infraNamespace);
    try {
        InternalEnvironment internalEnv = createInternalEnvironment(config.getEnvironments().get(envName), config.getAttributes(), config.getCommands(), config.getDevfile());
        RuntimeContext runtimeContext = infrastructure.prepare(runtimeId, internalEnv);
        InternalRuntime runtime = runtimeContext.getRuntime();
        try (Unlocker ignored = lockService.writeLock(workspaceId)) {
            final WorkspaceStatus existingStatus = statuses.putIfAbsent(workspaceId, STARTING);
            if (existingStatus != null) {
                throw new ConflictException(format("Could not start workspace '%s' because its state is '%s'", workspaceId, existingStatus));
            }
            setRuntimesId(workspaceId);
            runtimes.put(workspaceId, runtime);
        }
        LOG.info("Starting workspace '{}/{}' with id '{}' by user '{}'", workspace.getNamespace(), workspace.getName(), workspace.getId(), sessionUserNameOr("undefined"));
        publishWorkspaceStatusEvent(workspaceId, STARTING, STOPPED, null, true, options);
        return CompletableFuture.runAsync(ThreadLocalPropagateContext.wrap(new StartRuntimeTask(workspace, options, runtime)), sharedPool.getExecutor());
    } catch (ValidationException e) {
        LOG.error(e.getLocalizedMessage(), e);
        throw new ConflictException(e.getLocalizedMessage());
    } catch (InfrastructureException e) {
        LOG.error(e.getLocalizedMessage(), e);
        throw new ServerException(e.getLocalizedMessage(), e);
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) ValidationException(org.eclipse.che.api.core.ValidationException) ConflictException(org.eclipse.che.api.core.ConflictException) InternalRuntime(org.eclipse.che.api.workspace.server.spi.InternalRuntime) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) Traced(org.eclipse.che.commons.annotation.Traced)

Example 79 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project devspaces-images by redhat-developer.

the class WorkspaceRuntimes method getInternalRuntime.

/**
 * Returns {@link InternalRuntime} implementation for workspace with the specified id.
 *
 * <p>If memory-storage does not contain internal runtime, then runtime will be recovered if it is
 * active. Otherwise, an exception will be thrown.
 *
 * @param workspaceId identifier of workspace to fetch runtime
 * @return {@link InternalRuntime} implementation for workspace with the specified id.
 * @throws InfrastructureException if any infrastructure exception occurs
 * @throws ServerException if there is no active runtime for the specified workspace
 * @throws ServerException if any other exception occurs
 */
public InternalRuntime<?> getInternalRuntime(String workspaceId) throws InfrastructureException, ServerException {
    try (Unlocker ignored = lockService.writeLock(workspaceId)) {
        InternalRuntime<?> runtime = runtimes.get(workspaceId);
        if (runtime == null) {
            try {
                final Optional<RuntimeIdentity> runtimeIdentity = infrastructure.getIdentities().stream().filter(id -> id.getWorkspaceId().equals(workspaceId)).findAny();
                if (runtimeIdentity.isPresent()) {
                    LOG.info("Runtime for workspace '{}' is requested but there is no cached one. Recovering it.", workspaceId);
                    runtime = recoverOne(infrastructure, runtimeIdentity.get());
                } else {
                    // runtime is not considered by Infrastructure as active
                    throw new ServerException("No active runtime is found");
                }
            } catch (ServerException e) {
                statuses.remove(workspaceId);
                throw e;
            } catch (UnsupportedOperationException | ConflictException e) {
                statuses.remove(workspaceId);
                throw new ServerException(e.getMessage(), e);
            }
        }
        return runtime;
    }
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) STOPPED_ATTRIBUTE_NAME(org.eclipse.che.api.workspace.shared.Constants.STOPPED_ATTRIBUTE_NAME) ThreadLocalPropagateContext(org.eclipse.che.commons.lang.concurrent.ThreadLocalPropagateContext) LoggerFactory(org.slf4j.LoggerFactory) PostConstruct(jakarta.annotation.PostConstruct) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) RUNNING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.RUNNING) RuntimeInfrastructure(org.eclipse.che.api.workspace.server.spi.RuntimeInfrastructure) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) InternalEnvironmentFactory(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironmentFactory) WorkspaceStatusEvent(org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) Map(java.util.Map) DevfileConverter(org.eclipse.che.api.workspace.server.devfile.convert.DevfileConverter) WorkspaceDao(org.eclipse.che.api.workspace.server.spi.WorkspaceDao) Collectors.toSet(java.util.stream.Collectors.toSet) NameGenerator(org.eclipse.che.commons.lang.NameGenerator) EventService(org.eclipse.che.api.core.notification.EventService) Command(org.eclipse.che.api.core.model.workspace.config.Command) ImmutableSet(com.google.common.collect.ImmutableSet) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext) RuntimeAbnormalStoppingEvent(org.eclipse.che.api.workspace.server.event.RuntimeAbnormalStoppingEvent) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.emptyList(java.util.Collections.emptyList) EventSubscriber(org.eclipse.che.api.core.notification.EventSubscriber) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) String.format(java.lang.String.format) STOPPED_ABNORMALLY_ATTRIBUTE_NAME(org.eclipse.che.api.workspace.shared.Constants.STOPPED_ABNORMALLY_ATTRIBUTE_NAME) Sets(com.google.common.collect.Sets) RuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeImpl) Nullable(org.eclipse.che.commons.annotation.Nullable) RuntimeStartInterruptedException(org.eclipse.che.api.workspace.server.spi.RuntimeStartInterruptedException) STARTING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STARTING) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) TracingTags(org.eclipse.che.commons.tracing.TracingTags) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE) Entry(java.util.Map.Entry) Optional(java.util.Optional) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) STOPPED(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED) Workspace(org.eclipse.che.api.core.model.workspace.Workspace) STOPPING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPING) System.currentTimeMillis(java.lang.System.currentTimeMillis) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Singleton(javax.inject.Singleton) Traced(org.eclipse.che.commons.annotation.Traced) ValidationException(org.eclipse.che.api.core.ValidationException) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) Inject(javax.inject.Inject) CHE_DEVWORKSPACES_ENABLED_PROPERTY(org.eclipse.che.api.workspace.shared.Constants.CHE_DEVWORKSPACES_ENABLED_PROPERTY) WORKSPACE_STOPPED_BY(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_STOPPED_BY) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ERROR_MESSAGE_ATTRIBUTE_NAME(org.eclipse.che.api.workspace.shared.Constants.ERROR_MESSAGE_ATTRIBUTE_NAME) Subject(org.eclipse.che.commons.subject.Subject) ConflictException(org.eclipse.che.api.core.ConflictException) DevfileImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl) Named(javax.inject.Named) DBInitializer(org.eclipse.che.core.db.DBInitializer) DtoFactory(org.eclipse.che.dto.server.DtoFactory) ProbeScheduler(org.eclipse.che.api.workspace.server.hc.probe.ProbeScheduler) InternalRuntime(org.eclipse.che.api.workspace.server.spi.InternalRuntime) Collections.emptyMap(java.util.Collections.emptyMap) Logger(org.slf4j.Logger) RuntimeAbnormalStoppedEvent(org.eclipse.che.api.workspace.server.event.RuntimeAbnormalStoppedEvent) Constants(org.eclipse.che.api.workspace.shared.Constants) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) SetView(com.google.common.collect.Sets.SetView) WORKSPACE_STOP_REASON(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_STOP_REASON) Environment(org.eclipse.che.api.core.model.workspace.config.Environment) NotFoundException(org.eclipse.che.api.core.NotFoundException) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) WORKSPACE_RUNTIMES_ID_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_RUNTIMES_ID_ATTRIBUTE) ServerException(org.eclipse.che.api.core.ServerException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker)

Example 80 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project che-server by eclipse-che.

the class InconsistentRuntimesDetector method stopAbnormally.

private void stopAbnormally(KubernetesInternalRuntime k8sRuntime) throws InfrastructureException {
    RuntimeIdentity runtimeId = k8sRuntime.getContext().getIdentity();
    eventPublisher.sendAbnormalStoppingEvent(runtimeId, "The runtime has inconsistent state.");
    try {
        k8sRuntime.stop(emptyMap());
    } catch (InfrastructureException e) {
        throw new InfrastructureException(format("Failed to stop the runtime '%s:%s' which has inconsistent state. Error: %s", runtimeId.getWorkspaceId(), runtimeId.getOwnerId(), e.getMessage()), e);
    } finally {
        eventPublisher.sendAbnormalStoppedEvent(runtimeId, "The runtime has inconsistent state.");
    }
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Aggregations

RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)128 Test (org.testng.annotations.Test)88 RuntimeIdentityImpl (org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl)70 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)40 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)28 InternalEnvironment (org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment)24 RuntimeContext (org.eclipse.che.api.workspace.server.spi.RuntimeContext)22 Map (java.util.Map)20 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)18 List (java.util.List)16 Set (java.util.Set)14 ServerException (org.eclipse.che.api.core.ServerException)14 InternalInfrastructureException (org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)14 Traced (org.eclipse.che.commons.annotation.Traced)14 KubernetesMachineImpl (org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl)14 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)12 Secret (io.fabric8.kubernetes.api.model.Secret)12 String.format (java.lang.String.format)12 Collectors (java.util.stream.Collectors)12 Named (javax.inject.Named)12