Search in sources :

Example 51 with NamespaceResolutionContext

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

the class CredentialsSecretConfiguratorTest method setUp.

@BeforeMethod
public void setUp() throws InfrastructureException {
    configurator = new CredentialsSecretConfigurator(clientFactory);
    serverMock = new KubernetesServer(true, true);
    serverMock.before();
    KubernetesClient client = spy(serverMock.getClient());
    when(clientFactory.create()).thenReturn(client);
    namespaceResolutionContext = new NamespaceResolutionContext(TEST_WORKSPACE_ID, TEST_USER_ID, TEST_USERNAME);
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) KubernetesServer(io.fabric8.kubernetes.client.server.mock.KubernetesServer) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 52 with NamespaceResolutionContext

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

the class UserPermissionConfiguratorTest method setUp.

@BeforeMethod
public void setUp() throws InfrastructureException {
    configurator = new UserPermissionConfigurator(TEST_CLUSTER_ROLES, clientFactory);
    serverMock = new KubernetesServer(true, true);
    serverMock.before();
    client = spy(serverMock.getClient());
    lenient().when(clientFactory.create()).thenReturn(client);
    namespaceResolutionContext = new NamespaceResolutionContext(TEST_WORKSPACE_ID, TEST_USER_ID, TEST_USERNAME);
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) KubernetesServer(io.fabric8.kubernetes.client.server.mock.KubernetesServer) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 53 with NamespaceResolutionContext

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

the class UserProfileConfiguratorTest method setUp.

@BeforeMethod
public void setUp() throws InfrastructureException, NotFoundException, ServerException {
    context = new NamespaceResolutionContext(null, USER_ID, USER_NAME);
    kubernetesServer = new KubernetesServer(true, true);
    kubernetesServer.before();
    when(userManager.getById(USER_ID)).thenReturn(new UserImpl(USER_ID, USER_EMAIL, USER_NAME));
    when(clientFactory.create()).thenReturn(kubernetesServer.getClient());
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl) KubernetesServer(io.fabric8.kubernetes.client.server.mock.KubernetesServer) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 54 with NamespaceResolutionContext

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

the class KubernetesNamespaceFactory method getOrCreate.

public KubernetesNamespace getOrCreate(RuntimeIdentity identity) throws InfrastructureException {
    KubernetesNamespace namespace = get(identity);
    var subject = EnvironmentContext.getCurrent().getSubject();
    NamespaceResolutionContext resolutionCtx = new NamespaceResolutionContext(identity.getWorkspaceId(), subject.getUserId(), subject.getUserName());
    Map<String, String> namespaceAnnotationsEvaluated = evaluateAnnotationPlaceholders(resolutionCtx);
    namespace.prepare(canCreateNamespace(identity), labelNamespaces ? namespaceLabels : emptyMap(), annotateNamespaces ? namespaceAnnotationsEvaluated : emptyMap());
    configureNamespace(resolutionCtx, namespace.getName());
    return namespace;
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext)

Example 55 with NamespaceResolutionContext

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

the class KubernetesNamespaceFactory method getNamespaceName.

/**
 * Returns a namespace name where workspace is assigned to.
 */
protected String getNamespaceName(Workspace workspace) throws InfrastructureException {
    String namespace = workspace.getAttributes().get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE);
    if (namespace == null) {
        // it seems to be legacy workspace since the namespace is not stored in workspace attributes
        // it's needed to evaluate that with current user and workspace id
        NamespaceResolutionContext resolutionCtx = new NamespaceResolutionContext(workspace.getId(), EnvironmentContext.getCurrent().getSubject().getUserId(), EnvironmentContext.getCurrent().getSubject().getUserName());
        namespace = evaluateNamespaceName(resolutionCtx);
        LOG.warn("Workspace '{}' doesn't have an explicit namespace assigned." + " The legacy namespace resolution resolved it to '{}'.", workspace.getId(), namespace);
    }
    if (!NamespaceNameValidator.isValid(namespace)) {
        // At a certain unfortunate past version of Che, we stored invalid namespace names.
        // At this point in time, we're trying to work with an existing workspace that never could
        // started OR has been running since before that unfortunate version. In both cases, going
        // back to the default namespace name is the most safe bet we can make.
        // but of course, our attempt will be futile if we're running in a context that doesn't know
        // the current user.
        Subject subj = EnvironmentContext.getCurrent().getSubject();
        if (!subj.isAnonymous()) {
            NamespaceResolutionContext resolutionCtx = new NamespaceResolutionContext(workspace.getId(), subj.getUserId(), subj.getUserName());
            String defaultNamespace = evaluateNamespaceName(resolutionCtx);
            LOG.warn("The namespace '{}' of the workspace '{}' is not valid. Trying to recover" + " from this situation using a default namespace which resolved to '{}'.", namespace, workspace.getId(), defaultNamespace);
            namespace = defaultNamespace;
        } else {
            // log a warning including a stacktrace to be able to figure out from where we got here...
            LOG.warn("The namespace '{}' of the workspace '{}' is not valid but we currently don't have" + " an active user to try an recover from this situation. We're letting the parent" + " workflow continue, but it may fail at some later point in time because of" + " the incorrect namespace name in use.", namespace, workspace.getId(), new Throwable());
        }
    // ok, we tried to recover the namespace but nothing helped.
    }
    return namespace;
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) Subject(org.eclipse.che.commons.subject.Subject)

Aggregations

NamespaceResolutionContext (org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext)56 Test (org.testng.annotations.Test)30 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 KubernetesServer (io.fabric8.kubernetes.client.server.mock.KubernetesServer)14 BeforeMethod (org.testng.annotations.BeforeMethod)14 HashMap (java.util.HashMap)8 KubernetesNamespaceMetaImpl (org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl)8 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)6 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)6 KubernetesNamespaceMeta (org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta)6 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)4 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)4 UserImpl (org.eclipse.che.api.user.server.model.impl.UserImpl)4 RuntimeIdentityImpl (org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl)4 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)4 SubjectImpl (org.eclipse.che.commons.subject.SubjectImpl)4 NamespaceConfigurator (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.NamespaceConfigurator)4 Namespace (io.fabric8.kubernetes.api.model.Namespace)2 NamespaceBuilder (io.fabric8.kubernetes.api.model.NamespaceBuilder)2 Project (io.fabric8.openshift.api.model.Project)2