|
1 | 1 | package io.javaoperatorsdk.operator.junit;
|
2 | 2 |
|
3 | 3 | import java.io.ByteArrayInputStream;
|
| 4 | +import java.io.IOException; |
4 | 5 | import java.io.InputStream;
|
5 | 6 | import java.nio.charset.StandardCharsets;
|
6 | 7 | import java.time.Duration;
|
|
19 | 20 |
|
20 | 21 | import io.fabric8.kubernetes.api.model.HasMetadata;
|
21 | 22 | import io.fabric8.kubernetes.api.model.Namespaced;
|
| 23 | +import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition; |
22 | 24 | import io.fabric8.kubernetes.client.CustomResource;
|
23 | 25 | import io.fabric8.kubernetes.client.KubernetesClient;
|
24 | 26 | import io.fabric8.kubernetes.client.LocalPortForward;
|
| 27 | +import io.fabric8.kubernetes.client.dsl.NonDeletingOperation; |
25 | 28 | import io.javaoperatorsdk.operator.Operator;
|
26 | 29 | import io.javaoperatorsdk.operator.ReconcilerUtils;
|
27 | 30 | import io.javaoperatorsdk.operator.RegisteredController;
|
@@ -173,23 +176,46 @@ public static void applyCrd(Class<? extends HasMetadata> resourceClass, Kubernet
|
173 | 176 | applyCrd(ReconcilerUtils.getResourceTypeName(resourceClass), client);
|
174 | 177 | }
|
175 | 178 |
|
176 |
| - public static void applyCrd(String resourceTypeName, KubernetesClient client) { |
| 179 | + public static void deleteCrd(Class<? extends HasMetadata> resourceClass, |
| 180 | + KubernetesClient client) { |
| 181 | + try { |
| 182 | + var crd = loadCRD(ReconcilerUtils.getResourceTypeName(resourceClass), client); |
| 183 | + client.resource(crd).delete(); |
| 184 | + |
| 185 | + Thread.sleep(CRD_READY_WAIT); |
| 186 | + } catch (InterruptedException e) { |
| 187 | + throw new RuntimeException(e); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + |
| 192 | + private static CustomResourceDefinition loadCRD(String resourceTypeName, |
| 193 | + KubernetesClient client) { |
177 | 194 | String path = "/META-INF/fabric8/" + resourceTypeName + "-v1.yml";
|
178 | 195 | try (InputStream is = LocallyRunOperatorExtension.class.getResourceAsStream(path)) {
|
179 | 196 | if (is == null) {
|
180 | 197 | throw new IllegalStateException("Cannot find CRD at " + path);
|
181 | 198 | }
|
182 | 199 | var crdString = new String(is.readAllBytes(), StandardCharsets.UTF_8);
|
183 | 200 | LOGGER.debug("Applying CRD: {}", crdString);
|
184 |
| - final var crd = client.load(new ByteArrayInputStream(crdString.getBytes())); |
185 |
| - crd.createOrReplace(); |
| 201 | + final var resources = client.load(new ByteArrayInputStream(crdString.getBytes())); |
| 202 | + return (CustomResourceDefinition) resources.items().get(0); |
| 203 | + } catch (IOException e) { |
| 204 | + throw new IllegalStateException(e); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + public static void applyCrd(String resourceTypeName, KubernetesClient client) { |
| 209 | + try { |
| 210 | + var crd = loadCRD(resourceTypeName, client); |
| 211 | + client.resource(crd).createOr(NonDeletingOperation::update); |
186 | 212 | Thread.sleep(CRD_READY_WAIT); // readiness is not applicable for CRD, just wait a little
|
187 |
| - LOGGER.debug("Applied CRD with path: {}", path); |
| 213 | + LOGGER.debug("Applied CRD for type {}", resourceTypeName); |
188 | 214 | } catch (InterruptedException ex) {
|
189 | 215 | LOGGER.error("Interrupted.", ex);
|
190 | 216 | Thread.currentThread().interrupt();
|
191 | 217 | } catch (Exception ex) {
|
192 |
| - throw new IllegalStateException("Cannot apply CRD yaml: " + path, ex); |
| 218 | + throw new IllegalStateException("Cannot apply CRD for type: " + resourceTypeName, ex); |
193 | 219 | }
|
194 | 220 | }
|
195 | 221 |
|
|
0 commit comments