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 ;
40
42
public class LocallyRunOperatorExtension extends AbstractOperatorExtension {
41
43
42
44
private static final Logger LOGGER = LoggerFactory .getLogger (LocallyRunOperatorExtension .class );
45
+ private static final int CRD_DELETE_TIMEOUT = 1000 ;
43
46
44
47
private final Operator operator ;
45
48
private final List <ReconcilerSpec > reconcilers ;
@@ -48,6 +51,7 @@ public class LocallyRunOperatorExtension extends AbstractOperatorExtension {
48
51
private final List <Class <? extends CustomResource >> additionalCustomResourceDefinitions ;
49
52
private final Map <Reconciler , RegisteredController > registeredControllers ;
50
53
private final Map <String , String > crdMappings ;
54
+ private static final LinkedList <AppliedCRD > appliedCRDs = new LinkedList <>();
51
55
52
56
private LocallyRunOperatorExtension (
53
57
List <ReconcilerSpec > reconcilers ,
@@ -144,6 +148,7 @@ private static void applyCrd(String crdString, String path, KubernetesClient cli
144
148
LOGGER .debug ("Applying CRD: {}" , crdString );
145
149
final var crd = client .load (new ByteArrayInputStream (crdString .getBytes ()));
146
150
crd .serverSideApply ();
151
+ appliedCRDs .add (new AppliedCRD (crdString , path ));
147
152
Thread .sleep (CRD_READY_WAIT ); // readiness is not applicable for CRD, just wait a little
148
153
LOGGER .debug ("Applied CRD with path: {}" , path );
149
154
} catch (InterruptedException ex ) {
@@ -290,6 +295,14 @@ protected void before(ExtensionContext context) {
290
295
protected void after (ExtensionContext context ) {
291
296
super .after (context );
292
297
298
+ var kubernetesClient = getKubernetesClient ();
299
+
300
+ while (!appliedCRDs .isEmpty ()) {
301
+ deleteCrd (appliedCRDs .poll (), kubernetesClient );
302
+ }
303
+
304
+ kubernetesClient .close ();
305
+
293
306
try {
294
307
this .operator .stop ();
295
308
} catch (Exception e ) {
@@ -306,6 +319,19 @@ protected void after(ExtensionContext context) {
306
319
localPortForwards .clear ();
307
320
}
308
321
322
+ private void deleteCrd (AppliedCRD appliedCRD , KubernetesClient client ) {
323
+ try {
324
+ LOGGER .debug ("Deleting CRD: {}" , appliedCRD .crdString );
325
+ final var crd = client .load (new ByteArrayInputStream (appliedCRD .crdString .getBytes ()));
326
+ crd .withTimeoutInMillis (CRD_DELETE_TIMEOUT ).delete ();
327
+ LOGGER .debug ("Deleted CRD with path: {}" , appliedCRD .path );
328
+ } catch (Exception ex ) {
329
+ throw new IllegalStateException ("Cannot delete CRD yaml: " + appliedCRD .path , ex );
330
+ }
331
+ }
332
+
333
+ private record AppliedCRD (String crdString , String path ) {}
334
+
309
335
@ SuppressWarnings ("rawtypes" )
310
336
public static class Builder extends AbstractBuilder <Builder > {
311
337
private final List <ReconcilerSpec > reconcilers ;
0 commit comments