From b45e8a630eb07544ff2d20ecfdb428e09cc1bcd3 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Mon, 26 Aug 2024 13:56:55 -0400 Subject: [PATCH] chore(db): use Flyway migration to set up discovery plugins (#614) --- .../discovery/ContainerDiscovery.java | 12 ------ .../cryostat/discovery/CustomDiscovery.java | 17 -------- .../java/io/cryostat/discovery/Discovery.java | 3 -- .../io/cryostat/discovery/DiscoveryNode.java | 4 +- .../io/cryostat/discovery/JDPDiscovery.java | 12 ------ .../cryostat/discovery/KubeApiDiscovery.java | 12 ------ src/main/resources/application-dev.properties | 1 - src/main/resources/application.properties | 1 + .../db/migration/V4.0.0__cryostat.sql | 41 ++++++++++++++++++- 9 files changed, 42 insertions(+), 61 deletions(-) diff --git a/src/main/java/io/cryostat/discovery/ContainerDiscovery.java b/src/main/java/io/cryostat/discovery/ContainerDiscovery.java index 7a0be8f0f..0fdc154bf 100644 --- a/src/main/java/io/cryostat/discovery/ContainerDiscovery.java +++ b/src/main/java/io/cryostat/discovery/ContainerDiscovery.java @@ -200,18 +200,6 @@ void onStart(@Observes StartupEvent evt) { return; } - DiscoveryNode universe = DiscoveryNode.getUniverse(); - if (DiscoveryNode.getRealm(getRealm()).isEmpty()) { - DiscoveryPlugin plugin = new DiscoveryPlugin(); - DiscoveryNode node = DiscoveryNode.environment(getRealm(), BaseNodeType.REALM); - plugin.realm = node; - plugin.builtin = true; - universe.children.add(node); - node.parent = universe; - plugin.persist(); - universe.persist(); - } - logger.debugv("Starting {0} client", getRealm()); queryContainers(); diff --git a/src/main/java/io/cryostat/discovery/CustomDiscovery.java b/src/main/java/io/cryostat/discovery/CustomDiscovery.java index bbcd6d26a..460b331a5 100644 --- a/src/main/java/io/cryostat/discovery/CustomDiscovery.java +++ b/src/main/java/io/cryostat/discovery/CustomDiscovery.java @@ -35,11 +35,9 @@ import io.cryostat.targets.TargetConnectionManager; import io.cryostat.util.URIUtil; -import io.quarkus.runtime.StartupEvent; import io.vertx.mutiny.core.eventbus.EventBus; import jakarta.annotation.security.RolesAllowed; import jakarta.enterprise.context.ApplicationScoped; -import jakarta.enterprise.event.Observes; import jakarta.inject.Inject; import jakarta.transaction.Transactional; import jakarta.ws.rs.Consumes; @@ -74,21 +72,6 @@ public class CustomDiscovery { @ConfigProperty(name = ConfigProperties.CONNECTIONS_FAILED_TIMEOUT) Duration timeout; - @Transactional - void onStart(@Observes StartupEvent evt) { - DiscoveryNode universe = DiscoveryNode.getUniverse(); - if (DiscoveryNode.getRealm(REALM).isEmpty()) { - DiscoveryPlugin plugin = new DiscoveryPlugin(); - DiscoveryNode node = DiscoveryNode.environment(REALM, BaseNodeType.REALM); - plugin.realm = node; - plugin.builtin = true; - universe.children.add(node); - node.parent = universe; - plugin.persist(); - universe.persist(); - } - } - @Transactional(rollbackOn = {JvmIdException.class}) @POST @Path("/api/v2/targets") diff --git a/src/main/java/io/cryostat/discovery/Discovery.java b/src/main/java/io/cryostat/discovery/Discovery.java index 62f5b5269..1eb867def 100644 --- a/src/main/java/io/cryostat/discovery/Discovery.java +++ b/src/main/java/io/cryostat/discovery/Discovery.java @@ -108,9 +108,6 @@ public class Discovery { @Transactional void onStart(@Observes StartupEvent evt) { - // ensure lazily initialized entries are created - DiscoveryNode.getUniverse(); - DiscoveryPlugin.findAll().list().stream() .filter(p -> !p.builtin) .forEach( diff --git a/src/main/java/io/cryostat/discovery/DiscoveryNode.java b/src/main/java/io/cryostat/discovery/DiscoveryNode.java index f2f229410..164292e25 100644 --- a/src/main/java/io/cryostat/discovery/DiscoveryNode.java +++ b/src/main/java/io/cryostat/discovery/DiscoveryNode.java @@ -107,9 +107,7 @@ public boolean hasChildren() { public static DiscoveryNode getUniverse() { return DiscoveryNode.find(NODE_TYPE, BaseNodeType.UNIVERSE.getKind()) - .singleResultOptional() - .orElseGet( - () -> environment(BaseNodeType.UNIVERSE.toString(), BaseNodeType.UNIVERSE)); + .singleResult(); } public static Optional getRealm(String name) { diff --git a/src/main/java/io/cryostat/discovery/JDPDiscovery.java b/src/main/java/io/cryostat/discovery/JDPDiscovery.java index ee6ccd8b9..2bc0a77d5 100644 --- a/src/main/java/io/cryostat/discovery/JDPDiscovery.java +++ b/src/main/java/io/cryostat/discovery/JDPDiscovery.java @@ -71,18 +71,6 @@ void onStart(@Observes StartupEvent evt) { return; } - DiscoveryNode universe = DiscoveryNode.getUniverse(); - if (DiscoveryNode.getRealm(REALM).isEmpty()) { - DiscoveryPlugin plugin = new DiscoveryPlugin(); - DiscoveryNode node = DiscoveryNode.environment(REALM, BaseNodeType.REALM); - plugin.realm = node; - plugin.builtin = true; - universe.children.add(node); - node.parent = universe; - plugin.persist(); - universe.persist(); - } - logger.debug("Starting JDP client"); jdp.addListener(this); try { diff --git a/src/main/java/io/cryostat/discovery/KubeApiDiscovery.java b/src/main/java/io/cryostat/discovery/KubeApiDiscovery.java index b39a33e59..bed1a0cef 100644 --- a/src/main/java/io/cryostat/discovery/KubeApiDiscovery.java +++ b/src/main/java/io/cryostat/discovery/KubeApiDiscovery.java @@ -140,18 +140,6 @@ void onStart(@Observes @Priority(1) StartupEvent evt) { return; } - DiscoveryNode universe = DiscoveryNode.getUniverse(); - if (DiscoveryNode.getRealm(REALM).isEmpty()) { - DiscoveryPlugin plugin = new DiscoveryPlugin(); - DiscoveryNode node = DiscoveryNode.environment(REALM, BaseNodeType.REALM); - plugin.realm = node; - plugin.builtin = true; - universe.children.add(node); - node.parent = universe; - plugin.persist(); - universe.persist(); - } - logger.debugv("Starting {0} client", REALM); } diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index f995e8829..986f4fcd1 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -9,7 +9,6 @@ quarkus.http.cors.access-control-allow-credentials=true # quarkus.http.cors.methods=GET,PUT,POST,PATCH,OPTIONS # quarkus.http.cors.access-control-max-age=1s -quarkus.hibernate-orm.database.generation=drop-and-create quarkus.hibernate-orm.log.sql=true quarkus.log.category."org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext".level=DEBUG diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 32889c49f..aeddd9cf3 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,7 @@ quarkus.flyway.baseline-on-migrate=true quarkus.flyway.baseline-version=4.0.0 quarkus.flyway.migrate-at-start=true +quarkus.flyway.validate-migration-naming=true quarkus.naming.enable-jndi=true cryostat.discovery.jdp.enabled=false diff --git a/src/main/resources/db/migration/V4.0.0__cryostat.sql b/src/main/resources/db/migration/V4.0.0__cryostat.sql index b6d0bfce8..d852656e8 100644 --- a/src/main/resources/db/migration/V4.0.0__cryostat.sql +++ b/src/main/resources/db/migration/V4.0.0__cryostat.sql @@ -3,7 +3,7 @@ create sequence Credential_SEQ start with 1 increment by 50; - create sequence DiscoveryNode_SEQ start with 1 increment by 50; + create sequence DiscoveryNode_SEQ start with 1 increment by 1; create sequence MatchExpression_SEQ start with 1 increment by 50; @@ -121,3 +121,42 @@ add constraint FKl0dhd7qeayg54dcoblpww6x34 foreign key (discoveryNode) references DiscoveryNode; + + + /* Insert the Universe node first explicitly to ensure it gets the first ID in sequence */ + insert into DiscoveryNode( + id, + labels, + name, + nodeType, + parentNode + ) values((select nextval('DiscoveryNode_SEQ')), '{}'::jsonb, 'Universe', 'Universe', null); + + /* Select the Universe node, then insert Realm nodes for each builtin discovery plugin with the universe as their parent */ + with universe as ( + select id from DiscoveryNode where (nodeType = 'Universe') + ) + insert into DiscoveryNode( + id, + labels, + name, + nodeType, + parentNode + ) values + ((select nextval('DiscoveryNode_SEQ')), '{}'::jsonb, 'Custom Targets', 'Realm', (select id from universe)), + ((select nextval('DiscoveryNode_SEQ')), '{}'::jsonb, 'KubernetesApi', 'Realm', (select id from universe)), + ((select nextval('DiscoveryNode_SEQ')), '{}'::jsonb, 'JDP', 'Realm', (select id from universe)), + ((select nextval('DiscoveryNode_SEQ')), '{}'::jsonb, 'Podman', 'Realm', (select id from universe)), + ((select nextval('DiscoveryNode_SEQ')), '{}'::jsonb, 'Docker', 'Realm', (select id from universe)); + + /* For each Realm node, register a corresponding plugin */ + insert into DiscoveryPlugin( + id, + builtin, + callback, + credential_id, + realm_id + ) + select gen_random_uuid(), true, null, null, DiscoveryNode.id + from DiscoveryNode + where nodeType = 'Realm';