Search in sources :

Example 21 with RuntimeContext

use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project che-server by eclipse-che.

the class WorkspaceRuntimesTest method runtimeRecoveryContinuesThroughException.

@Test
public void runtimeRecoveryContinuesThroughException() throws Exception {
    // Given
    RuntimeIdentityImpl identity1 = new RuntimeIdentityImpl("workspace1", "env1", "owner1", "infraNamespace");
    RuntimeIdentityImpl identity2 = new RuntimeIdentityImpl("workspace2", "env2", "owner2", "infraNamespace");
    RuntimeIdentityImpl identity3 = new RuntimeIdentityImpl("workspace3", "env3", "owner3", "infraNamespace");
    Set<RuntimeIdentity> identities = ImmutableSet.<RuntimeIdentity>builder().add(identity1).add(identity2).add(identity3).build();
    mockWorkspaceWithConfig(identity1);
    mockWorkspaceWithConfig(identity2);
    mockWorkspaceWithConfig(identity3);
    when(statuses.get(anyString())).thenReturn(WorkspaceStatus.STARTING);
    RuntimeContext context1 = mockContext(identity1);
    when(context1.getRuntime()).thenReturn(new TestInternalRuntime(context1, emptyMap(), WorkspaceStatus.STARTING));
    doReturn(context1).when(infrastructure).prepare(eq(identity1), any());
    RuntimeContext context2 = mockContext(identity1);
    RuntimeContext context3 = mockContext(identity1);
    when(context3.getRuntime()).thenReturn(new TestInternalRuntime(context3, emptyMap(), WorkspaceStatus.STARTING));
    doReturn(context3).when(infrastructure).prepare(eq(identity3), any());
    InternalEnvironment internalEnvironment = mock(InternalEnvironment.class);
    doReturn(internalEnvironment).when(testEnvFactory).create(any(Environment.class));
    // Want to fail recovery of identity2
    doThrow(new InfrastructureException("oops!")).when(infrastructure).prepare(eq(identity2), any(InternalEnvironment.class));
    // When
    runtimes.new RecoverRuntimesTask(identities).run();
    // Then
    verify(infrastructure).prepare(identity1, internalEnvironment);
    verify(infrastructure).prepare(identity2, internalEnvironment);
    verify(infrastructure).prepare(identity3, internalEnvironment);
    WorkspaceImpl workspace1 = WorkspaceImpl.builder().setId(identity1.getWorkspaceId()).build();
    runtimes.injectRuntime(workspace1);
    assertNotNull(workspace1.getRuntime());
    assertEquals(workspace1.getStatus(), WorkspaceStatus.STARTING);
    WorkspaceImpl workspace3 = WorkspaceImpl.builder().setId(identity3.getWorkspaceId()).build();
    runtimes.injectRuntime(workspace3);
    assertNotNull(workspace3.getRuntime());
    assertEquals(workspace3.getStatus(), WorkspaceStatus.STARTING);
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) Environment(org.eclipse.che.api.core.model.workspace.config.Environment) 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) Test(org.testng.annotations.Test)

Example 22 with RuntimeContext

use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project che-server by eclipse-che.

the class KeycloakProviderConfigFactory method buildConfig.

/**
 * Builds the OpenShift {@link Config} object based on a default {@link Config} object and an
 * optional workspace Id.
 */
public Config buildConfig(Config defaultConfig, @Nullable String workspaceId) throws InfrastructureException {
    Subject subject = EnvironmentContext.getCurrent().getSubject();
    if (oauthIdentityProvider == null) {
        LOG.debug("OAuth Provider is not configured, default config is used.");
        return defaultConfig;
    }
    if (subject == Subject.ANONYMOUS) {
        LOG.debug("OAuth Provider is configured but default subject is anonymous, default config is used.");
        return defaultConfig;
    }
    if (workspaceId == null) {
        LOG.debug("OAuth Provider is configured and this request is not related to any workspace. OAuth token will be retrieved.");
        return personalizeConfig(defaultConfig);
    }
    Optional<RuntimeContext> context = workspaceRuntimeProvider.get().getRuntimeContext(workspaceId);
    if (!context.isPresent()) {
        // there is no cached info for this workspace in workspace API.
        // it means that it's not started yet and it's initial call for preparing context
        LOG.debug("There is no runtime context for the specified workspace '%s'. It's the first workspace " + "related call, so context is personalized with OAuth token.");
        return personalizeConfig(defaultConfig);
    }
    String workspaceOwnerId = context.map(c -> c.getIdentity().getOwnerId()).orElse(null);
    boolean isRuntimeOwner = subject.getUserId().equals(workspaceOwnerId);
    if (!isRuntimeOwner) {
        LOG.debug("OAuth Provider is configured, but current subject is not runtime owner, default config is used." + "Subject user id: '{}'. Runtime owner id: '{}'", subject.getUserId(), workspaceOwnerId);
        return defaultConfig;
    }
    LOG.debug("OAuth Provider is configured and current subject is runtime owner. OAuth token will be retrieved.");
    return personalizeConfig(defaultConfig);
}
Also used : KeycloakServiceClient(org.eclipse.che.multiuser.keycloak.server.KeycloakServiceClient) CLIENT_ID_SETTING(org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.CLIENT_ID_SETTING) LoggerFactory(org.slf4j.LoggerFactory) Singleton(javax.inject.Singleton) Inject(javax.inject.Inject) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) Subject(org.eclipse.che.commons.subject.Subject) URI(java.net.URI) Named(javax.inject.Named) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) KubernetesClientConfigFactory(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesClientConfigFactory) KeycloakTokenResponse(org.eclipse.che.multiuser.keycloak.shared.dto.KeycloakTokenResponse) REALM_SETTING(org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.REALM_SETTING) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext) Logger(org.slf4j.Logger) OpenShiftConfigBuilder(io.fabric8.openshift.client.OpenShiftConfigBuilder) Nullable(org.eclipse.che.commons.annotation.Nullable) Config(io.fabric8.kubernetes.client.Config) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Provider(com.google.inject.Provider) OpenShiftConfig(io.fabric8.openshift.client.OpenShiftConfig) URLEncoder(java.net.URLEncoder) BadRequestException(org.eclipse.che.api.core.BadRequestException) UriBuilder(jakarta.ws.rs.core.UriBuilder) WorkspaceRuntimes(org.eclipse.che.api.workspace.server.WorkspaceRuntimes) Optional(java.util.Optional) KeycloakSettings(org.eclipse.che.multiuser.keycloak.server.KeycloakSettings) AUTH_SERVER_URL_SETTING(org.eclipse.che.multiuser.oidc.OIDCInfoProvider.AUTH_SERVER_URL_SETTING) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext) Subject(org.eclipse.che.commons.subject.Subject)

