Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a predefined configmap to store theia preferences #174

Merged
merged 5 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public abstract class AbstractWorkspaceServiceAccount<
public static final String VIEW_ROLE_NAME = "workspace-view";
public static final String METRICS_ROLE_NAME = "workspace-metrics";
public static final String SECRETS_ROLE_NAME = "workspace-secrets";
public static final String CONFIGMAPS_ROLE_NAME = "workspace-configmaps";
public static final String CREDENTIALS_SECRET_NAME = "workspace-credentials-secret";
public static final String PREFERENCES_CONFIGMAP_NAME = "workspace-preferences-configmap";

protected final String namespace;
protected final String serviceAccountName;
Expand Down Expand Up @@ -163,6 +165,17 @@ private void ensureImplicitRolesWithBindings(Client k8sClient) {
singletonList(""),
Arrays.asList("get", "patch")),
serviceAccountName + "-secrets");

// preferences-configmap role
ensureRoleWithBinding(
k8sClient,
buildRole(
CONFIGMAPS_ROLE_NAME,
singletonList("configmaps"),
singletonList(PREFERENCES_CONFIGMAP_NAME),
singletonList(""),
Arrays.asList("get", "patch")),
serviceAccountName + "-configmaps");
}

private void ensureRoleWithBinding(Client k8sClient, R role, String bindingName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta.DEFAULT_ATTRIBUTE;
import static org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta.PHASE_ATTRIBUTE;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.AbstractWorkspaceServiceAccount.CREDENTIALS_SECRET_NAME;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.AbstractWorkspaceServiceAccount.PREFERENCES_CONFIGMAP_NAME;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.NamespaceNameValidator.METADATA_NAME_MAX_LENGTH;

import com.google.common.annotations.VisibleForTesting;
Expand All @@ -29,6 +30,8 @@
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.Secret;
Expand Down Expand Up @@ -362,6 +365,20 @@ public KubernetesNamespace getOrCreate(RuntimeIdentity identity) throws Infrastr
.create(secret);
}

if (namespace.configMaps().get(PREFERENCES_CONFIGMAP_NAME).isEmpty()) {
ConfigMap configMap =
new ConfigMapBuilder()
.withNewMetadata()
.withName(PREFERENCES_CONFIGMAP_NAME)
.endMetadata()
.build();
clientFactory
.create()
.configMaps()
.inNamespace(identity.getInfrastructureNamespace())
.create(configMap);
}

