Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add clean up of applied CRDs after tests #2685

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

import io.fabric8.kubernetes.client.dsl.NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -40,6 +42,7 @@
public class LocallyRunOperatorExtension extends AbstractOperatorExtension {

private static final Logger LOGGER = LoggerFactory.getLogger(LocallyRunOperatorExtension.class);
private static final int CRD_DELETE_TIMEOUT = 1000;

private final Operator operator;
private final List<ReconcilerSpec> reconcilers;
Expand All @@ -48,6 +51,7 @@ public class LocallyRunOperatorExtension extends AbstractOperatorExtension {
private final List<Class<? extends CustomResource>> additionalCustomResourceDefinitions;
private final Map<Reconciler, RegisteredController> registeredControllers;
private final Map<String, String> crdMappings;
private static final LinkedList<AppliedCRD> appliedCRDs = new LinkedList<>();

private LocallyRunOperatorExtension(
List<ReconcilerSpec> reconcilers,
Expand Down Expand Up @@ -144,6 +148,7 @@ private static void applyCrd(String crdString, String path, KubernetesClient cli
LOGGER.debug("Applying CRD: {}", crdString);
final var crd = client.load(new ByteArrayInputStream(crdString.getBytes()));
crd.serverSideApply();
appliedCRDs.add(new AppliedCRD(crdString, path));
Thread.sleep(CRD_READY_WAIT); // readiness is not applicable for CRD, just wait a little
LOGGER.debug("Applied CRD with path: {}", path);
} catch (InterruptedException ex) {
Expand Down Expand Up @@ -290,6 +295,14 @@ protected void before(ExtensionContext context) {
protected void after(ExtensionContext context) {
super.after(context);

var kubernetesClient = getKubernetesClient();

while (!appliedCRDs.isEmpty()) {
deleteCrd(appliedCRDs.poll(), kubernetesClient);
}

kubernetesClient.close();

try {
this.operator.stop();
} catch (Exception e) {
Expand All @@ -306,6 +319,19 @@ protected void after(ExtensionContext context) {
localPortForwards.clear();
}

private void deleteCrd(AppliedCRD appliedCRD, KubernetesClient client) {
try {
LOGGER.debug("Deleting CRD: {}", appliedCRD.crdString);
final var crd = client.load(new ByteArrayInputStream(appliedCRD.crdString.getBytes()));
crd.withTimeoutInMillis(CRD_DELETE_TIMEOUT).delete();
LOGGER.debug("Deleted CRD with path: {}", appliedCRD.path);
} catch (Exception ex) {
throw new IllegalStateException("Cannot delete CRD yaml: " + appliedCRD.path, ex);
}
}

private record AppliedCRD(String crdString, String path) {}

@SuppressWarnings("rawtypes")
public static class Builder extends AbstractBuilder<Builder> {
private final List<ReconcilerSpec> reconcilers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ void test() {
throw new AssertionError(ex);
}
});

log.info("Deleting test Tomcat object: {}", tomcat);
tomcatClient.inNamespace(operator.getNamespace()).resource(tomcat).delete();
log.info("Deleting test Webapp object: {}", webapp1);
webappClient.inNamespace(operator.getNamespace()).resource(webapp1).delete();
}

}