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