Search in sources :

Example 1 with RuntimeIdentity

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

the class OpenShiftProjectFactoryTest method shouldRequireNamespacePriorExistenceIfDifferentFromDefaultAndUserDefinedIsNotAllowed.

@Test
public void shouldRequireNamespacePriorExistenceIfDifferentFromDefaultAndUserDefinedIsNotAllowed() throws Exception {
    // There is only one scenario where this can happen. The workspace was created and started in
    // some default namespace. Then server was reconfigured to use a different default namespace
    // AND the namespace of the workspace was MANUALLY deleted in the cluster. In this case, we
    // should NOT try to re-create the namespace because it would be created in a namespace that
    // is not configured. We DO allow it to start if the namespace still exists though.
    // given
    projectFactory = spy(new OpenShiftProjectFactory("<userid>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, true, emptySet(), clientFactory, cheClientFactory, cheServerOpenshiftClientFactory, userManager, preferenceManager, pool, NO_OAUTH_IDENTITY_PROVIDER));
    OpenShiftProject toReturnProject = mock(OpenShiftProject.class);
    prepareProject(toReturnProject);
    doReturn(toReturnProject).when(projectFactory).doCreateProjectAccess(any(), any());
    // when
    RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", null, USER_ID, "old-default");
    OpenShiftProject project = projectFactory.getOrCreate(identity);
    // then
    assertEquals(toReturnProject, project);
    verify(toReturnProject).prepare(eq(false), eq(false), any(), any());
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Test(org.testng.annotations.Test)

Example 2 with RuntimeIdentity

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

the class OpenShiftProjectFactoryTest method shouldCallStopWorkspaceRoleProvisionWhenIdentityProviderIsDefined.

@Test
public void shouldCallStopWorkspaceRoleProvisionWhenIdentityProviderIsDefined() throws Exception {
    var saConf = spy(new OpenShiftWorkspaceServiceAccountConfigurator("serviceAccount", "", clientFactory));
    projectFactory = spy(new OpenShiftProjectFactory("<userid>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, true, Set.of(saConf), clientFactory, cheClientFactory, cheServerOpenshiftClientFactory, userManager, preferenceManager, pool, OAUTH_IDENTITY_PROVIDER));
    OpenShiftProject toReturnProject = mock(OpenShiftProject.class);
    when(toReturnProject.getName()).thenReturn("workspace123");
    prepareProject(toReturnProject);
    doReturn(toReturnProject).when(projectFactory).doCreateProjectAccess(any(), any());
    OpenShiftWorkspaceServiceAccount serviceAccount = mock(OpenShiftWorkspaceServiceAccount.class);
    doReturn(serviceAccount).when(saConf).createServiceAccount("workspace123", "workspace123");
    // when
    RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", null, USER_ID, "workspace123");
    projectFactory.getOrCreate(identity);
    // then
    verify(serviceAccount).prepare();
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) OpenShiftWorkspaceServiceAccountConfigurator(org.eclipse.che.workspace.infrastructure.openshift.project.configurator.OpenShiftWorkspaceServiceAccountConfigurator) Test(org.testng.annotations.Test)

Example 3 with RuntimeIdentity

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

the class OpenShiftProjectFactoryTest method shouldNotCreatePreferencesConfigmapIfExist.

@Test
public void shouldNotCreatePreferencesConfigmapIfExist() throws Exception {
    // given
    projectFactory = spy(new OpenShiftProjectFactory("<userid>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, true, Set.of(new PreferencesConfigMapConfigurator(clientFactory)), clientFactory, cheClientFactory, cheServerOpenshiftClientFactory, userManager, preferenceManager, pool, NO_OAUTH_IDENTITY_PROVIDER));
    OpenShiftProject toReturnProject = mock(OpenShiftProject.class);
    prepareProject(toReturnProject);
    doReturn(toReturnProject).when(projectFactory).doCreateProjectAccess(any(), any());
    when(toReturnProject.getName()).thenReturn("namespace123");
    NonNamespaceOperation namespaceOperation = mock(NonNamespaceOperation.class);
    MixedOperation mixedOperation = mock(MixedOperation.class);
    when(osClient.configMaps()).thenReturn(mixedOperation);
    when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
    Resource<ConfigMap> cmResource = mock(Resource.class);
    when(namespaceOperation.withName(PREFERENCES_CONFIGMAP_NAME)).thenReturn(cmResource);
    when(cmResource.get()).thenReturn(mock(ConfigMap.class));
    // when
    RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", null, USER_ID, "workspace123");
    projectFactory.getOrCreate(identity);
    // then
    verify(namespaceOperation, never()).create(any());
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) PreferencesConfigMapConfigurator(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.PreferencesConfigMapConfigurator) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Test(org.testng.annotations.Test)

Example 4 with RuntimeIdentity

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

the class OpenShiftProjectFactoryTest method testAllConfiguratorsAreCalledWhenCreatingProject.

@Test
public void testAllConfiguratorsAreCalledWhenCreatingProject() throws InfrastructureException {
    // given
    String projectName = "testprojectname";
    NamespaceConfigurator configurator1 = Mockito.mock(NamespaceConfigurator.class);
    NamespaceConfigurator configurator2 = Mockito.mock(NamespaceConfigurator.class);
    Set<NamespaceConfigurator> namespaceConfigurators = Set.of(configurator1, configurator2);
    projectFactory = spy(new OpenShiftProjectFactory("<username>-che", true, true, true, NAMESPACE_LABELS, "try_placeholder_here=<username>", true, namespaceConfigurators, clientFactory, cheClientFactory, cheServerOpenshiftClientFactory, userManager, preferenceManager, pool, NO_OAUTH_IDENTITY_PROVIDER));
    EnvironmentContext.getCurrent().setSubject(new SubjectImpl("jondoe", "123", null, false));
    OpenShiftProject toReturnProject = mock(OpenShiftProject.class);
    when(toReturnProject.getName()).thenReturn(projectName);
    RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", null, USER_ID, "old-che");
    doReturn(toReturnProject).when(projectFactory).get(identity);
    // when
    OpenShiftProject project = projectFactory.getOrCreate(identity);
    // then
    NamespaceResolutionContext resolutionCtx = new NamespaceResolutionContext("workspace123", "123", "jondoe");
    verify(configurator1).configure(resolutionCtx, projectName);
    verify(configurator2).configure(resolutionCtx, projectName);
    assertEquals(project, toReturnProject);
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) NamespaceConfigurator(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.NamespaceConfigurator) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SubjectImpl(org.eclipse.che.commons.subject.SubjectImpl) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Test(org.testng.annotations.Test)

Example 5 with RuntimeIdentity

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

the class OpenShiftRuntimeContext method getRuntime.

@Override
public OpenShiftInternalRuntime getRuntime() throws InfrastructureException {
    Optional<KubernetesRuntimeState> runtimeStateOpt = runtimeStatuses.get(getIdentity());
    String workspaceId = getIdentity().getWorkspaceId();
    if (!runtimeStateOpt.isPresent()) {
        // there is no cached runtime, create a new one
        return runtimeFactory.create(this, projectFactory.getOrCreate(getIdentity()));
    }
    // there is cached runtime, restore cached one
    KubernetesRuntimeState runtimeState = runtimeStateOpt.get();
    RuntimeIdentity runtimeId = runtimeState.getRuntimeId();
    LOG.debug("Restoring runtime `{}:{}:{}`", runtimeId.getWorkspaceId(), runtimeId.getEnvName(), runtimeId.getOwnerId());
    OpenShiftInternalRuntime runtime = runtimeFactory.create(this, projectFactory.access(workspaceId, runtimeState.getNamespace()));
    runtime.scheduleRuntimeStateChecks();
    return runtime;
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) KubernetesRuntimeState(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesRuntimeState)

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