Skip to content

Commit f84c03e

Browse files
authored
feat: allow to skip namespace deletion in junit extension (#3178)
1 parent 2f1868d commit f84c03e

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

operator-framework-junit/src/main/java/io/javaoperatorsdk/operator/junit/AbstractOperatorExtension.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public abstract class AbstractOperatorExtension
5858
protected Duration infrastructureTimeout;
5959
protected final boolean oneNamespacePerClass;
6060
protected final boolean preserveNamespaceOnError;
61+
protected final boolean skipNamespaceDeletion;
6162
protected final boolean waitForNamespaceDeletion;
6263
protected final int namespaceDeleteTimeout = DEFAULT_NAMESPACE_DELETE_TIMEOUT;
6364
protected final Function<ExtensionContext, String> namespaceNameSupplier;
@@ -70,6 +71,7 @@ protected AbstractOperatorExtension(
7071
Duration infrastructureTimeout,
7172
boolean oneNamespacePerClass,
7273
boolean preserveNamespaceOnError,
74+
boolean skipNamespaceDeletion,
7375
boolean waitForNamespaceDeletion,
7476
KubernetesClient kubernetesClient,
7577
KubernetesClient infrastructureKubernetesClient,
@@ -85,6 +87,7 @@ protected AbstractOperatorExtension(
8587
this.infrastructureTimeout = infrastructureTimeout;
8688
this.oneNamespacePerClass = oneNamespacePerClass;
8789
this.preserveNamespaceOnError = preserveNamespaceOnError;
90+
this.skipNamespaceDeletion = skipNamespaceDeletion;
8891
this.waitForNamespaceDeletion = waitForNamespaceDeletion;
8992
this.namespaceNameSupplier = namespaceNameSupplier;
9093
this.perClassNamespaceNameSupplier = perClassNamespaceNameSupplier;
@@ -202,19 +205,22 @@ protected void after(ExtensionContext context) {
202205
if (preserveNamespaceOnError && context.getExecutionException().isPresent()) {
203206
LOGGER.info("Preserving namespace {}", namespace);
204207
} else {
208+
LOGGER.info("Deleting infrastructure resources and operator in namespace {}", namespace);
205209
infrastructureKubernetesClient.resourceList(infrastructure).delete();
206210
deleteOperator();
207-
LOGGER.info("Deleting namespace {} and stopping operator", namespace);
208-
infrastructureKubernetesClient.namespaces().withName(namespace).delete();
209-
if (waitForNamespaceDeletion) {
210-
LOGGER.info("Waiting for namespace {} to be deleted", namespace);
211-
Awaitility.await("namespace deleted")
212-
.pollInterval(50, TimeUnit.MILLISECONDS)
213-
.atMost(namespaceDeleteTimeout, TimeUnit.SECONDS)
214-
.until(
215-
() ->
216-
infrastructureKubernetesClient.namespaces().withName(namespace).get()
217-
== null);
211+
if (!skipNamespaceDeletion) {
212+
LOGGER.info("Deleting namespace {}", namespace);
213+
infrastructureKubernetesClient.namespaces().withName(namespace).delete();
214+
if (waitForNamespaceDeletion) {
215+
LOGGER.info("Waiting for namespace {} to be deleted", namespace);
216+
Awaitility.await("namespace deleted")
217+
.pollInterval(50, TimeUnit.MILLISECONDS)
218+
.atMost(namespaceDeleteTimeout, TimeUnit.SECONDS)
219+
.until(
220+
() ->
221+
infrastructureKubernetesClient.namespaces().withName(namespace).get()
222+
== null);
223+
}
218224
}
219225
}
220226
}
@@ -229,6 +235,7 @@ public abstract static class AbstractBuilder<T extends AbstractBuilder<T>> {
229235
protected final List<HasMetadata> infrastructure;
230236
protected Duration infrastructureTimeout;
231237
protected boolean preserveNamespaceOnError;
238+
protected boolean skipNamespaceDeletion;
232239
protected boolean waitForNamespaceDeletion;
233240
protected boolean oneNamespacePerClass;
234241
protected int namespaceDeleteTimeout;
@@ -245,6 +252,9 @@ protected AbstractBuilder() {
245252
this.preserveNamespaceOnError =
246253
Utils.getSystemPropertyOrEnvVar("josdk.it.preserveNamespaceOnError", false);
247254

255+
this.skipNamespaceDeletion =
256+
Utils.getSystemPropertyOrEnvVar("josdk.it.skipNamespaceDeletion", false);
257+
248258
this.waitForNamespaceDeletion =
249259
Utils.getSystemPropertyOrEnvVar("josdk.it.waitForNamespaceDeletion", true);
250260

@@ -261,6 +271,11 @@ public T preserveNamespaceOnError(boolean value) {
261271
return (T) this;
262272
}
263273

274+
public T skipNamespaceDeletion(boolean value) {
275+
this.skipNamespaceDeletion = value;
276+
return (T) this;
277+
}
278+
264279
public T waitForNamespaceDeletion(boolean value) {
265280
this.waitForNamespaceDeletion = value;
266281
return (T) this;

operator-framework-junit/src/main/java/io/javaoperatorsdk/operator/junit/ClusterDeployedOperatorExtension.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ private ClusterDeployedOperatorExtension(
5151
List<HasMetadata> infrastructure,
5252
Duration infrastructureTimeout,
5353
boolean preserveNamespaceOnError,
54+
boolean skipNamespaceDeletion,
5455
boolean waitForNamespaceDeletion,
5556
boolean oneNamespacePerClass,
5657
KubernetesClient kubernetesClient,
@@ -62,6 +63,7 @@ private ClusterDeployedOperatorExtension(
6263
infrastructureTimeout,
6364
oneNamespacePerClass,
6465
preserveNamespaceOnError,
66+
skipNamespaceDeletion,
6567
waitForNamespaceDeletion,
6668
kubernetesClient,
6769
infrastructureKubernetesClient,
@@ -189,6 +191,7 @@ public ClusterDeployedOperatorExtension build() {
189191
infrastructure,
190192
infrastructureTimeout,
191193
preserveNamespaceOnError,
194+
skipNamespaceDeletion,
192195
waitForNamespaceDeletion,
193196
oneNamespacePerClass,
194197
kubernetesClient,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ private LocallyRunOperatorExtension(
8080
List<CustomResourceDefinition> additionalCustomResourceDefinitionInstances,
8181
Duration infrastructureTimeout,
8282
boolean preserveNamespaceOnError,
83+
boolean skipNamespaceDeletion,
8384
boolean waitForNamespaceDeletion,
8485
boolean oneNamespacePerClass,
8586
KubernetesClient kubernetesClient,
@@ -94,6 +95,7 @@ private LocallyRunOperatorExtension(
9495
infrastructureTimeout,
9596
oneNamespacePerClass,
9697
preserveNamespaceOnError,
98+
skipNamespaceDeletion,
9799
waitForNamespaceDeletion,
98100
kubernetesClient,
99101
infrastructureKubernetesClient,
@@ -541,6 +543,7 @@ public LocallyRunOperatorExtension build() {
541543
additionalCustomResourceDefinitionInstances,
542544
infrastructureTimeout,
543545
preserveNamespaceOnError,
546+
skipNamespaceDeletion,
544547
waitForNamespaceDeletion,
545548
oneNamespacePerClass,
546549
kubernetesClient,

0 commit comments

Comments
 (0)