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

fix: workspaces must not fail on metrics roles creation when APi is disabled or unauthorized #93

Merged
merged 3 commits into from
Sep 2, 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 @@ -18,6 +18,7 @@
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.api.model.ServiceAccountBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import java.util.Arrays;
Expand Down Expand Up @@ -130,15 +131,27 @@ private void ensureImplicitRolesWithBindings(Client k8sClient) {
serviceAccountName + "-view");

// metrics role
ensureRoleWithBinding(
k8sClient,
buildRole(
METRICS_ROLE_NAME,
Arrays.asList("pods", "nodes"),
emptyList(),
singletonList("metrics.k8s.io"),
Arrays.asList("list", "get", "watch")),
serviceAccountName + "-metrics");
if (k8sClient.supportsApiPath("/apis/metrics.k8s.io")) {
try {
ensureRoleWithBinding(
k8sClient,
buildRole(
METRICS_ROLE_NAME,
Arrays.asList("pods", "nodes"),
emptyList(),
singletonList("metrics.k8s.io"),
Arrays.asList("list", "get", "watch")),
serviceAccountName + "-metrics");
} catch (KubernetesClientException e) {
// workaround to unblock workspace start if no permissions for metrics
if (e.getCode() == 403) {
LOG.warn(
"Unable to add metrics roles due to insufficient permissions. Workspace metrics will be disabled.");
} else {
throw e;
}
}
}

// credentials-secret role
ensureRoleWithBinding(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ public void shouldBindToAllConfiguredClusterRoles() throws Exception {
when(toReturnNamespace.getWorkspaceId()).thenReturn("workspace123");
when(toReturnNamespace.getName()).thenReturn("workspace123");
doReturn(toReturnNamespace).when(namespaceFactory).doCreateNamespaceAccess(any(), any());
when(k8sClient.supportsApiPath(eq("/apis/metrics.k8s.io"))).thenReturn(true);
when(clientFactory.create(any())).thenReturn(k8sClient);

// pre-create the cluster roles
Expand Down Expand Up @@ -754,8 +755,8 @@ public void shouldBindToAllConfiguredClusterRoles() throws Exception {

RoleList roles = k8sClient.rbac().roles().inNamespace("workspace123").list();
assertEquals(
Sets.newHashSet("workspace-view", "workspace-metrics", "workspace-secrets", "exec"),
roles.getItems().stream().map(r -> r.getMetadata().getName()).collect(Collectors.toSet()));
roles.getItems().stream().map(r -> r.getMetadata().getName()).collect(Collectors.toSet()),
Sets.newHashSet("workspace-view", "workspace-metrics", "workspace-secrets", "exec"));
RoleBindingList bindings = k8sClient.rbac().roleBindings().inNamespace("workspace123").list();
assertEquals(
bindings
Expand Down Expand Up @@ -855,6 +856,7 @@ public void shouldCreateExecAndViewRolesAndBindings() throws Exception {
when(toReturnNamespace.getWorkspaceId()).thenReturn("workspace123");
when(toReturnNamespace.getName()).thenReturn("workspace123");
doReturn(toReturnNamespace).when(namespaceFactory).doCreateNamespaceAccess(any(), any());
when(k8sClient.supportsApiPath(eq("/apis/metrics.k8s.io"))).thenReturn(true);
when(clientFactory.create(any())).thenReturn(k8sClient);

// when
Expand All @@ -871,8 +873,8 @@ public void shouldCreateExecAndViewRolesAndBindings() throws Exception {

RoleList roles = k8sClient.rbac().roles().inNamespace("workspace123").list();
assertEquals(
Sets.newHashSet("workspace-view", "workspace-metrics", "workspace-secrets", "exec"),
roles.getItems().stream().map(r -> r.getMetadata().getName()).collect(Collectors.toSet()));
roles.getItems().stream().map(r -> r.getMetadata().getName()).collect(Collectors.toSet()),
Sets.newHashSet("workspace-view", "workspace-metrics", "workspace-secrets", "exec"));
Role role1 = roles.getItems().get(0);
Role role2 = roles.getItems().get(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
*/
package org.eclipse.che.workspace.infrastructure.kubernetes.namespace;

import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.AbstractWorkspaceServiceAccount.METRICS_ROLE_NAME;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import io.fabric8.kubernetes.api.model.ServiceAccountBuilder;
Expand Down Expand Up @@ -52,6 +55,7 @@ public void setUp() throws Exception {

serverMock = new KubernetesServer(true, true);
serverMock.before();

k8sClient = serverMock.getClient();
when(clientFactory.create(anyString())).thenReturn(k8sClient);
}
Expand Down Expand Up @@ -83,4 +87,48 @@ public void shouldProvisionSARolesEvenIfItAlreadyExists() throws Exception {
RoleBindingList rbl = k8sClient.rbac().roleBindings().inNamespace(NAMESPACE).list();
assertTrue(rbl.getItems().size() > 1);
}

@Test
public void shouldCreateMetricsRoleIfAPIEnabledOnServer() throws Exception {
KubernetesClient localK8sClient = spy(serverMock.getClient());
when(localK8sClient.supportsApiPath(eq("/apis/metrics.k8s.io"))).thenReturn(true);
when(clientFactory.create(anyString())).thenReturn(localK8sClient);

// when
serviceAccount.prepare();

// then
// make sure metrics role & rb added
RoleList rl = k8sClient.rbac().roles().inNamespace(NAMESPACE).list();
assertTrue(
rl.getItems().stream().anyMatch(r -> r.getMetadata().getName().equals(METRICS_ROLE_NAME)));

RoleBindingList rbl = k8sClient.rbac().roleBindings().inNamespace(NAMESPACE).list();
assertTrue(
rbl.getItems()
.stream()
.anyMatch(rb -> rb.getMetadata().getName().equals(SA_NAME + "-metrics")));
}

@Test
public void shouldNotCreateMetricsRoleIfAPIEnabledOnServer() throws Exception {
KubernetesClient localK8sClient = spy(serverMock.getClient());
when(localK8sClient.supportsApiPath(eq("/apis/metrics.k8s.io"))).thenReturn(false);
when(clientFactory.create(anyString())).thenReturn(localK8sClient);

// when
serviceAccount.prepare();

// then
// make sure metrics role & rb not added
RoleList rl = k8sClient.rbac().roles().inNamespace(NAMESPACE).list();
assertTrue(
rl.getItems().stream().noneMatch(r -> r.getMetadata().getName().equals(METRICS_ROLE_NAME)));

RoleBindingList rbl = k8sClient.rbac().roleBindings().inNamespace(NAMESPACE).list();
assertTrue(
rbl.getItems()
.stream()
.noneMatch(rb -> rb.getMetadata().getName().equals(SA_NAME + "-metrics")));
}
}