|
| 1 | +package io.javaoperatorsdk.operator.baseapi.infrastructureclient; |
| 2 | + |
| 3 | +import java.util.concurrent.TimeUnit; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.AfterEach; |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 9 | + |
| 10 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
| 11 | +import io.fabric8.kubernetes.api.model.rbac.ClusterRole; |
| 12 | +import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding; |
| 13 | +import io.fabric8.kubernetes.client.ConfigBuilder; |
| 14 | +import io.fabric8.kubernetes.client.KubernetesClientBuilder; |
| 15 | +import io.fabric8.kubernetes.client.KubernetesClientException; |
| 16 | +import io.javaoperatorsdk.operator.ReconcilerUtils; |
| 17 | +import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 21 | +import static org.awaitility.Awaitility.await; |
| 22 | + |
| 23 | +class InfrastructureClientIT { |
| 24 | + |
| 25 | + private static final String RBAC_TEST_ROLE = "rbac-test-role.yaml"; |
| 26 | + private static final String RBAC_TEST_ROLE_BINDING = "rbac-test-role-binding.yaml"; |
| 27 | + private static final String RBAC_TEST_USER = "rbac-test-user"; |
| 28 | + |
| 29 | + @RegisterExtension |
| 30 | + LocallyRunOperatorExtension operator = |
| 31 | + LocallyRunOperatorExtension.builder() |
| 32 | + .withReconciler(new InfrastructureClientTestReconciler()) |
| 33 | + .withKubernetesClient( |
| 34 | + new KubernetesClientBuilder() |
| 35 | + .withConfig(new ConfigBuilder().withImpersonateUsername(RBAC_TEST_USER).build()) |
| 36 | + .build()) |
| 37 | + .withInfrastructureKubernetesClient( |
| 38 | + new KubernetesClientBuilder().build()) // no limitations |
| 39 | + .build(); |
| 40 | + |
| 41 | + /** |
| 42 | + * We need to apply the cluster role also before the CRD deployment so the rbac-test-user is |
| 43 | + * permitted to deploy it |
| 44 | + */ |
| 45 | + public InfrastructureClientIT() { |
| 46 | + applyClusterRole(RBAC_TEST_ROLE); |
| 47 | + applyClusterRoleBinding(RBAC_TEST_ROLE_BINDING); |
| 48 | + } |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + void setup() { |
| 52 | + applyClusterRole(RBAC_TEST_ROLE); |
| 53 | + applyClusterRoleBinding(RBAC_TEST_ROLE_BINDING); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterEach |
| 57 | + void cleanup() { |
| 58 | + removeClusterRoleBinding(RBAC_TEST_ROLE_BINDING); |
| 59 | + removeClusterRole(RBAC_TEST_ROLE); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void canCreateInfrastructure() { |
| 64 | + var resource = new InfrastructureClientTestCustomResource(); |
| 65 | + resource.setMetadata( |
| 66 | + new ObjectMetaBuilder().withName("infrastructure-client-resource").build()); |
| 67 | + operator.create(resource); |
| 68 | + |
| 69 | + await() |
| 70 | + .atMost(5, TimeUnit.SECONDS) |
| 71 | + .untilAsserted( |
| 72 | + () -> { |
| 73 | + InfrastructureClientTestCustomResource r = |
| 74 | + operator.get( |
| 75 | + InfrastructureClientTestCustomResource.class, |
| 76 | + "infrastructure-client-resource"); |
| 77 | + assertThat(r).isNotNull(); |
| 78 | + }); |
| 79 | + |
| 80 | + assertThat( |
| 81 | + operator |
| 82 | + .getReconcilerOfType(InfrastructureClientTestReconciler.class) |
| 83 | + .getNumberOfExecutions()) |
| 84 | + .isEqualTo(1); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void shouldNotAccessNotPermittedResources() { |
| 89 | + assertThatThrownBy( |
| 90 | + () -> |
| 91 | + operator |
| 92 | + .getKubernetesClient() |
| 93 | + .apiextensions() |
| 94 | + .v1() |
| 95 | + .customResourceDefinitions() |
| 96 | + .list()) |
| 97 | + .isInstanceOf(KubernetesClientException.class) |
| 98 | + .hasMessageContaining( |
| 99 | + "User \"%s\" cannot list resource \"customresourcedefinitions\"" |
| 100 | + .formatted(RBAC_TEST_USER)); |
| 101 | + |
| 102 | + // but we should be able to access all resources with the infrastructure client |
| 103 | + var deploymentList = |
| 104 | + operator |
| 105 | + .getInfrastructureKubernetesClient() |
| 106 | + .apiextensions() |
| 107 | + .v1() |
| 108 | + .customResourceDefinitions() |
| 109 | + .list(); |
| 110 | + assertThat(deploymentList).isNotNull(); |
| 111 | + } |
| 112 | + |
| 113 | + private void applyClusterRoleBinding(String filename) { |
| 114 | + var clusterRoleBinding = |
| 115 | + ReconcilerUtils.loadYaml(ClusterRoleBinding.class, this.getClass(), filename); |
| 116 | + operator.getInfrastructureKubernetesClient().resource(clusterRoleBinding).serverSideApply(); |
| 117 | + } |
| 118 | + |
| 119 | + private void applyClusterRole(String filename) { |
| 120 | + var clusterRole = ReconcilerUtils.loadYaml(ClusterRole.class, this.getClass(), filename); |
| 121 | + operator.getInfrastructureKubernetesClient().resource(clusterRole).serverSideApply(); |
| 122 | + } |
| 123 | + |
| 124 | + private void removeClusterRoleBinding(String filename) { |
| 125 | + var clusterRoleBinding = |
| 126 | + ReconcilerUtils.loadYaml(ClusterRoleBinding.class, this.getClass(), filename); |
| 127 | + operator.getInfrastructureKubernetesClient().resource(clusterRoleBinding).delete(); |
| 128 | + } |
| 129 | + |
| 130 | + private void removeClusterRole(String filename) { |
| 131 | + var clusterRoleBinding = ReconcilerUtils.loadYaml(ClusterRole.class, this.getClass(), filename); |
| 132 | + operator.getInfrastructureKubernetesClient().resource(clusterRoleBinding).delete(); |
| 133 | + } |
| 134 | +} |
0 commit comments