From a8f2aa12b7e2d1e2f72e9e43d06447250e859301 Mon Sep 17 00:00:00 2001 From: Ioannis Canellos Date: Thu, 3 Nov 2022 10:14:12 +0200 Subject: [PATCH] feat: flyway & liquibase produce init containers --- extensions/flyway/deployment/pom.xml | 8 +++++++ .../io/quarkus/flyway/FlywayProcessor.java | 22 +++++++++++++++++-- .../flyway/runtime/FlywayRuntimeConfig.java | 9 +++++++- extensions/liquibase/deployment/pom.xml | 8 +++++++ .../deployment/LiquibaseProcessor.java | 22 +++++++++++++++++-- .../runtime/LiquibaseRuntimeConfig.java | 9 +++++++- 6 files changed, 72 insertions(+), 6 deletions(-) diff --git a/extensions/flyway/deployment/pom.xml b/extensions/flyway/deployment/pom.xml index 99592449ce68a..2a6e2d42edc08 100644 --- a/extensions/flyway/deployment/pom.xml +++ b/extensions/flyway/deployment/pom.xml @@ -29,6 +29,14 @@ io.quarkus quarkus-flyway + + io.quarkus + quarkus-container-image-spi + + + io.quarkus + quarkus-kubernetes-spi + io.quarkus quarkus-junit5-internal diff --git a/extensions/flyway/deployment/src/main/java/io/quarkus/flyway/FlywayProcessor.java b/extensions/flyway/deployment/src/main/java/io/quarkus/flyway/FlywayProcessor.java index 78f7130dc8ccd..40cf229f275f3 100644 --- a/extensions/flyway/deployment/src/main/java/io/quarkus/flyway/FlywayProcessor.java +++ b/extensions/flyway/deployment/src/main/java/io/quarkus/flyway/FlywayProcessor.java @@ -38,6 +38,7 @@ import io.quarkus.arc.deployment.SyntheticBeansRuntimeInitBuildItem; import io.quarkus.arc.processor.DotNames; import io.quarkus.builder.item.SimpleBuildItem; +import io.quarkus.container.spi.ContainerImageInfoBuildItem; import io.quarkus.datasource.common.runtime.DataSourceUtil; import io.quarkus.deployment.Feature; import io.quarkus.deployment.annotations.BuildProducer; @@ -60,6 +61,9 @@ import io.quarkus.flyway.runtime.FlywayBuildTimeConfig; import io.quarkus.flyway.runtime.FlywayContainerProducer; import io.quarkus.flyway.runtime.FlywayRecorder; +import io.quarkus.flyway.runtime.FlywayRuntimeConfig; +import io.quarkus.kubernetes.spi.KubernetesEnvBuildItem; +import io.quarkus.kubernetes.spi.KubernetesInitContainerBuildItem; import io.quarkus.runtime.util.ClassPathUtils; class FlywayProcessor { @@ -204,10 +208,14 @@ void createBeans(FlywayRecorder recorder, @Consume(SyntheticBeansRuntimeInitBuildItem.class) @Record(ExecutionTime.RUNTIME_INIT) public ServiceStartBuildItem startActions(FlywayRecorder recorder, + FlywayRuntimeConfig config, BuildProducer schemaReadyBuildItem, MigrationStateBuildItem migrationsBuildItem) { - // will actually run the actions at runtime - recorder.doStartActions(); + + if (config.enabled) { + // will actually run the actions at runtime + recorder.doStartActions(); + } // once we are done running the migrations, we produce a build item indicating that the // schema is "ready" @@ -216,6 +224,16 @@ public ServiceStartBuildItem startActions(FlywayRecorder recorder, return new ServiceStartBuildItem("flyway"); } + @BuildStep + void configureKubernetes(ContainerImageInfoBuildItem imageInfo, + BuildProducer initContainers, BuildProducer env) { + initContainers.produce(KubernetesInitContainerBuildItem.create("flyway") + .withImage(imageInfo.getImage()) + .withEnvVars(Map.of("QUARKUS_FLYWAY_RUN_AND_EXIT", "true"))); + + env.produce(KubernetesEnvBuildItem.createSimpleVar("QUARKUS_FLYWAY_ENABLED", "false", null)); + } + private Set getDataSourceNames(List jdbcDataSourceBuildItems) { Set result = new HashSet<>(jdbcDataSourceBuildItems.size()); for (JdbcDataSourceBuildItem item : jdbcDataSourceBuildItems) { diff --git a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayRuntimeConfig.java b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayRuntimeConfig.java index 966ef4afdffbd..111b37ab3d96b 100644 --- a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayRuntimeConfig.java +++ b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayRuntimeConfig.java @@ -21,6 +21,13 @@ public FlywayDataSourceRuntimeConfig getConfigForDataSourceName(String dataSourc return namedDataSources.getOrDefault(dataSourceName, FlywayDataSourceRuntimeConfig.defaultConfig()); } + /** + * Flag to enable / disable Liquibase. + * + */ + @ConfigItem(defaultValue = "true") + public boolean enabled; + /** * Flyway configuration for the default datasource. */ @@ -32,4 +39,4 @@ public FlywayDataSourceRuntimeConfig getConfigForDataSourceName(String dataSourc */ @ConfigItem(name = ConfigItem.PARENT) public Map namedDataSources = Collections.emptyMap(); -} \ No newline at end of file +} diff --git a/extensions/liquibase/deployment/pom.xml b/extensions/liquibase/deployment/pom.xml index 204165e919458..7bc9f1f3c08c9 100644 --- a/extensions/liquibase/deployment/pom.xml +++ b/extensions/liquibase/deployment/pom.xml @@ -33,6 +33,14 @@ io.quarkus quarkus-liquibase + + io.quarkus + quarkus-container-image-spi + + + io.quarkus + quarkus-kubernetes-spi + io.quarkus quarkus-junit5-internal diff --git a/extensions/liquibase/deployment/src/main/java/io/quarkus/liquibase/deployment/LiquibaseProcessor.java b/extensions/liquibase/deployment/src/main/java/io/quarkus/liquibase/deployment/LiquibaseProcessor.java index 88075fd790835..d21e679c26ce9 100644 --- a/extensions/liquibase/deployment/src/main/java/io/quarkus/liquibase/deployment/LiquibaseProcessor.java +++ b/extensions/liquibase/deployment/src/main/java/io/quarkus/liquibase/deployment/LiquibaseProcessor.java @@ -11,6 +11,7 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.function.BiConsumer; @@ -31,6 +32,7 @@ import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.arc.deployment.SyntheticBeansRuntimeInitBuildItem; import io.quarkus.arc.processor.DotNames; +import io.quarkus.container.spi.ContainerImageInfoBuildItem; import io.quarkus.datasource.common.runtime.DataSourceUtil; import io.quarkus.deployment.Feature; import io.quarkus.deployment.annotations.BuildProducer; @@ -51,11 +53,14 @@ import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem; import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild; import io.quarkus.deployment.util.ServiceUtil; +import io.quarkus.kubernetes.spi.KubernetesEnvBuildItem; +import io.quarkus.kubernetes.spi.KubernetesInitContainerBuildItem; import io.quarkus.liquibase.LiquibaseDataSource; import io.quarkus.liquibase.LiquibaseFactory; import io.quarkus.liquibase.runtime.LiquibaseBuildTimeConfig; import io.quarkus.liquibase.runtime.LiquibaseFactoryProducer; import io.quarkus.liquibase.runtime.LiquibaseRecorder; +import io.quarkus.liquibase.runtime.LiquibaseRuntimeConfig; import liquibase.change.Change; import liquibase.change.DatabaseChangeProperty; import liquibase.change.core.CreateProcedureChange; @@ -278,10 +283,13 @@ void createBeans(LiquibaseRecorder recorder, @Record(ExecutionTime.RUNTIME_INIT) @Consume(SyntheticBeansRuntimeInitBuildItem.class) ServiceStartBuildItem startLiquibase(LiquibaseRecorder recorder, + LiquibaseRuntimeConfig config, List jdbcDataSourceBuildItems, BuildProducer schemaReadyBuildItem) { - // will actually run the actions at runtime - recorder.doStartActions(); + if (config.enabled) { + // will actually run the actions at runtime + recorder.doStartActions(); + } // once we are done running the migrations, we produce a build item indicating that the // schema is "ready" @@ -290,6 +298,16 @@ ServiceStartBuildItem startLiquibase(LiquibaseRecorder recorder, return new ServiceStartBuildItem("liquibase"); } + @BuildStep + void configureKubernetes(ContainerImageInfoBuildItem imageInfo, + BuildProducer initContainers, BuildProducer env) { + initContainers.produce(KubernetesInitContainerBuildItem.create("liquibase") + .withImage(imageInfo.getImage()) + .withEnvVars(Map.of("QUARKUS_LIQUIBASE_RUN_AND_EXIT", "true"))); + + env.produce(KubernetesEnvBuildItem.createSimpleVar("QUARKUS_LIQUIBASE_ENABLED", "false", null)); + } + private Set getDataSourceNames(List jdbcDataSourceBuildItems) { Set result = new HashSet<>(jdbcDataSourceBuildItems.size()); for (JdbcDataSourceBuildItem item : jdbcDataSourceBuildItems) { diff --git a/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseRuntimeConfig.java b/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseRuntimeConfig.java index afc03399bb66e..0d9561e3ce1c8 100644 --- a/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseRuntimeConfig.java +++ b/extensions/liquibase/runtime/src/main/java/io/quarkus/liquibase/runtime/LiquibaseRuntimeConfig.java @@ -29,6 +29,13 @@ public LiquibaseDataSourceRuntimeConfig getConfigForDataSourceName(String dataSo return namedDataSources.getOrDefault(dataSourceName, LiquibaseDataSourceRuntimeConfig.defaultConfig()); } + /** + * Flag to enable / disable Liquibase. + * + */ + @ConfigItem(defaultValue = "true") + public boolean enabled; + /** * Liquibase configuration for the default datasource. */ @@ -40,4 +47,4 @@ public LiquibaseDataSourceRuntimeConfig getConfigForDataSourceName(String dataSo */ @ConfigItem(name = ConfigItem.PARENT) public Map namedDataSources = Collections.emptyMap(); -} \ No newline at end of file +}