Skip to content

Commit

Permalink
refactor: move flyway/liquibase init to job resources
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed Dec 1, 2022
1 parent f531609 commit 7eb7ab0
Show file tree
Hide file tree
Showing 16 changed files with 598 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package io.quarkus.deployment.builditem;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import io.quarkus.builder.item.MultiBuildItem;

/**
* Represents an initalization task for the application.
* Often extension perform some short of initialization as part of the application startup.
* There are cases where we we want to externalize the initialization (e.g. in a pipeline).
*
* Often the task is run using the same artifact as the application but using a different command or
* arguments. In the later case it might be deseriable to pass addtinal environment variable to both the
* init tasks (to enable init) and the application (to disable the init).
*/
public final class InitTaskBuildItem extends MultiBuildItem {

private final String name;
private final Optional<String> image;
private final List<String> command;
private final List<String> arguments;
private final Map<String, String> taskEnvVars;
private final Map<String, String> appEnvVars;
private final boolean sharedEnvironment;
private final boolean sharedFilesystem;

public static InitTaskBuildItem create() {
return new InitTaskBuildItem("init", Optional.empty(), Collections.emptyList(), Collections.emptyList(),
Collections.emptyMap(), Collections.emptyMap(), false, false);
}

public InitTaskBuildItem(String name, Optional<String> image, List<String> command, List<String> arguments,
Map<String, String> taskEnvVars, Map<String, String> appEnvVars, boolean sharedEnvironment,
boolean sharedFilesystem) {
this.name = name;
this.image = image;
this.command = command;
this.arguments = arguments;
this.taskEnvVars = taskEnvVars;
this.appEnvVars = appEnvVars;
this.sharedEnvironment = sharedEnvironment;
this.sharedFilesystem = sharedFilesystem;
}

public String getName() {
return name;
}

public InitTaskBuildItem withName(String name) {
return new InitTaskBuildItem(name, image, command, arguments, taskEnvVars, appEnvVars, sharedEnvironment,
sharedFilesystem);
}

public Optional<String> getImage() {
return image;
}

public InitTaskBuildItem withImage(String image) {
return new InitTaskBuildItem(name, Optional.of(image), command, arguments, taskEnvVars, appEnvVars, sharedEnvironment,
sharedFilesystem);
}

public List<String> getCommand() {
return command;
}

public InitTaskBuildItem withCommand(List<String> command) {
return new InitTaskBuildItem(name, image, command, arguments, taskEnvVars, appEnvVars, sharedEnvironment,
sharedFilesystem);
}

public List<String> getArguments() {
return arguments;
}

public InitTaskBuildItem withArguments(List<String> arguments) {
return new InitTaskBuildItem(name, image, command, arguments, taskEnvVars, appEnvVars, sharedEnvironment,
sharedFilesystem);
}

public Map<String, String> getTaskEnvVars() {
return taskEnvVars;
}

public InitTaskBuildItem withTaskEnvVars(Map<String, String> taskEnvVars) {
return new InitTaskBuildItem(name, image, command, arguments, taskEnvVars, appEnvVars, sharedEnvironment,
sharedFilesystem);
}

public Map<String, String> getAppEnvVars() {
return appEnvVars;
}

public InitTaskBuildItem withAppEnvVars(Map<String, String> appEnvVars) {
return new InitTaskBuildItem(name, image, command, arguments, taskEnvVars, appEnvVars, sharedEnvironment,
sharedFilesystem);
}

public boolean isSharedEnvironment() {
return sharedEnvironment;
}

public InitTaskBuildItem withSharedEnvironment(boolean sharedEnvironment) {
return new InitTaskBuildItem(name, image, command, arguments, taskEnvVars, appEnvVars, sharedEnvironment,
sharedFilesystem);
}

public boolean isSharedFilesystem() {
return sharedFilesystem;
}

public InitTaskBuildItem withSharedFilesystem(boolean sharedFilesystem) {
return new InitTaskBuildItem(name, image, command, arguments, taskEnvVars, appEnvVars, sharedEnvironment,
sharedFilesystem);
}
}
8 changes: 0 additions & 8 deletions extensions/flyway/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-container-image-spi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-kubernetes-spi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
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;
Expand All @@ -51,6 +50,7 @@
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
import io.quarkus.deployment.builditem.IndexDependencyBuildItem;
import io.quarkus.deployment.builditem.InitTaskBuildItem;
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
Expand All @@ -62,8 +62,6 @@
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 {
Expand Down Expand Up @@ -222,15 +220,13 @@ public ServiceStartBuildItem startActions(FlywayRecorder recorder,
}

@BuildStep
void configureKubernetes(ContainerImageInfoBuildItem imageInfo,
BuildProducer<KubernetesInitContainerBuildItem> initContainers, BuildProducer<KubernetesEnvBuildItem> env) {
initContainers.produce(KubernetesInitContainerBuildItem.create("flyway")
.withImage(imageInfo.getImage())
.withEnvVars(Map.of("QUARKUS_FLYWAY_RUN_AND_EXIT", "true", "QUARKUS_FLYWAY_ENABLED", "true"))
.withInheritEnvVars(true)
.withInheritMounts(true));

env.produce(KubernetesEnvBuildItem.createSimpleVar("QUARKUS_FLYWAY_ENABLED", "false", null));
public InitTaskBuildItem configureInitTask() {
return InitTaskBuildItem.create()
.withName("flyway-init")
.withTaskEnvVars(Map.of("QUARKUS_FLYWAY_RUN_AND_EXIT", "true", "QUARKUS_FLYWAY_ENABLED", "true"))
.withAppEnvVars(Map.of("QUARKS_FLYWAY_ENABLED", "false"))
.withSharedEnvironment(true)
.withSharedFilesystem(true);
}

private Set<String> getDataSourceNames(List<JdbcDataSourceBuildItem> jdbcDataSourceBuildItems) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.quarkus.kubernetes.spi.KubernetesHealthLivenessPathBuildItem;
import io.quarkus.kubernetes.spi.KubernetesHealthReadinessPathBuildItem;
import io.quarkus.kubernetes.spi.KubernetesInitContainerBuildItem;
import io.quarkus.kubernetes.spi.KubernetesJobBuildItem;
import io.quarkus.kubernetes.spi.KubernetesLabelBuildItem;
import io.quarkus.kubernetes.spi.KubernetesPortBuildItem;
import io.quarkus.kubernetes.spi.KubernetesResourceMetadataBuildItem;
Expand Down Expand Up @@ -95,6 +96,7 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
PackageConfig packageConfig,
Optional<MetricsCapabilityBuildItem> metricsConfiguration,
List<KubernetesInitContainerBuildItem> initContainers,
List<KubernetesJobBuildItem> jobs,
List<KubernetesAnnotationBuildItem> annotations,
List<KubernetesLabelBuildItem> labels,
List<KubernetesEnvBuildItem> envs,
Expand All @@ -109,7 +111,8 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
Optional<CustomProjectRootBuildItem> customProjectRoot) {

return DevClusterHelper.createDecorators(KIND, applicationInfo, outputTarget, config, packageConfig,
metricsConfiguration, initContainers, annotations, labels, envs, baseImage, image, command, ports, livenessPath,
metricsConfiguration, initContainers, jobs, annotations, labels, envs, baseImage, image, command, ports,
livenessPath,
readinessPath,
roles, roleBindings, customProjectRoot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.quarkus.kubernetes.spi.KubernetesHealthLivenessPathBuildItem;
import io.quarkus.kubernetes.spi.KubernetesHealthReadinessPathBuildItem;
import io.quarkus.kubernetes.spi.KubernetesInitContainerBuildItem;
import io.quarkus.kubernetes.spi.KubernetesJobBuildItem;
import io.quarkus.kubernetes.spi.KubernetesLabelBuildItem;
import io.quarkus.kubernetes.spi.KubernetesPortBuildItem;
import io.quarkus.kubernetes.spi.KubernetesResourceMetadataBuildItem;
Expand Down Expand Up @@ -91,6 +92,7 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
PackageConfig packageConfig,
Optional<MetricsCapabilityBuildItem> metricsConfiguration,
List<KubernetesInitContainerBuildItem> initContainers,
List<KubernetesJobBuildItem> jobs,
List<KubernetesAnnotationBuildItem> annotations,
List<KubernetesLabelBuildItem> labels,
List<KubernetesEnvBuildItem> envs,
Expand All @@ -105,7 +107,8 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
Optional<CustomProjectRootBuildItem> customProjectRoot) {

return DevClusterHelper.createDecorators(MINIKUBE, applicationInfo, outputTarget, config, packageConfig,
metricsConfiguration, initContainers, annotations, labels, envs, baseImage, image, command, ports, livenessPath,
metricsConfiguration, initContainers, jobs, annotations, labels, envs, baseImage, image, command, ports,
livenessPath,
readinessPath,
roles, roleBindings, customProjectRoot);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package io.quarkus.kubernetes.spi;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import io.quarkus.builder.item.MultiBuildItem;

/**
* A built item for generating init containers.
* The generated container will have the specified fields
* and may optionally inherit env vars and volumes from the app container.
*
* Env vars specified through this build item, will take precedence over inherited ones.
*/
public final class KubernetesJobBuildItem extends MultiBuildItem {

private final String name;
private final String image;
private final List<String> command;
private final List<String> arguments;
private final Map<String, String> envVars;
private final boolean sharedEnvironment;
private final boolean sharedFilesystem;

public static KubernetesJobBuildItem create(String image) {
return new KubernetesJobBuildItem("init", image, Collections.emptyList(), Collections.emptyList(),
Collections.emptyMap(), false, false);
}

public KubernetesJobBuildItem(String name, String image, List<String> command, List<String> arguments,
Map<String, String> envVars, boolean sharedEnvironment, boolean sharedFilesystem) {
this.name = name;
this.image = image;
this.command = command;
this.arguments = arguments;
this.envVars = envVars;
this.sharedEnvironment = sharedEnvironment;
this.sharedFilesystem = sharedFilesystem;
}

public String getName() {
return name;
}

public KubernetesJobBuildItem withName(String name) {
return new KubernetesJobBuildItem(name, image, command, arguments, envVars, sharedEnvironment, sharedFilesystem);
}

public String getImage() {
return image;
}

public KubernetesJobBuildItem withImage(String image) {
return new KubernetesJobBuildItem(name, image, command, arguments, envVars, sharedEnvironment, sharedFilesystem);
}

public List<String> getCommand() {
return command;
}

public KubernetesJobBuildItem withCommand(List<String> command) {
return new KubernetesJobBuildItem(name, image, command, arguments, envVars, sharedEnvironment, sharedFilesystem);
}

public List<String> getArguments() {
return arguments;
}

public KubernetesJobBuildItem withArguments(List<String> arguments) {
return new KubernetesJobBuildItem(name, image, command, arguments, envVars, sharedEnvironment, sharedFilesystem);
}

public Map<String, String> getEnvVars() {
return envVars;
}

public KubernetesJobBuildItem withEnvVars(Map<String, String> envVars) {
return new KubernetesJobBuildItem(name, image, command, arguments, envVars, sharedEnvironment, sharedFilesystem);
}

public boolean isSharedEnvironment() {
return sharedEnvironment;
}

public KubernetesJobBuildItem withSharedEnvironment(boolean sharedEnvironment) {
return new KubernetesJobBuildItem(name, image, command, arguments, envVars, sharedEnvironment, sharedFilesystem);
}

public boolean isSharedFilesystem() {
return sharedFilesystem;
}

public KubernetesJobBuildItem withSharedFilesystem(boolean sharedFilesystem) {
return new KubernetesJobBuildItem(name, image, command, arguments, envVars, sharedEnvironment, sharedFilesystem);
}
}
Loading

0 comments on commit 7eb7ab0

Please sign in to comment.