2828import java .util .HashSet ;
2929import java .util .List ;
3030import java .util .Map ;
31+ import java .util .Objects ;
3132import java .util .Set ;
33+ import java .util .concurrent .TimeUnit ;
3234import java .util .function .Consumer ;
3335import java .util .function .Function ;
3436import 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