|  | 
| 10 | 10 | import java.time.Duration; | 
| 11 | 11 | import java.util.ArrayList; | 
| 12 | 12 | import java.util.HashMap; | 
|  | 13 | +import java.util.LinkedList; | 
| 13 | 14 | import java.util.List; | 
| 14 | 15 | import java.util.Map; | 
| 15 | 16 | import java.util.function.Consumer; | 
| 16 | 17 | import java.util.function.Function; | 
| 17 | 18 | import java.util.stream.Stream; | 
| 18 | 19 | 
 | 
|  | 20 | +import io.fabric8.kubernetes.client.dsl.NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable; | 
| 19 | 21 | import org.junit.jupiter.api.extension.ExtensionContext; | 
| 20 | 22 | import org.slf4j.Logger; | 
| 21 | 23 | import org.slf4j.LoggerFactory; | 
| @@ -48,6 +50,7 @@ public class LocallyRunOperatorExtension extends AbstractOperatorExtension { | 
| 48 | 50 |   private final List<Class<? extends CustomResource>> additionalCustomResourceDefinitions; | 
| 49 | 51 |   private final Map<Reconciler, RegisteredController> registeredControllers; | 
| 50 | 52 |   private final Map<String, String> crdMappings; | 
|  | 53 | +  private static final LinkedList<AppliedCRD> appliedCRDs = new LinkedList<>(); | 
| 51 | 54 | 
 | 
| 52 | 55 |   private LocallyRunOperatorExtension( | 
| 53 | 56 |       List<ReconcilerSpec> reconcilers, | 
| @@ -144,6 +147,7 @@ private static void applyCrd(String crdString, String path, KubernetesClient cli | 
| 144 | 147 |       LOGGER.debug("Applying CRD: {}", crdString); | 
| 145 | 148 |       final var crd = client.load(new ByteArrayInputStream(crdString.getBytes())); | 
| 146 | 149 |       crd.serverSideApply(); | 
|  | 150 | +      appliedCRDs.add(new AppliedCRD(crdString, path)); | 
| 147 | 151 |       Thread.sleep(CRD_READY_WAIT); // readiness is not applicable for CRD, just wait a little | 
| 148 | 152 |       LOGGER.debug("Applied CRD with path: {}", path); | 
| 149 | 153 |     } catch (InterruptedException ex) { | 
| @@ -304,8 +308,27 @@ protected void after(ExtensionContext context) { | 
| 304 | 308 |       } | 
| 305 | 309 |     } | 
| 306 | 310 |     localPortForwards.clear(); | 
|  | 311 | + | 
|  | 312 | +    var kubernetesClient = getKubernetesClient(); | 
|  | 313 | + | 
|  | 314 | +    while (!appliedCRDs.isEmpty()) { | 
|  | 315 | +      deleteCrd(appliedCRDs.poll(), kubernetesClient); | 
|  | 316 | +    } | 
| 307 | 317 |   } | 
| 308 | 318 | 
 | 
|  | 319 | +  private void deleteCrd(AppliedCRD appliedCRD, KubernetesClient client) { | 
|  | 320 | +    try { | 
|  | 321 | +      LOGGER.debug("Deleting CRD: {}", appliedCRD.crdString); | 
|  | 322 | +      final var crd = client.load(new ByteArrayInputStream(appliedCRD.crdString.getBytes())); | 
|  | 323 | +      crd.withTimeoutInMillis(1000).delete(); | 
|  | 324 | +      LOGGER.debug("Deleted CRD with path: {}", appliedCRD.path); | 
|  | 325 | +    } catch (Exception ex) { | 
|  | 326 | +      throw new IllegalStateException("Cannot delete CRD yaml: " + appliedCRD.path, ex); | 
|  | 327 | +    } | 
|  | 328 | +  } | 
|  | 329 | + | 
|  | 330 | +  private record AppliedCRD(String crdString, String path) {} | 
|  | 331 | + | 
| 309 | 332 |   @SuppressWarnings("rawtypes") | 
| 310 | 333 |   public static class Builder extends AbstractBuilder<Builder> { | 
| 311 | 334 |     private final List<ReconcilerSpec> reconcilers; | 
|  | 
0 commit comments