Search in sources :

Example 1 with KubernetesNamespaceMeta

use of org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta in project che-server by eclipse-che.

the class KubernetesNamespaceFactory method list.

/**
 * Returns list of k8s namespaces names where a user is able to run workspaces.
 */
public List<KubernetesNamespaceMeta> list() throws InfrastructureException {
    NamespaceResolutionContext resolutionCtx = new NamespaceResolutionContext(EnvironmentContext.getCurrent().getSubject());
    List<KubernetesNamespaceMeta> labeledNamespaces = findPreparedNamespaces(resolutionCtx);
    if (!labeledNamespaces.isEmpty()) {
        return labeledNamespaces;
    } else {
        return singletonList(getDefaultNamespace(resolutionCtx));
    }
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) KubernetesNamespaceMeta(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta)

Example 2 with KubernetesNamespaceMeta

use of org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta in project che-server by eclipse-che.

the class KubernetesNamespaceFactoryTest method shouldReturnDefaultNamespaceWhenItExists.

@Test
public void shouldReturnDefaultNamespaceWhenItExists() throws Exception {
    prepareNamespaceToBeFoundByName("jondoe-che", new NamespaceBuilder().withNewMetadata().withName("jondoe-che").endMetadata().withNewStatus().withNewPhase("Active").endStatus().build());
    namespaceFactory = new KubernetesNamespaceFactory("<username>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, emptySet(), clientFactory, cheClientFactory, userManager, preferenceManager, pool);
    List<KubernetesNamespaceMeta> availableNamespaces = namespaceFactory.list();
    assertEquals(availableNamespaces.size(), 1);
    KubernetesNamespaceMeta defaultNamespace = availableNamespaces.get(0);
    assertEquals(defaultNamespace.getName(), "jondoe-che");
    assertEquals(defaultNamespace.getAttributes().get(DEFAULT_ATTRIBUTE), "true");
    assertEquals(defaultNamespace.getAttributes().get(PHASE_ATTRIBUTE), "Active");
}
Also used : KubernetesNamespaceMeta(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) Test(org.testng.annotations.Test)

Example 3 with KubernetesNamespaceMeta

use of org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta in project che-server by eclipse-che.

the class KubernetesNamespaceFactoryTest method shouldNotThrowAnExceptionWhenNotAllowedToListNamespaces.

@Test
public void shouldNotThrowAnExceptionWhenNotAllowedToListNamespaces() throws Exception {
    // given
    Namespace ns = new NamespaceBuilder().withNewMetadata().withName("ns1").endMetadata().withNewStatus().withNewPhase("Active").endStatus().build();
    doThrow(new KubernetesClientException("Not allowed.", 403, new Status())).when(namespaceList).getItems();
    prepareNamespaceToBeFoundByName("jondoe-che", ns);
    namespaceFactory = new KubernetesNamespaceFactory("<username>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, emptySet(), clientFactory, cheClientFactory, userManager, preferenceManager, pool);
    EnvironmentContext.getCurrent().setSubject(new SubjectImpl("jondoe", "123", null, false));
    // when
    List<KubernetesNamespaceMeta> availableNamespaces = namespaceFactory.list();
    // then
    assertEquals(availableNamespaces.get(0).getName(), "ns1");
}
Also used : Status(io.fabric8.kubernetes.api.model.Status) KubernetesNamespaceMeta(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta) SubjectImpl(org.eclipse.che.commons.subject.SubjectImpl) Namespace(io.fabric8.kubernetes.api.model.Namespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Test(org.testng.annotations.Test)

Example 4 with KubernetesNamespaceMeta

use of org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta in project che-server by eclipse-che.

the class KubernetesNamespaceFactoryTest method shouldReturnPreparedNamespacesWhenFound.

@Test
public void shouldReturnPreparedNamespacesWhenFound() throws InfrastructureException {
    // given
    List<Namespace> namespaces = Arrays.asList(new NamespaceBuilder().withNewMetadata().withName("ns1").withAnnotations(Map.of(NAMESPACE_ANNOTATION_NAME, "jondoe")).endMetadata().withNewStatus().withNewPhase("Active").endStatus().build(), new NamespaceBuilder().withNewMetadata().withName("ns2").withAnnotations(Map.of(NAMESPACE_ANNOTATION_NAME, "jondoe")).endMetadata().withNewStatus().withNewPhase("Active").endStatus().build(), new NamespaceBuilder().withNewMetadata().withName("ns3").withAnnotations(Map.of(NAMESPACE_ANNOTATION_NAME, "some_other_user")).endMetadata().withNewStatus().withNewPhase("Active").endStatus().build());
    doReturn(namespaces).when(namespaceList).getItems();
    namespaceFactory = new KubernetesNamespaceFactory("<username>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, emptySet(), clientFactory, cheClientFactory, userManager, preferenceManager, pool);
    EnvironmentContext.getCurrent().setSubject(new SubjectImpl("jondoe", "123", null, false));
    // when
    List<KubernetesNamespaceMeta> availableNamespaces = namespaceFactory.list();
    // then
    assertEquals(availableNamespaces.size(), 2);
    verify(namespaceOperation).withLabels(Map.of(NAMESPACE_LABEL_NAME, "workspace"));
    assertEquals(availableNamespaces.get(0).getName(), "ns1");
    assertEquals(availableNamespaces.get(1).getName(), "ns2");
}
Also used : KubernetesNamespaceMeta(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta) SubjectImpl(org.eclipse.che.commons.subject.SubjectImpl) Namespace(io.fabric8.kubernetes.api.model.Namespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) Test(org.testng.annotations.Test)

Example 5 with KubernetesNamespaceMeta

use of org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta in project che-server by eclipse-che.

the class KubernetesGitCredentialManagerTest method testCreateAndSaveNewPATGitCredential.

@Test
public void testCreateAndSaveNewPATGitCredential() throws Exception {
    KubernetesNamespaceMeta meta = new KubernetesNamespaceMetaImpl("test");
    when(namespaceFactory.list()).thenReturn(Collections.singletonList(meta));
    when(clientFactory.create()).thenReturn(kubeClient);
    when(kubeClient.secrets()).thenReturn(secretsMixedOperation);
    when(secretsMixedOperation.inNamespace(eq(meta.getName()))).thenReturn(nonNamespaceOperation);
    when(nonNamespaceOperation.withLabels(anyMap())).thenReturn(filterWatchDeletable);
    when(filterWatchDeletable.list()).thenReturn(secretList);
    when(secretList.getItems()).thenReturn(emptyList());
    ArgumentCaptor<Secret> captor = ArgumentCaptor.forClass(Secret.class);
    PersonalAccessToken token = new PersonalAccessToken("https://bitbucket.com", "cheUser", "username", "userId", "token-name", "tid-23434", "token123");
    // when
    kubernetesGitCredentialManager.createOrReplace(token);
    // then
    verify(nonNamespaceOperation).createOrReplace(captor.capture());
    Secret createdSecret = captor.getValue();
    assertNotNull(createdSecret);
    assertEquals(new String(Base64.getDecoder().decode(createdSecret.getData().get("credentials"))), "https://username:token123@bitbucket.com");
    assertTrue(createdSecret.getMetadata().getName().startsWith(NAME_PATTERN));
    assertFalse(createdSecret.getMetadata().getName().contains(token.getScmUserName()));
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) KubernetesNamespaceMetaImpl(org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl) PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) KubernetesNamespaceMeta(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta) Test(org.testng.annotations.Test)

Aggregations

KubernetesNamespaceMeta (org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta)42 Test (org.testng.annotations.Test)34 KubernetesNamespaceMetaImpl (org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl)20 Secret (io.fabric8.kubernetes.api.model.Secret)18 PersonalAccessToken (org.eclipse.che.api.factory.server.scm.PersonalAccessToken)18 SubjectImpl (org.eclipse.che.commons.subject.SubjectImpl)16 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)10 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)10 SecretBuilder (io.fabric8.kubernetes.api.model.SecretBuilder)10 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)8 KubernetesNamespace (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace)8 KubernetesSecrets (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesSecrets)8 NamespaceBuilder (io.fabric8.kubernetes.api.model.NamespaceBuilder)6 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)6 NamespaceResolutionContext (org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext)6 Namespace (io.fabric8.kubernetes.api.model.Namespace)4 Status (io.fabric8.kubernetes.api.model.Status)4 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)4 Project (io.fabric8.openshift.api.model.Project)3 SecretList (io.fabric8.kubernetes.api.model.SecretList)2