|
| 1 | +package io.javaoperatorsdk.operator.junit; |
| 2 | + |
| 3 | +import static io.javaoperatorsdk.operator.api.config.ControllerConfigurationOverrider.override; |
| 4 | + |
| 5 | +import io.fabric8.kubernetes.api.model.KubernetesResourceList; |
| 6 | +import io.fabric8.kubernetes.api.model.NamespaceBuilder; |
| 7 | +import io.fabric8.kubernetes.client.CustomResource; |
| 8 | +import io.fabric8.kubernetes.client.DefaultKubernetesClient; |
| 9 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 10 | +import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation; |
| 11 | +import io.fabric8.kubernetes.client.dsl.Resource; |
| 12 | +import io.javaoperatorsdk.operator.Operator; |
| 13 | +import io.javaoperatorsdk.operator.api.ResourceController; |
| 14 | +import io.javaoperatorsdk.operator.api.config.ConfigurationService; |
| 15 | +import io.javaoperatorsdk.operator.api.config.DefaultConfigurationService; |
| 16 | +import io.javaoperatorsdk.operator.processing.retry.Retry; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.InputStream; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.UUID; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import java.util.stream.Collectors; |
| 24 | +import org.awaitility.Awaitility; |
| 25 | +import org.junit.jupiter.api.extension.AfterAllCallback; |
| 26 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 27 | +import org.junit.jupiter.api.extension.BeforeAllCallback; |
| 28 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 29 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +public class OperatorExtension implements |
| 34 | + HasKubernetesClient, |
| 35 | + BeforeAllCallback, BeforeEachCallback, |
| 36 | + AfterAllCallback, AfterEachCallback { |
| 37 | + |
| 38 | + private static final Logger LOGGER = LoggerFactory.getLogger(OperatorExtension.class); |
| 39 | + |
| 40 | + private final KubernetesClient kubernetesClient; |
| 41 | + private final ConfigurationService configurationService; |
| 42 | + private final String namespace; |
| 43 | + private final Operator operator; |
| 44 | + private final boolean preserveNamespaceOnError; |
| 45 | + |
| 46 | + private OperatorExtension(ConfigurationService configurationService, boolean preserveNamespaceOnError) { |
| 47 | + this.kubernetesClient = new DefaultKubernetesClient(); |
| 48 | + this.namespace = UUID.randomUUID().toString(); |
| 49 | + this.configurationService = configurationService; |
| 50 | + this.operator = new Operator(this.kubernetesClient, this.configurationService); |
| 51 | + this.preserveNamespaceOnError = preserveNamespaceOnError; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void beforeAll(ExtensionContext context) throws Exception { |
| 56 | + LOGGER.info("beforeAll"); |
| 57 | + before(context); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void beforeEach(ExtensionContext context) throws Exception { |
| 62 | + LOGGER.info("beforeEach"); |
| 63 | + before(context); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void afterAll(ExtensionContext context) throws Exception { |
| 68 | + LOGGER.info("afterAll"); |
| 69 | + after(context); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void afterEach(ExtensionContext context) throws Exception { |
| 74 | + LOGGER.info("afterEach"); |
| 75 | + after(context); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public KubernetesClient getKubernetesClient() { |
| 80 | + return kubernetesClient; |
| 81 | + } |
| 82 | + |
| 83 | + public String getNamespace() { |
| 84 | + return namespace; |
| 85 | + } |
| 86 | + |
| 87 | + @SuppressWarnings({"rawtypes"}) |
| 88 | + public Iterable<ResourceController> controllers() { |
| 89 | + return operator.getControllers().stream() |
| 90 | + .map(Operator.ControllerRef::getController) |
| 91 | + .collect(Collectors.toUnmodifiableList()); |
| 92 | + } |
| 93 | + |
| 94 | + @SuppressWarnings({"rawtypes"}) |
| 95 | + public <T extends CustomResource> NonNamespaceOperation<T, KubernetesResourceList<T>, Resource<T>> getResourceClient( |
| 96 | + Class<T> type) { |
| 97 | + return kubernetesClient.resources(type).inNamespace(namespace); |
| 98 | + } |
| 99 | + |
| 100 | + @SuppressWarnings({"rawtypes"}) |
| 101 | + public <T extends CustomResource> T getCustomResource(Class<T> type, String name) { |
| 102 | + return kubernetesClient.resources(type).inNamespace(namespace).withName(name).get(); |
| 103 | + } |
| 104 | + |
| 105 | + public void register(ResourceController<? extends CustomResource<?, ?>> controller) { |
| 106 | + register(controller, null); |
| 107 | + } |
| 108 | + |
| 109 | + @SuppressWarnings({"unchecked", "rawtypes"}) |
| 110 | + public void register(ResourceController controller, Retry retry) { |
| 111 | + final var config = configurationService.getConfigurationFor(controller); |
| 112 | + final var oconfig = override(config).settingNamespace(namespace); |
| 113 | + final var path = "/META-INF/fabric8/" + config.getCRDName() + "-v1.yml"; |
| 114 | + |
| 115 | + if (retry != null) { |
| 116 | + oconfig.withRetry(retry); |
| 117 | + } |
| 118 | + |
| 119 | + try (InputStream is = getClass().getResourceAsStream(path)) { |
| 120 | + kubernetesClient.load(is).createOrReplace(); |
| 121 | + } catch (IOException ex) { |
| 122 | + throw new IllegalStateException("Cannot find yaml on classpath: " + path); |
| 123 | + } |
| 124 | + |
| 125 | + if (controller instanceof KubernetesClientAware) { |
| 126 | + ((KubernetesClientAware) controller).setKubernetesClient(kubernetesClient); |
| 127 | + } |
| 128 | + |
| 129 | + this.operator.register(controller, oconfig.build()); |
| 130 | + |
| 131 | + LOGGER.info("Controller {} is registered", controller.getClass().getCanonicalName()); |
| 132 | + } |
| 133 | + |
| 134 | + protected void before(ExtensionContext context) { |
| 135 | + LOGGER.info("Initializing integration test in namespace {}", namespace); |
| 136 | + |
| 137 | + kubernetesClient.namespaces().create( |
| 138 | + new NamespaceBuilder() |
| 139 | + .withNewMetadata() |
| 140 | + .withName(namespace) |
| 141 | + .endMetadata() |
| 142 | + .build()); |
| 143 | + |
| 144 | + this.operator.start(); |
| 145 | + } |
| 146 | + |
| 147 | + protected void after(ExtensionContext context) { |
| 148 | + if (preserveNamespaceOnError && context.getExecutionException().isPresent()) { |
| 149 | + LOGGER.info("Preserving namespace {}", namespace); |
| 150 | + } else { |
| 151 | + LOGGER.info("Deleting namespace {} and stopping operator", namespace); |
| 152 | + kubernetesClient.namespaces().withName(namespace).delete(); |
| 153 | + Awaitility.await("namespace deleted") |
| 154 | + .atMost(45, TimeUnit.SECONDS) |
| 155 | + .until(() -> kubernetesClient.namespaces().withName(namespace).get() == null); |
| 156 | + } |
| 157 | + |
| 158 | + this.operator.close(); |
| 159 | + this.kubernetesClient.close(); |
| 160 | + } |
| 161 | + |
| 162 | + public static Builder builder() { |
| 163 | + return new Builder(); |
| 164 | + } |
| 165 | + |
| 166 | + @SuppressWarnings("rawtypes") |
| 167 | + public static class Builder { |
| 168 | + private final List<ResourceController> controllers; |
| 169 | + private ConfigurationService configurationService; |
| 170 | + private boolean preserveNamespaceOnError = false; |
| 171 | + |
| 172 | + protected Builder() { |
| 173 | + this.configurationService = new DefaultConfigurationService(null); |
| 174 | + this.controllers = new ArrayList<>(); |
| 175 | + } |
| 176 | + |
| 177 | + public Builder preserveNamespaceOnError(boolean value) { |
| 178 | + this.preserveNamespaceOnError = value; |
| 179 | + return this; |
| 180 | + } |
| 181 | + |
| 182 | + public Builder withConfigurationService(ConfigurationService value) { |
| 183 | + configurationService = value; |
| 184 | + return this; |
| 185 | + } |
| 186 | + |
| 187 | + @SuppressWarnings("rawtypes") |
| 188 | + public Builder withController(ResourceController value) { |
| 189 | + controllers.add(value); |
| 190 | + return this; |
| 191 | + } |
| 192 | + |
| 193 | + @SuppressWarnings("rawtypes") |
| 194 | + public Builder withController(Class<? extends ResourceController> value) { |
| 195 | + try { |
| 196 | + controllers.add(value.getConstructor().newInstance()); |
| 197 | + } catch (Exception e) { |
| 198 | + throw new RuntimeException(e); |
| 199 | + } |
| 200 | + return this; |
| 201 | + } |
| 202 | + |
| 203 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 204 | + public OperatorExtension build() { |
| 205 | + OperatorExtension answer = new OperatorExtension(configurationService, preserveNamespaceOnError); |
| 206 | + for (ResourceController controller: controllers) { |
| 207 | + answer.register(controller); |
| 208 | + } |
| 209 | + |
| 210 | + return answer; |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments