-
-
Notifications
You must be signed in to change notification settings - Fork 35
Support reuse of dev containers #1861
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,7 @@ | |
| private AutoCloseable postgresContainer; | ||
| private AutoCloseable kafkaContainer; | ||
| private AutoCloseable frontendContainer; | ||
| private boolean isContainerReuseEnabled; | ||
|
|
||
| @Override | ||
| public void contextInitialized(final ServletContextEvent event) { | ||
|
|
@@ -72,6 +73,8 @@ | |
| throw new IllegalStateException("Dev services are not available for production builds"); | ||
| } | ||
|
|
||
| isContainerReuseEnabled = config.getValue("dev.services.container-reuse-enabled", boolean.class); | ||
|
|
||
| // Infer database port and name from the JDBC URL of the primary data source. | ||
| final URI defaultDataSourceUri = URI.create( | ||
| config.getValue("dt.datasource.default.url", String.class).replaceFirst("^jdbc:", "").split("\\?", 2)[0]); | ||
|
|
@@ -103,6 +106,8 @@ | |
| postgresContainerClass.getMethod("withPassword", String.class).invoke(postgresContainer, postgresPassword); | ||
| postgresContainerClass.getMethod("withDatabaseName", String.class).invoke(postgresContainer, postgresDatabase); | ||
| postgresContainerClass.getMethod("withUrlParam", String.class, String.class).invoke(postgresContainer, "reWriteBatchedInserts", "true"); | ||
| postgresContainerClass.getMethod("withLabel", String.class, String.class).invoke(postgresContainer, "owner", "hyades-apiserver-dev"); | ||
| postgresContainerClass.getMethod("withReuse", boolean.class).invoke(postgresContainer, isContainerReuseEnabled); | ||
| addFixedExposedPortMethod.invoke(postgresContainer, /* hostPort */ postgresPort, /* containerPort */ 5432); | ||
|
|
||
| // TODO: Detect when Apache Kafka is requested vs. when Kafka is requested, | ||
|
|
@@ -114,12 +119,16 @@ | |
| // * https://github.com/testcontainers/testcontainers-java/issues/9506#issuecomment-2463504967 | ||
| // * https://issues.apache.org/jira/browse/KAFKA-18281 | ||
| kafkaContainerClass.getMethod("withEnv", String.class, String.class).invoke(kafkaContainer, "KAFKA_LISTENERS", "PLAINTEXT://:9092,BROKER://:9093,CONTROLLER://:9094"); | ||
| kafkaContainerClass.getMethod("withLabel", String.class, String.class).invoke(kafkaContainer, "owner", "hyades-apiserver-dev"); | ||
| kafkaContainerClass.getMethod("withReuse", boolean.class).invoke(kafkaContainer, isContainerReuseEnabled); | ||
| addFixedExposedPortMethod.invoke(kafkaContainer, /* hostPort */ kafkaPort, /* containerPort */ 9092); | ||
|
|
||
| final Constructor<?> genericContainerConstructor = genericContainerClass.getDeclaredConstructor(String.class); | ||
| frontendContainer = (AutoCloseable) genericContainerConstructor.newInstance(config.getValue(DEV_SERVICES_IMAGE_FRONTEND.getPropertyName(), String.class)); | ||
| genericContainerClass.getMethod("withEnv", String.class, String.class).invoke(frontendContainer, "API_BASE_URL", "http://localhost:8080"); | ||
| genericContainerClass.getMethod("withExposedPorts", Integer[].class).invoke(frontendContainer, (Object) new Integer[]{8080}); | ||
| genericContainerClass.getMethod("withLabel", String.class, String.class).invoke(frontendContainer, "owner", "hyades-apiserver-dev"); | ||
| genericContainerClass.getMethod("withReuse", boolean.class).invoke(frontendContainer, isContainerReuseEnabled); | ||
| addFixedExposedPortMethod.invoke(frontendContainer, /* hostPort */ frontendPort, /* containerPort */ 8080); | ||
| if (config.getValue(DEV_SERVICES_IMAGE_FRONTEND.getPropertyName(), String.class).endsWith(":snapshot")) { | ||
| genericContainerClass.getMethod("withImagePullPolicy", imagePullPolicyClass).invoke(frontendContainer, alwaysPullPolicy); | ||
|
|
@@ -161,7 +170,10 @@ | |
| LOGGER.info("Creating topics: %s".formatted(topicsToCreate)); | ||
| adminClient.createTopics(topicsToCreate).all().get(); | ||
| } catch (ExecutionException | InterruptedException e) { | ||
| throw new RuntimeException("Failed to create topics", e); | ||
| if (e.getCause() == null | ||
| || !"TopicExistsException".equals(e.getCause().getClass().getSimpleName())) { | ||
| throw new RuntimeException("Failed to create topics", e); | ||
|
Check warning on line 175 in apiserver/src/main/java/org/dependencytrack/dev/DevServicesInitializer.java
|
||
| } | ||
| } | ||
|
|
||
| LOGGER.info("PostgreSQL is listening at localhost:%d".formatted(postgresPort)); | ||
|
|
@@ -171,23 +183,23 @@ | |
|
|
||
| @Override | ||
| public void contextDestroyed(final ServletContextEvent event) { | ||
| if (postgresContainer != null) { | ||
| if (postgresContainer != null && !isContainerReuseEnabled) { | ||
| LOGGER.info("Stopping postgres container"); | ||
| try { | ||
| postgresContainer.close(); | ||
| } catch (Exception e) { | ||
| LOGGER.error("Failed to stop PostgreSQL container", e); | ||
| } | ||
| } | ||
| if (kafkaContainer != null) { | ||
| if (kafkaContainer != null && !isContainerReuseEnabled) { | ||
| LOGGER.info("Stopping Kafka container"); | ||
| try { | ||
| kafkaContainer.close(); | ||
| } catch (Exception e) { | ||
| LOGGER.error("Failed to stop Kafka container", e); | ||
| } | ||
| } | ||
| if (frontendContainer != null) { | ||
| if (frontendContainer != null && !isContainerReuseEnabled) { | ||
| LOGGER.info("Stopping frontend container"); | ||
|
Comment on lines
185
to
203
|
||
| try { | ||
| frontendContainer.close(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1395,6 +1395,14 @@ init.and.exit=false | |
| # @type: boolean | ||
| dev.services.enabled=false | ||
|
|
||
| # Defines whether dev services containers should be reused. | ||
| # <br/><br/> | ||
| # Refer to <https://java.testcontainers.org/features/reuse/> for details. | ||
| # | ||
| # @category: Development | ||
| # @type: boolean | ||
| dev.services.container-reuse-enabled=true | ||
|
Comment on lines
+1398
to
+1404
|
||
|
|
||
| # The image to use for the frontend dev services container. | ||
| # | ||
| # @category: Development | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The TopicExists handling is based on comparing
e.getCause().getClass().getSimpleName()to a string. This is brittle and can misclassify unrelated exceptions with the same simple name; it also doesn’t preserve the thread interrupt status when anInterruptedExceptionis caught. Prefer checking the actual Kafka exception type (e.g.org.apache.kafka.common.errors.TopicExistsException, accounting for potential wrapping) and, forInterruptedException, re-interrupt the thread before propagating/handling it.