Skip to content

Commit e484cc8

Browse files
committed
fix: allow keeping deleted CRDs in test with configuration
Signed-off-by: xstefank <[email protected]>
1 parent f5f0a60 commit e484cc8

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

operator-framework-junit5/src/main/java/io/javaoperatorsdk/operator/junit/LocallyRunOperatorExtension.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import java.nio.file.Files;
99
import java.nio.file.Path;
1010
import java.time.Duration;
11+
import java.util.ArrayDeque;
1112
import java.util.ArrayList;
13+
import java.util.Deque;
1214
import java.util.HashMap;
1315
import java.util.LinkedList;
1416
import java.util.List;
@@ -43,6 +45,9 @@ public class LocallyRunOperatorExtension extends AbstractOperatorExtension {
4345

4446
private static final Logger LOGGER = LoggerFactory.getLogger(LocallyRunOperatorExtension.class);
4547
private static final int CRD_DELETE_TIMEOUT = 1000;
48+
// Use Deque so we can keep deletion in LIFO order (stack)
49+
private static final Deque<AppliedCRD> appliedCRDs = new ArrayDeque<>();
50+
private static final boolean deleteCRDs = Boolean.parseBoolean(System.getProperty("testsuite.deleteCRDs", "true"));
4651

4752
private final Operator operator;
4853
private final List<ReconcilerSpec> reconcilers;
@@ -51,7 +56,6 @@ public class LocallyRunOperatorExtension extends AbstractOperatorExtension {
5156
private final List<Class<? extends CustomResource>> additionalCustomResourceDefinitions;
5257
private final Map<Reconciler, RegisteredController> registeredControllers;
5358
private final Map<String, String> crdMappings;
54-
private static final LinkedList<AppliedCRD> appliedCRDs = new LinkedList<>();
5559

5660
private LocallyRunOperatorExtension(
5761
List<ReconcilerSpec> reconcilers,
@@ -148,7 +152,7 @@ private static void applyCrd(String crdString, String path, KubernetesClient cli
148152
LOGGER.debug("Applying CRD: {}", crdString);
149153
final var crd = client.load(new ByteArrayInputStream(crdString.getBytes()));
150154
crd.serverSideApply();
151-
appliedCRDs.add(new AppliedCRD(crdString, path));
155+
appliedCRDs.push(new AppliedCRD(crdString, path));
152156
Thread.sleep(CRD_READY_WAIT); // readiness is not applicable for CRD, just wait a little
153157
LOGGER.debug("Applied CRD with path: {}", path);
154158
} catch (InterruptedException ex) {
@@ -298,7 +302,7 @@ protected void after(ExtensionContext context) {
298302
var kubernetesClient = getKubernetesClient();
299303

300304
while (!appliedCRDs.isEmpty()) {
301-
deleteCrd(appliedCRDs.poll(), kubernetesClient);
305+
deleteCrd(appliedCRDs.pop(), kubernetesClient);
302306
}
303307

304308
kubernetesClient.close();
@@ -320,6 +324,10 @@ protected void after(ExtensionContext context) {
320324
}
321325

322326
private void deleteCrd(AppliedCRD appliedCRD, KubernetesClient client) {
327+
if (!deleteCRDs) {
328+
LOGGER.debug("Skipping deleting CRD because of configuration: {}", appliedCRD);
329+
return;
330+
}
323331
try {
324332
LOGGER.debug("Deleting CRD: {}", appliedCRD.crdString);
325333
final var crd = client.load(new ByteArrayInputStream(appliedCRD.crdString.getBytes()));

0 commit comments

Comments
 (0)