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

chore(db): use Flyway migration to set up discovery plugins #614

Merged
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
12 changes: 0 additions & 12 deletions src/main/java/io/cryostat/discovery/ContainerDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/io/cryostat/discovery/CustomDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/io/cryostat/discovery/Discovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ public class Discovery {

@Transactional
void onStart(@Observes StartupEvent evt) {
// ensure lazily initialized entries are created
DiscoveryNode.getUniverse();

DiscoveryPlugin.<DiscoveryPlugin>findAll().list().stream()
.filter(p -> !p.builtin)
.forEach(
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/io/cryostat/discovery/DiscoveryNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ public boolean hasChildren() {

public static DiscoveryNode getUniverse() {
return DiscoveryNode.find(NODE_TYPE, BaseNodeType.UNIVERSE.getKind())
.<DiscoveryNode>singleResultOptional()
.orElseGet(
() -> environment(BaseNodeType.UNIVERSE.toString(), BaseNodeType.UNIVERSE));
.<DiscoveryNode>singleResult();
}

public static Optional<DiscoveryNode> getRealm(String name) {
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/io/cryostat/discovery/JDPDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/io/cryostat/discovery/KubeApiDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
41 changes: 40 additions & 1 deletion src/main/resources/db/migration/V4.0.0__cryostat.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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';
Loading