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));
}
}
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");
}
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");
}
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");
}
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()));
}
Aggregations