Skip to content

Commit fce47b6

Browse files
committed
feat: allow to skip namespace deletion in junit extension
This is important for tests which are using KubeApiTest from fabric8 client. There is no namespace controller present, so namespaces cannot be deleted. Although waitForNamespaceDeletion already is a workaround, but thought might be nicer to have this more explicit. Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 429c2c5 commit fce47b6

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

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

Lines changed: 15 additions & 0 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;
@@ -201,6 +204,9 @@ protected void after(ExtensionContext context) {
201204
if (namespace != null) {
202205
if (preserveNamespaceOnError && context.getExecutionException().isPresent()) {
203206
LOGGER.info("Preserving namespace {}", namespace);
207+
} else if (skipNamespaceDeletion) {
208+
LOGGER.info("Skipping namespace deletion for {}", namespace);
209+
deleteOperator();
204210
} else {
205211
infrastructureKubernetesClient.resourceList(infrastructure).delete();
206212
deleteOperator();
@@ -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)