|
| 1 | +package io.javaoperatorsdk.operator.sample; |
| 2 | + |
| 3 | +import java.io.FileInputStream; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.time.Duration; |
| 8 | +import java.util.List; |
| 9 | +import java.util.UUID; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +import io.fabric8.kubernetes.api.model.HasMetadata; |
| 17 | +import io.fabric8.kubernetes.api.model.NamespaceBuilder; |
| 18 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
| 19 | +import io.fabric8.kubernetes.api.model.rbac.RoleBinding; |
| 20 | +import io.fabric8.kubernetes.client.ConfigBuilder; |
| 21 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 22 | +import io.fabric8.kubernetes.client.KubernetesClientBuilder; |
| 23 | + |
| 24 | +import static io.javaoperatorsdk.operator.junit.AbstractOperatorExtension.CRD_READY_WAIT; |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | +import static org.awaitility.Awaitility.await; |
| 27 | + |
| 28 | + |
| 29 | +class ControllerNamespaceDeletionE2E { |
| 30 | + |
| 31 | + private static final Logger log = LoggerFactory.getLogger(ControllerNamespaceDeletionE2E.class); |
| 32 | + |
| 33 | + public static final String TEST_RESOURCE_NAME = "test1"; |
| 34 | + public static final String INITIAL_VALUE = "initial value"; |
| 35 | + public static final String ROLE_ROLE_BINDING_FINALIZER = "controller.deletion/finalizer"; |
| 36 | + public static final String RESOURCE_NAME = "operator"; |
| 37 | + |
| 38 | + String namespace; |
| 39 | + KubernetesClient client; |
| 40 | + |
| 41 | + // not for local mode by design |
| 42 | + // @EnabledIfSystemProperty(named = "test.deployment", matches = "remote") |
| 43 | + @Test |
| 44 | + void customResourceCleanedUpOnNamespaceDeletion() { |
| 45 | + deployController(); |
| 46 | + client.resource(testResource()).serverSideApply(); |
| 47 | + |
| 48 | + await().untilAsserted(() -> { |
| 49 | + var res = client.resources(ControllerNamespaceDeletionCustomResource.class) |
| 50 | + .inNamespace(namespace).withName(TEST_RESOURCE_NAME).get(); |
| 51 | + assertThat(res.getStatus()).isNotNull(); |
| 52 | + assertThat(res.getStatus().getValue()).isEqualTo(INITIAL_VALUE); |
| 53 | + }); |
| 54 | + |
| 55 | + client.namespaces().withName(namespace).delete(); |
| 56 | + |
| 57 | + await().timeout(Duration.ofSeconds(20)).untilAsserted(() -> { |
| 58 | + var ns = client.resources(ControllerNamespaceDeletionCustomResource.class) |
| 59 | + .inNamespace(namespace).withName(TEST_RESOURCE_NAME).get(); |
| 60 | + assertThat(ns).isNull(); |
| 61 | + }); |
| 62 | + |
| 63 | + log.info("Removing finalizers from role and role bing"); |
| 64 | + removeRoleAndRoleBindingFinalizers(); |
| 65 | + |
| 66 | + await().untilAsserted(() -> { |
| 67 | + var ns = client.namespaces().withName(namespace).get(); |
| 68 | + assertThat(ns).isNull(); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + private void removeRoleAndRoleBindingFinalizers() { |
| 73 | + var rolebinding = |
| 74 | + client.rbac().roleBindings().inNamespace(namespace).withName(RESOURCE_NAME).get(); |
| 75 | + rolebinding.getFinalizers().clear(); |
| 76 | + client.resource(rolebinding).update(); |
| 77 | + |
| 78 | + var role = client.rbac().roles().inNamespace(namespace).withName(RESOURCE_NAME).get(); |
| 79 | + role.getFinalizers().clear(); |
| 80 | + client.resource(role).update(); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + ControllerNamespaceDeletionCustomResource testResource() { |
| 85 | + var cr = new ControllerNamespaceDeletionCustomResource(); |
| 86 | + cr.setMetadata(new ObjectMetaBuilder() |
| 87 | + .withName(TEST_RESOURCE_NAME) |
| 88 | + .withNamespace(namespace) |
| 89 | + .build()); |
| 90 | + cr.setSpec(new ControllerNamespaceDeletionSpec()); |
| 91 | + cr.getSpec().setValue(INITIAL_VALUE); |
| 92 | + return cr; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + @BeforeEach |
| 97 | + void setup() { |
| 98 | + namespace = "controller-namespace-" + UUID.randomUUID(); |
| 99 | + client = new KubernetesClientBuilder().withConfig(new ConfigBuilder() |
| 100 | + .withNamespace(namespace) |
| 101 | + .build()).build(); |
| 102 | + applyCRD(); |
| 103 | + client.namespaces().resource(new NamespaceBuilder().withNewMetadata().withName(namespace) |
| 104 | + .endMetadata().build()).create(); |
| 105 | + } |
| 106 | + |
| 107 | + void deployController() { |
| 108 | + try { |
| 109 | + List<HasMetadata> resources = client.load(new FileInputStream("k8s/operator.yaml")).items(); |
| 110 | + resources.forEach(hm -> { |
| 111 | + hm.getMetadata().setNamespace(namespace); |
| 112 | + if (hm.getKind().equalsIgnoreCase("rolebinding")) { |
| 113 | + var crb = (RoleBinding) hm; |
| 114 | + for (var subject : crb.getSubjects()) { |
| 115 | + subject.setNamespace(namespace); |
| 116 | + } |
| 117 | + } |
| 118 | + }); |
| 119 | + client.resourceList(resources) |
| 120 | + .inNamespace(namespace) |
| 121 | + .createOrReplace(); |
| 122 | + |
| 123 | + } catch (FileNotFoundException e) { |
| 124 | + throw new RuntimeException(e); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + void applyCRD() { |
| 129 | + String path = |
| 130 | + "target/classes/META-INF/fabric8/controllernamespacedeletioncustomresources.namespacedeletion.io-v1.yml"; |
| 131 | + try (InputStream is = new FileInputStream(path)) { |
| 132 | + final var crd = client.load(is); |
| 133 | + crd.serverSideApply(); |
| 134 | + Thread.sleep(CRD_READY_WAIT); |
| 135 | + log.debug("Applied CRD with name: {}", crd.get().get(0).getMetadata().getName()); |
| 136 | + } catch (InterruptedException | IOException e) { |
| 137 | + throw new RuntimeException(e); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments