Skip to content

Commit f9c1e9b

Browse files
committed
fix: add clean up of applied CRDs after tests
Signed-off-by: xstefank <[email protected]>
1 parent a8f08ed commit f9c1e9b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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

+25
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
import java.time.Duration;
1111
import java.util.ArrayList;
1212
import java.util.HashMap;
13+
import java.util.LinkedList;
1314
import java.util.List;
1415
import java.util.Map;
1516
import java.util.function.Consumer;
1617
import java.util.function.Function;
1718
import java.util.stream.Stream;
1819

20+
import io.fabric8.kubernetes.client.dsl.NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;
1921
import org.junit.jupiter.api.extension.ExtensionContext;
2022
import org.slf4j.Logger;
2123
import org.slf4j.LoggerFactory;
@@ -48,6 +50,7 @@ public class LocallyRunOperatorExtension extends AbstractOperatorExtension {
4850
private final List<Class<? extends CustomResource>> additionalCustomResourceDefinitions;
4951
private final Map<Reconciler, RegisteredController> registeredControllers;
5052
private final Map<String, String> crdMappings;
53+
private static final LinkedList<AppliedCRD> appliedCRDs = new LinkedList<>();
5154

5255
private LocallyRunOperatorExtension(
5356
List<ReconcilerSpec> reconcilers,
@@ -144,6 +147,7 @@ private static void applyCrd(String crdString, String path, KubernetesClient cli
144147
LOGGER.debug("Applying CRD: {}", crdString);
145148
final var crd = client.load(new ByteArrayInputStream(crdString.getBytes()));
146149
crd.serverSideApply();
150+
appliedCRDs.add(new AppliedCRD(crdString, path));
147151
Thread.sleep(CRD_READY_WAIT); // readiness is not applicable for CRD, just wait a little
148152
LOGGER.debug("Applied CRD with path: {}", path);
149153
} catch (InterruptedException ex) {
@@ -290,6 +294,14 @@ protected void before(ExtensionContext context) {
290294
protected void after(ExtensionContext context) {
291295
super.after(context);
292296

297+
var kubernetesClient = getKubernetesClient();
298+
299+
while (!appliedCRDs.isEmpty()) {
300+
deleteCrd(appliedCRDs.poll(), kubernetesClient);
301+
}
302+
303+
kubernetesClient.close();
304+
293305
try {
294306
this.operator.stop();
295307
} catch (Exception e) {
@@ -306,6 +318,19 @@ protected void after(ExtensionContext context) {
306318
localPortForwards.clear();
307319
}
308320

321+
private void deleteCrd(AppliedCRD appliedCRD, KubernetesClient client) {
322+
try {
323+
LOGGER.debug("Deleting CRD: {}", appliedCRD.crdString);
324+
final var crd = client.load(new ByteArrayInputStream(appliedCRD.crdString.getBytes()));
325+
crd.withTimeoutInMillis(50000000).delete();
326+
LOGGER.debug("Deleted CRD with path: {}", appliedCRD.path);
327+
} catch (Exception ex) {
328+
throw new IllegalStateException("Cannot delete CRD yaml: " + appliedCRD.path, ex);
329+
}
330+
}
331+
332+
private record AppliedCRD(String crdString, String path) {}
333+
309334
@SuppressWarnings("rawtypes")
310335
public static class Builder extends AbstractBuilder<Builder> {
311336
private final List<ReconcilerSpec> reconcilers;

sample-operators/tomcat-operator/src/test/java/io/javaoperatorsdk/operator/sample/TomcatOperatorE2E.java

+5
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ void test() {
117117
throw new AssertionError(ex);
118118
}
119119
});
120+
121+
log.info("Deleting test Tomcat object: {}", tomcat);
122+
tomcatClient.inNamespace(operator.getNamespace()).resource(tomcat).delete();
123+
log.info("Deleting test Webapp object: {}", webapp1);
124+
webappClient.inNamespace(operator.getNamespace()).resource(webapp1).delete();
120125
}
121126

122127
}

0 commit comments

Comments
 (0)