if (!isNullOrEmpty(serviceAccountName)) {
KubernetesWorkspaceServiceAccount workspaceServiceAccount =
doCreateServiceAccount(namespace.getWorkspaceId(), namespace.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static java.util.Collections.singletonList;
import static java.util.Optional.empty;
import static org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE;
import static org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta.DEFAULT_ATTRIBUTE;
import static org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta.PHASE_ATTRIBUTE;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.AbstractWorkspaceServiceAccount.CREDENTIALS_SECRET_NAME;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.AbstractWorkspaceServiceAccount.SECRETS_ROLE_NAME;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.AbstractWorkspaceServiceAccount.PREFERENCES_CONFIGMAP_NAME;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespaceFactory.NAMESPACE_TEMPLATE_ATTRIBUTE;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
Expand All @@ -41,6 +42,7 @@
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.Appender;
import com.google.common.collect.ImmutableMap;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.fabric8.kubernetes.api.model.NamespaceList;
Expand All @@ -49,7 +51,6 @@
import io.fabric8.kubernetes.api.model.ServiceAccountList;
import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBuilder;
import io.fabric8.kubernetes.api.model.rbac.PolicyRule;
import io.fabric8.kubernetes.api.model.rbac.Role;
import io.fabric8.kubernetes.api.model.rbac.RoleBindingList;
import io.fabric8.kubernetes.api.model.rbac.RoleList;
Expand Down Expand Up @@ -504,6 +505,7 @@ public void shouldCreateCredentialsSecretIfNotExists() throws Exception {
KubernetesNamespace toReturnNamespace = mock(KubernetesNamespace.class);
KubernetesSecrets secrets = mock(KubernetesSecrets.class);
when(toReturnNamespace.secrets()).thenReturn(secrets);
when(toReturnNamespace.configMaps()).thenReturn(mock(KubernetesConfigsMaps.class));
when(secrets.get()).thenReturn(Collections.emptyList());
doReturn(toReturnNamespace).when(namespaceFactory).doCreateNamespaceAccess(any(), any());
MixedOperation mixedOperation = mock(MixedOperation.class);
Expand All @@ -523,6 +525,47 @@ public void shouldCreateCredentialsSecretIfNotExists() throws Exception {
Assert.assertEquals(secret.getType(), "opaque");
}

@Test
public void shouldCreatePreferencesConfigmapIfNotExists() throws Exception {
// given
namespaceFactory =
spy(
new KubernetesNamespaceFactory(
"",
"",
"<username>-che",
true,
true,
true,
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
clientFactory,
cheClientFactory,
userManager,
preferenceManager,
pool));
KubernetesNamespace toReturnNamespace = mock(KubernetesNamespace.class);
KubernetesConfigsMaps configsMaps = mock(KubernetesConfigsMaps.class);
when(toReturnNamespace.secrets()).thenReturn(mock(KubernetesSecrets.class));
when(toReturnNamespace.configMaps()).thenReturn(configsMaps);
when(configsMaps.get(eq(PREFERENCES_CONFIGMAP_NAME))).thenReturn(empty());
doReturn(toReturnNamespace).when(namespaceFactory).doCreateNamespaceAccess(any(), any());
MixedOperation mixedOperation = mock(MixedOperation.class);
lenient().when(k8sClient.configMaps()).thenReturn(mixedOperation);
lenient().when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);

// when
RuntimeIdentity identity =
new RuntimeIdentityImpl("workspace123", null, USER_ID, "workspace123");
namespaceFactory.getOrCreate(identity);

// then
ArgumentCaptor<ConfigMap> configMapCaptor = ArgumentCaptor.forClass(ConfigMap.class);
verify(namespaceOperation).create(configMapCaptor.capture());
ConfigMap configmap = configMapCaptor.getValue();
Assert.assertEquals(configmap.getMetadata().getName(), PREFERENCES_CONFIGMAP_NAME);
}

@Test
public void shouldNotCreateCredentialsSecretIfExists() throws Exception {
// given
Expand Down Expand Up @@ -558,6 +601,41 @@ public void shouldNotCreateCredentialsSecretIfExists() throws Exception {
verify(namespaceOperation, never()).create(any());
}

@Test
public void shouldNotCreatePreferencesConfigmapIfExists() throws Exception {
// given
namespaceFactory =
spy(
new KubernetesNamespaceFactory(
"",
"",
"<username>-che",
true,
true,
true,
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
clientFactory,
cheClientFactory,
userManager,
preferenceManager,
pool));
KubernetesNamespace toReturnNamespace = mock(KubernetesNamespace.class);
prepareNamespace(toReturnNamespace);
doReturn(toReturnNamespace).when(namespaceFactory).doCreateNamespaceAccess(any(), any());
MixedOperation mixedOperation = mock(MixedOperation.class);
lenient().when(k8sClient.configMaps()).thenReturn(mixedOperation);
lenient().when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);

// when
RuntimeIdentity identity =
new RuntimeIdentityImpl("workspace123", null, USER_ID, "workspace123");
namespaceFactory.getOrCreate(identity);

// then
verify(namespaceOperation, never()).create(any());
}

@Test(
expectedExceptions = InfrastructureException.class,
expectedExceptionsMessageRegExp =
Expand Down Expand Up @@ -779,7 +857,12 @@ public void shouldBindToAllConfiguredClusterRoles() throws Exception {
RoleList roles = k8sClient.rbac().roles().inNamespace("workspace123").list();
assertEquals(
roles.getItems().stream().map(r -> r.getMetadata().getName()).collect(Collectors.toSet()),
Sets.newHashSet("workspace-view", "workspace-metrics", "workspace-secrets", "exec"));
Sets.newHashSet(
"workspace-configmaps",
"workspace-view",
"workspace-metrics",
"workspace-secrets",
"exec"));
RoleBindingList bindings = k8sClient.rbac().roleBindings().inNamespace("workspace123").list();
assertEquals(
bindings
Expand All @@ -791,70 +874,12 @@ public void shouldBindToAllConfiguredClusterRoles() throws Exception {
"serviceAccount-metrics",
"serviceAccount-cluster0",
"serviceAccount-cluster1",
"serviceAccount-configmaps",
"serviceAccount-view",
"serviceAccount-exec",
"serviceAccount-secrets"));
}

@Test
public void shouldCreateAndBindCredentialsSecretRole() throws Exception {
// given
namespaceFactory =
spy(
new KubernetesNamespaceFactory(
"serviceAccount",
"cr2, cr3",
"<username>-che",
true,
true,
true,
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
clientFactory,
cheClientFactory,
userManager,
preferenceManager,
pool));
KubernetesNamespace toReturnNamespace = mock(KubernetesNamespace.class);
prepareNamespace(toReturnNamespace);
when(toReturnNamespace.getWorkspaceId()).thenReturn("workspace123");
when(toReturnNamespace.getName()).thenReturn("workspace123");
doReturn(toReturnNamespace).when(namespaceFactory).doCreateNamespaceAccess(any(), any());
when(clientFactory.create(any())).thenReturn(k8sClient);

// when
RuntimeIdentity identity =
new RuntimeIdentityImpl("workspace123", null, USER_ID, "workspace123");
namespaceFactory.getOrCreate(identity);

// then
Optional<Role> roleOptional =
k8sClient
.rbac()
.roles()
.inNamespace("workspace123")
.list()
.getItems()
.stream()
.filter(r -> r.getMetadata().getName().equals(SECRETS_ROLE_NAME))
.findAny();
assertTrue(roleOptional.isPresent());
PolicyRule rule = roleOptional.get().getRules().get(0);
assertEquals(rule.getResources(), singletonList("secrets"));
assertEquals(rule.getResourceNames(), singletonList(CREDENTIALS_SECRET_NAME));
assertEquals(rule.getApiGroups(), singletonList(""));
assertEquals(rule.getVerbs(), Arrays.asList("get", "patch"));
assertTrue(
k8sClient
.rbac()
.roleBindings()
.inNamespace("workspace123")
.list()
.getItems()
.stream()
.anyMatch(rb -> rb.getMetadata().getName().equals("serviceAccount-secrets")));
}

@Test
public void shouldCreateExecAndViewRolesAndBindings() throws Exception {
// given
Expand Down Expand Up @@ -897,7 +922,12 @@ public void shouldCreateExecAndViewRolesAndBindings() throws Exception {
RoleList roles = k8sClient.rbac().roles().inNamespace("workspace123").list();
assertEquals(
roles.getItems().stream().map(r -> r.getMetadata().getName()).collect(Collectors.toSet()),
Sets.newHashSet("workspace-view", "workspace-metrics", "workspace-secrets", "exec"));
Sets.newHashSet(
"workspace-configmaps",
"workspace-view",
"workspace-metrics",
"workspace-secrets",
"exec"));
Role role1 = roles.getItems().get(0);
Role role2 = roles.getItems().get(1);

Expand All @@ -917,6 +947,7 @@ public void shouldCreateExecAndViewRolesAndBindings() throws Exception {
"serviceAccount-metrics",
"serviceAccount-view",
"serviceAccount-exec",
"serviceAccount-configmaps",
"serviceAccount-secrets"));
}

Expand Down Expand Up @@ -1102,7 +1133,7 @@ public void testEvalNamespaceKubeAdmin() throws Exception {
userManager,
preferenceManager,
pool));
doReturn(Optional.empty()).when(namespaceFactory).fetchNamespace(anyString());
doReturn(empty()).when(namespaceFactory).fetchNamespace(anyString());

