Search in sources :

Example 6 with RuntimeContext

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

the class WorkspaceRuntimesTest method shouldReturnRuntimesIdsOfActiveWorkspacesForGivenOwner.

@Test
public void shouldReturnRuntimesIdsOfActiveWorkspacesForGivenOwner() throws Exception {
    // given
    String ws1 = generate("workspace", 6);
    String ws2 = generate("workspace", 6);
    String ws3 = generate("workspace", 6);
    String owner = generate("user", 6);
    when(statuses.asMap()).thenReturn(ImmutableMap.of(ws1, WorkspaceStatus.STARTING, ws2, WorkspaceStatus.RUNNING, ws3, WorkspaceStatus.STOPPING));
    RuntimeIdentityImpl runtimeIdentity1 = new RuntimeIdentityImpl(ws1, generate("env", 6), owner, generate("infraNamespace", 6));
    RuntimeIdentityImpl runtimeIdentity2 = new RuntimeIdentityImpl(ws2, generate("env", 6), generate("user", 6), generate("infraNamespace", 6));
    RuntimeIdentityImpl runtimeIdentity3 = new RuntimeIdentityImpl(ws3, generate("env", 6), generate("user", 6), generate("infraNamespace", 6));
    mockWorkspaceWithConfig(runtimeIdentity1);
    mockWorkspaceWithConfig(runtimeIdentity2);
    mockWorkspaceWithConfig(runtimeIdentity3);
    RuntimeContext context1 = mockContext(runtimeIdentity1);
    RuntimeContext context2 = mockContext(runtimeIdentity2);
    RuntimeContext context3 = mockContext(runtimeIdentity3);
    when(context1.getRuntime()).thenReturn(new TestInternalRuntime(context1, emptyMap(), WorkspaceStatus.STARTING));
    when(context2.getRuntime()).thenReturn(new TestInternalRuntime(context2, emptyMap(), WorkspaceStatus.RUNNING));
    when(context3.getRuntime()).thenReturn(new TestInternalRuntime(context3, emptyMap(), WorkspaceStatus.STOPPING));
    doReturn(context1).when(infrastructure).prepare(eq(runtimeIdentity1), any());
    doReturn(context2).when(infrastructure).prepare(eq(runtimeIdentity2), any());
    doReturn(context3).when(infrastructure).prepare(eq(runtimeIdentity3), any());
    Set<RuntimeIdentity> identities = ImmutableSet.of(runtimeIdentity1, runtimeIdentity2, runtimeIdentity3);
    doReturn(identities).when(infrastructure).getIdentities();
    InternalEnvironment internalEnvironment = mock(InternalEnvironment.class);
    doReturn(internalEnvironment).when(testEnvFactory).create(any(Environment.class));
    // when
    Set<String> active = runtimes.getActive(owner);
    // then
    assertEquals(active.size(), 1);
    assertTrue(active.containsAll(asList(ws1)));
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) 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) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Test(org.testng.annotations.Test)

Example 7 with RuntimeContext

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

the class WorkspaceRuntimesTest method runtimeIsRecoveredForWorkspaceWithDevfile.

@Test
public void runtimeIsRecoveredForWorkspaceWithDevfile() throws Exception {
    RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", "default", "myId", "infraNamespace");
    WorkspaceImpl workspaceMock = mockWorkspaceWithDevfile(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);
    verify(devfileConverter).convert(workspaceMock.getDevfile());
}
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)

Example 8 with RuntimeContext

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

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

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

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

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

the class WorkspaceRuntimesTest method runtimeRecoveryContinuesThroughRuntimeException.

@Test
public void runtimeRecoveryContinuesThroughRuntimeException() 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(identity2);
    RuntimeContext context3 = mockContext(identity3);
    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 RuntimeException("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) 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