Search in sources :

Example 41 with KubernetesRuntimeState

use of org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesRuntimeState in project devspaces-images by redhat-developer.

the class KubernetesRuntimeStateCacheTest method shouldUpdateStatusIfPreviousValueMatchesPredicate.

@Test(dependsOnMethods = "shouldReturnRuntimeStatus")
public void shouldUpdateStatusIfPreviousValueMatchesPredicate() throws Exception {
    // given
    KubernetesRuntimeState stateToUpdate = runtimesStates[0];
    // when
    boolean isUpdated = runtimesStatesCache.updateStatus(stateToUpdate.getRuntimeId(), s -> s == stateToUpdate.getStatus(), WorkspaceStatus.STOPPED);
    // then
    assertTrue(isUpdated);
    Optional<WorkspaceStatus> updatedStatusOpt = runtimesStatesCache.getStatus(stateToUpdate.getRuntimeId());
    assertTrue(updatedStatusOpt.isPresent());
    assertEquals(updatedStatusOpt.get(), WorkspaceStatus.STOPPED);
    assertNotEquals(stateToUpdate, WorkspaceStatus.STOPPED);
}
Also used : KubernetesRuntimeState(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesRuntimeState) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) Test(org.testng.annotations.Test)

Example 42 with KubernetesRuntimeState

use of org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesRuntimeState in project devspaces-images by redhat-developer.

the class KubernetesRuntimeStateCacheTest method shouldReturnRuntimeStateByRuntimeId.

@Test
public void shouldReturnRuntimeStateByRuntimeId() throws Exception {
    // given
    KubernetesRuntimeState expectedState = runtimesStates[1];
    // when
    Optional<KubernetesRuntimeState> fetchedOpt = runtimesStatesCache.get(expectedState.getRuntimeId());
    // then
    assertTrue(fetchedOpt.isPresent());
    assertEquals(expectedState, fetchedOpt.get());
}
Also used : KubernetesRuntimeState(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesRuntimeState) Test(org.testng.annotations.Test)

Example 43 with KubernetesRuntimeState

use of org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesRuntimeState in project devspaces-images by redhat-developer.

the class KubernetesRuntimeStateCacheTest method shouldPutRuntimeState.

@Test(dependsOnMethods = "shouldReturnRuntimeStateByRuntimeId")
public void shouldPutRuntimeState() throws Exception {
    // given
    KubernetesRuntimeState runtimeState = createRuntimeState(workspaces[2]);
    // when
    boolean isInserted = runtimesStatesCache.putIfAbsent(runtimeState);
    // then
    assertTrue(isInserted);
    Optional<KubernetesRuntimeState> fetchedState = runtimesStatesCache.get(runtimeState.getRuntimeId());
    assertTrue(fetchedState.isPresent());
    assertEquals(runtimeState, fetchedState.get());
}
Also used : KubernetesRuntimeState(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesRuntimeState) Test(org.testng.annotations.Test)

Example 44 with KubernetesRuntimeState

use of org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesRuntimeState in project devspaces-images by redhat-developer.

the class KubernetesRuntimeContext method getRuntime.

@Override
public KubernetesInternalRuntime 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, namespaceFactory.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());
    KubernetesInternalRuntime runtime = runtimeFactory.create(this, namespaceFactory.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)

Example 45 with KubernetesRuntimeState

use of org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesRuntimeState in project devspaces-images by redhat-developer.

the class JpaKubernetesRuntimeStateCache method doUpdateStatus.

@Transactional(rollbackOn = { RuntimeException.class, InfrastructureException.class })
protected void doUpdateStatus(RuntimeIdentity id, Predicate<WorkspaceStatus> predicate, WorkspaceStatus newStatus) throws InfrastructureException {
    EntityManager entityManager = managerProvider.get();
    Optional<KubernetesRuntimeState> existingStateOpt = get(id);
    if (!existingStateOpt.isPresent()) {
        throw new InfrastructureException("Runtime state for workspace with id '" + id.getWorkspaceId() + "' was not found");
    }
    KubernetesRuntimeState existingState = existingStateOpt.get();
    if (!predicate.test(existingState.getStatus())) {
        throw new IllegalStateException("Runtime status doesn't match to the specified predicate");
    }
    existingState.setStatus(newStatus);
    entityManager.flush();
}
Also used : EntityManager(javax.persistence.EntityManager) KubernetesRuntimeState(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesRuntimeState) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Transactional(com.google.inject.persist.Transactional)

Aggregations

KubernetesRuntimeState (org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesRuntimeState)48 Test (org.testng.annotations.Test)34 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)8 TypeLiteral (com.google.inject.TypeLiteral)6 AccountImpl (org.eclipse.che.account.spi.AccountImpl)6 WorkspaceStatus (org.eclipse.che.api.core.model.workspace.WorkspaceStatus)6 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)6 PersistTestModuleBuilder (org.eclipse.che.commons.test.db.PersistTestModuleBuilder)6 TckResourcesCleaner (org.eclipse.che.commons.test.tck.TckResourcesCleaner)6 DBInitializer (org.eclipse.che.core.db.DBInitializer)6 SchemaInitializer (org.eclipse.che.core.db.schema.SchemaInitializer)6 FlywaySchemaInitializer (org.eclipse.che.core.db.schema.impl.flyway.FlywaySchemaInitializer)6 KubernetesMachineCache (org.eclipse.che.workspace.infrastructure.kubernetes.cache.KubernetesMachineCache)6 KubernetesRuntimeStateCache (org.eclipse.che.workspace.infrastructure.kubernetes.cache.KubernetesRuntimeStateCache)6 KubernetesMachineImpl (org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl)6 KubernetesServerImpl (org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesServerImpl)6 AccountDao (org.eclipse.che.account.spi.AccountDao)4 JpaAccountDao (org.eclipse.che.account.spi.jpa.JpaAccountDao)4 JpaUserDevfileDao (org.eclipse.che.api.devfile.server.jpa.JpaUserDevfileDao)4 UserDevfileImpl (org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl)4