Example 23 with RuntimeContext

use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project che-server by eclipse-che.

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 24 with RuntimeContext

use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project devspaces-images by redhat-developer.

the class WorkspaceLinksGenerator method addRuntimeLinks.

private void addRuntimeLinks(Map<String, String> links, String workspaceId, ServiceContext serviceContext) throws ServerException {
    URI uri = serviceContext.getServiceUriBuilder().build();
    links.put(LINK_REL_ENVIRONMENT_STATUS_CHANNEL, UriBuilder.fromUri(cheWebsocketEndpoint).scheme(uri.getScheme().equals("https") ? "wss" : "ws").host(uri.getHost()).port(uri.getPort()).build().toString());
    Optional<RuntimeContext> ctxOpt = workspaceRuntimes.getRuntimeContext(workspaceId);
    if (ctxOpt.isPresent()) {
        try {
            links.put(LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL, ctxOpt.get().getOutputChannel().toString());
        } catch (UnsupportedOperationException e) {
        // Do not include output channel to links since it is not supported by context
        } catch (InfrastructureException x) {
            throw new ServerException(x.getMessage(), x);
        }
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext) URI(java.net.URI) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 25 with RuntimeContext

use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project devspaces-images by redhat-developer.

the class WorkspaceRuntimesTest method runtimeIsRecoveredForWorkspaceWithConfig.

@Test
public void runtimeIsRecoveredForWorkspaceWithConfig() throws Exception {
    RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", "my-env", "myId", "infraNamespace");
    mockWorkspaceWithConfig(identity);
    RuntimeContext context = mockContext(identity);
    when(context.getRuntime()).thenReturn(new TestInternalRuntime(context, emptyMap(), WorkspaceStatus.STARTING));
    doReturn(context).when(infrastructure).prepare(eq(identity), any());
    doReturn(mock(InternalEnvironment.class)).when(testEnvFactory).create(any());
    when(statuses.get(anyString())).thenReturn(WorkspaceStatus.STARTING);
    // try recover
    runtimes.recoverOne(infrastructure, identity);
    WorkspaceImpl workspace = WorkspaceImpl.builder().setId(identity.getWorkspaceId()).build();
    runtimes.injectRuntime(workspace);
    assertNotNull(workspace.getRuntime());
    assertEquals(workspace.getStatus(), WorkspaceStatus.STARTING);
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Test(org.testng.annotations.Test)

Aggregations

RuntimeContext (org.eclipse.che.api.workspace.server.spi.RuntimeContext)30 Test (org.testng.annotations.Test)22 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)20 RuntimeIdentityImpl (org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl)18 InternalEnvironment (org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment)18 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)16 Environment (org.eclipse.che.api.core.model.workspace.config.Environment)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)10 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)8 ServerException (org.eclipse.che.api.core.ServerException)6 URI (java.net.URI)4 ArrayList (java.util.ArrayList)4 Machine (org.eclipse.che.api.core.model.workspace.runtime.Machine)4 EventService (org.eclipse.che.api.core.notification.EventService)4 MachineImpl (org.eclipse.che.api.workspace.server.model.impl.MachineImpl)4 RuntimeIdentityDto (org.eclipse.che.api.workspace.shared.dto.RuntimeIdentityDto)4 InternalRuntime (org.eclipse.che.api.workspace.server.spi.InternalRuntime)3 Provider (com.google.inject.Provider)2 Config (io.fabric8.kubernetes.client.Config)2 OpenShiftConfig (io.fabric8.openshift.client.OpenShiftConfig)2