Skip to content

Commit 310c540

Browse files
committed
fix: wait for the CRD deletion
Signed-off-by: xstefank <xstefank122@gmail.com>
1 parent 4d03e79 commit 310c540

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

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

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import java.util.HashSet;
2929
import java.util.List;
3030
import java.util.Map;
31+
import java.util.Objects;
3132
import java.util.Set;
33+
import java.util.concurrent.TimeUnit;
3234
import java.util.function.Consumer;
3335
import java.util.function.Function;
3436
import java.util.stream.Stream;
@@ -59,6 +61,7 @@ public class LocallyRunOperatorExtension extends AbstractOperatorExtension {
5961

6062
private static final Logger LOGGER = LoggerFactory.getLogger(LocallyRunOperatorExtension.class);
6163
private static final int CRD_DELETE_TIMEOUT = 5000;
64+
private static final int CRD_DELETE_WAIT_TIMEOUT = 60000;
6265
private static final Set<AppliedCRD> appliedCRDs = new HashSet<>();
6366
private static final boolean deleteCRDs =
6467
Boolean.parseBoolean(System.getProperty("testsuite.deleteCRDs", "true"));
@@ -426,8 +429,26 @@ record FileCRD(String crdString, String path) implements AppliedCRD {
426429
public void delete(KubernetesClient client) {
427430
try {
428431
LOGGER.debug("Deleting CRD: {}", crdString);
429-
final var crd = client.load(new ByteArrayInputStream(crdString.getBytes()));
430-
crd.withTimeoutInMillis(CRD_DELETE_TIMEOUT).delete();
432+
final var items =
433+
client.load(new ByteArrayInputStream(crdString.getBytes())).items();
434+
if (items == null || items.isEmpty() || items.get(0) == null) {
435+
LOGGER.warn("Could not determine CRD name from yaml: {}", path);
436+
return;
437+
}
438+
final var crdName = items.get(0).getMetadata().getName();
439+
client
440+
.apiextensions()
441+
.v1()
442+
.customResourceDefinitions()
443+
.withName(crdName)
444+
.withTimeoutInMillis(CRD_DELETE_TIMEOUT)
445+
.delete();
446+
client
447+
.apiextensions()
448+
.v1()
449+
.customResourceDefinitions()
450+
.withName(crdName)
451+
.waitUntilCondition(Objects::isNull, CRD_DELETE_WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
431452
LOGGER.debug("Deleted CRD with path: {}", path);
432453
} catch (Exception ex) {
433454
LOGGER.warn(
@@ -440,17 +461,28 @@ record InstanceCRD(CustomResourceDefinition customResourceDefinition) implements
440461

441462
@Override
442463
public void delete(KubernetesClient client) {
443-
String type = customResourceDefinition.getMetadata().getName();
464+
String crdName = customResourceDefinition.getMetadata().getName();
444465
try {
445-
LOGGER.debug("Deleting CustomResourceDefinition instance CRD: {}", type);
446-
final var crd = client.resource(customResourceDefinition);
447-
crd.withTimeoutInMillis(CRD_DELETE_TIMEOUT).delete();
448-
LOGGER.debug("Deleted CustomResourceDefinition instance CRD: {}", type);
466+
LOGGER.debug("Deleting CustomResourceDefinition instance CRD: {}", crdName);
467+
client
468+
.apiextensions()
469+
.v1()
470+
.customResourceDefinitions()
471+
.withName(crdName)
472+
.withTimeoutInMillis(CRD_DELETE_TIMEOUT)
473+
.delete();
474+
client
475+
.apiextensions()
476+
.v1()
477+
.customResourceDefinitions()
478+
.withName(crdName)
479+
.waitUntilCondition(Objects::isNull, CRD_DELETE_WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
480+
LOGGER.debug("Deleted CustomResourceDefinition instance CRD: {}", crdName);
449481
} catch (Exception ex) {
450482
LOGGER.warn(
451483
"Cannot delete CustomResourceDefinition instance CRD: {}. You might need to delete it"
452484
+ " manually.",
453-
type,
485+
crdName,
454486
ex);
455487
}
456488
}

0 commit comments

Comments
 (0)