String namespace =
namespaceFactory.evaluateNamespaceName(
Expand Down Expand Up @@ -1285,7 +1316,7 @@ public void shouldFailToProvisionIfNotAbleToFindNamespace() throws Infrastructur
KubernetesNamespaceMetaImpl namespaceMeta =
new KubernetesNamespaceMetaImpl(
"jondoe-cha-cha-cha", ImmutableMap.of("phase", "active", "default", "true"));
doReturn(Optional.empty()).when(namespaceFactory).fetchNamespace(eq("jondoe-cha-cha-cha"));
doReturn(empty()).when(namespaceFactory).fetchNamespace(eq("jondoe-cha-cha-cha"));

// when
NamespaceResolutionContext context =
Expand Down Expand Up @@ -1520,7 +1551,9 @@ private void throwOnTryToGetNamespacesList(Throwable e) throws Exception {

private void prepareNamespace(KubernetesNamespace namespace) throws InfrastructureException {
KubernetesSecrets secrets = mock(KubernetesSecrets.class);
KubernetesConfigsMaps configmaps = mock(KubernetesConfigsMaps.class);
when(namespace.secrets()).thenReturn(secrets);
when(namespace.configMaps()).thenReturn(configmaps);
Secret secretMock = mock(Secret.class);
ObjectMeta objectMeta = mock(ObjectMeta.class);
when(objectMeta.getName()).thenReturn(CREDENTIALS_SECRET_NAME);
Expand Down
Loading