Skip to content

Commit

Permalink
Merge pull request quarkusio#34223 from gsmet/container-env
Browse files Browse the repository at this point in the history
Provide a way to push env variables to dev services containers
  • Loading branch information
gsmet authored Jun 27, 2023
2 parents d8d084a + 58c3748 commit 6256208
Show file tree
Hide file tree
Showing 32 changed files with 175 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class DevServicesDatasourceContainerConfig {

private final Optional<String> imageName;
private final Map<String, String> containerEnv;
private final Map<String, String> containerProperties;
private final Map<String, String> additionalJdbcUrlProperties;
private final OptionalInt fixedExposedPort;
Expand All @@ -18,6 +19,7 @@ public class DevServicesDatasourceContainerConfig {
private final Map<String, String> volumes;

public DevServicesDatasourceContainerConfig(Optional<String> imageName,
Map<String, String> containerEnv,
Map<String, String> containerProperties,
Map<String, String> additionalJdbcUrlProperties,
OptionalInt port,
Expand All @@ -28,6 +30,7 @@ public DevServicesDatasourceContainerConfig(Optional<String> imageName,
Optional<String> initScriptPath,
Map<String, String> volumes) {
this.imageName = imageName;
this.containerEnv = containerEnv;
this.containerProperties = containerProperties;
this.additionalJdbcUrlProperties = additionalJdbcUrlProperties;
this.fixedExposedPort = port;
Expand All @@ -43,6 +46,10 @@ public Optional<String> getImageName() {
return imageName;
}

public Map<String, String> getContainerEnv() {
return containerEnv;
}

public Map<String, String> getContainerProperties() {
return containerProperties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ private RunningDevService startDevDb(
try {
DevServicesDatasourceContainerConfig containerConfig = new DevServicesDatasourceContainerConfig(
dataSourceBuildTimeConfig.devservices.imageName,
dataSourceBuildTimeConfig.devservices.containerEnv,
dataSourceBuildTimeConfig.devservices.containerProperties,
dataSourceBuildTimeConfig.devservices.properties,
dataSourceBuildTimeConfig.devservices.port,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public class DevServicesBuildTimeConfig {
@ConfigItem
public Optional<String> imageName;

/**
* Environment variables that are passed to the container.
*/
@ConfigItem
public Map<String, String> containerEnv;

/**
* Generic properties that are passed for additional container configuration.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt
Labels.addDataSourceLabel(container, datasourceName);
Volumes.addVolumes(container, containerConfig.getVolumes());

container.withEnv(containerConfig.getContainerEnv());

containerConfig.getAdditionalJdbcUrlProperties().forEach(container::withUrlParam);
containerConfig.getCommand().ifPresent(container::setCommand);
containerConfig.getInitScriptPath().ifPresent(container::withInitScript);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class MariaDBDevServicesProcessor {
DevServicesDatasourceProviderBuildItem setupMariaDB(
List<DevServicesSharedNetworkBuildItem> devServicesSharedNetworkBuildItem) {
return new DevServicesDatasourceProviderBuildItem(DatabaseKind.MARIADB, new DevServicesDatasourceProvider() {
@SuppressWarnings("unchecked")
@Override
public RunningDevServicesDatasource startDatabase(Optional<String> username, Optional<String> password,
Optional<String> datasourceName, DevServicesDatasourceContainerConfig containerConfig,
Expand All @@ -56,6 +57,8 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt
Labels.addDataSourceLabel(container, datasourceName);
Volumes.addVolumes(container, containerConfig.getVolumes());

container.withEnv(containerConfig.getContainerEnv());

if (containerConfig.getContainerProperties().containsKey(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME)) {
container.withConfigurationOverride(
containerConfig.getContainerProperties().get(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ public class MSSQLDevServicesProcessor {

private static final Logger LOG = Logger.getLogger(MSSQLDevServicesProcessor.class);

/**
* Using SA doesn't work with all collations so let's use the lowercase version instead.
*/
private static final String DEFAULT_USERNAME = "sa";

@BuildStep
DevServicesDatasourceProviderBuildItem setupMSSQL(
List<DevServicesSharedNetworkBuildItem> devServicesSharedNetworkBuildItem) {
return new DevServicesDatasourceProviderBuildItem(DatabaseKind.MSSQL, new DevServicesDatasourceProvider() {
@SuppressWarnings("unchecked")
@Override
public RunningDevServicesDatasource startDatabase(Optional<String> username, Optional<String> password,
Optional<String> datasourceName, DevServicesDatasourceContainerConfig containerConfig,
Expand All @@ -49,6 +55,8 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt
Labels.addDataSourceLabel(container, datasourceName);
Volumes.addVolumes(container, containerConfig.getVolumes());

container.withEnv(containerConfig.getContainerEnv());

containerConfig.getAdditionalJdbcUrlProperties().forEach(container::withUrlParam);
containerConfig.getCommand().ifPresent(container::setCommand);
containerConfig.getInitScriptPath().ifPresent(container::withInitScript);
Expand All @@ -60,7 +68,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt
return new RunningDevServicesDatasource(container.getContainerId(),
container.getEffectiveJdbcUrl(),
container.getReactiveUrl(),
container.getUsername(),
DEFAULT_USERNAME,
container.getPassword(),
new ContainerShutdownCloseable(container, "Microsoft SQL Server"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class MySQLDevServicesProcessor {
DevServicesDatasourceProviderBuildItem setupMysql(
List<DevServicesSharedNetworkBuildItem> devServicesSharedNetworkBuildItem) {
return new DevServicesDatasourceProviderBuildItem(DatabaseKind.MYSQL, new DevServicesDatasourceProvider() {
@SuppressWarnings("unchecked")
@Override
public RunningDevServicesDatasource startDatabase(Optional<String> username, Optional<String> password,
Optional<String> datasourceName, DevServicesDatasourceContainerConfig containerConfig,
Expand All @@ -55,6 +56,8 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt
Labels.addDataSourceLabel(container, datasourceName);
Volumes.addVolumes(container, containerConfig.getVolumes());

container.withEnv(containerConfig.getContainerEnv());

if (containerConfig.getContainerProperties().containsKey(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME)) {
container.withConfigurationOverride(
containerConfig.getContainerProperties().get(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt
Labels.addDataSourceLabel(container, datasourceName);
Volumes.addVolumes(container, containerConfig.getVolumes());

container.withEnv(containerConfig.getContainerEnv());

// We need to limit the maximum amount of CPUs being used by the container;
// otherwise the hardcoded memory configuration of the DB might not be enough to successfully boot it.
// See https://github.com/gvenzl/oci-oracle-xe/issues/64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ ConsoleCommandBuildItem psqlCommand(DevServicesLauncherConfigResultBuildItem dev
DevServicesDatasourceProviderBuildItem setupPostgres(
List<DevServicesSharedNetworkBuildItem> devServicesSharedNetworkBuildItem) {
return new DevServicesDatasourceProviderBuildItem(DatabaseKind.POSTGRESQL, new DevServicesDatasourceProvider() {
@SuppressWarnings("unchecked")
@Override
public RunningDevServicesDatasource startDatabase(Optional<String> username, Optional<String> password,
Optional<String> datasourceName, DevServicesDatasourceContainerConfig containerConfig,
Expand All @@ -62,6 +63,8 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt
Labels.addDataSourceLabel(container, datasourceName);
Volumes.addVolumes(container, containerConfig.getVolumes());

container.withEnv(containerConfig.getContainerEnv());

containerConfig.getAdditionalJdbcUrlProperties().forEach(container::withUrlParam);
containerConfig.getCommand().ifPresent(container::setCommand);
containerConfig.getInitScriptPath().ifPresent(container::withInitScript);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ private DevServicesResultBuildItem.RunningDevService startElasticsearch(
container.setPortBindings(List.of(config.port.get() + ":" + config.port.get()));
}
timeout.ifPresent(container::withStartupTimeout);

container.withEnv(config.containerEnv);
container.addEnv("ES_JAVA_OPTS", config.javaOpts);
// Disable security as else we would need to configure it correctly to avoid tons of WARNING in the log
container.addEnv("xpack.security.enabled", "false");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.elasticsearch.restclient.common.deployment;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

Expand Down Expand Up @@ -67,6 +68,12 @@ public class ElasticsearchDevServicesBuildTimeConfig {
@ConfigItem(defaultValue = "elasticsearch")
public String serviceName;

/**
* Environment variables that are passed to the container.
*/
@ConfigItem
public Map<String, String> containerEnv;

@Override
public boolean equals(Object o) {
if (this == o)
Expand All @@ -79,11 +86,12 @@ public boolean equals(Object o) {
&& Objects.equals(port, that.port)
&& Objects.equals(imageName, that.imageName)
&& Objects.equals(javaOpts, that.javaOpts)
&& Objects.equals(serviceName, that.serviceName);
&& Objects.equals(serviceName, that.serviceName)
&& Objects.equals(containerEnv, that.containerEnv);
}

@Override
public int hashCode() {
return Objects.hash(enabled, port, imageName, javaOpts, shared, serviceName);
return Objects.hash(enabled, port, imageName, javaOpts, shared, serviceName, containerEnv);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ private RunningDevService startContainer(String clientName, DockerStatusBuildIte
launchMode,
useSharedNetwork);
timeout.ifPresent(infinispanContainer::withStartupTimeout);
infinispanContainer.withEnv(devServicesConfig.containerEnv);
infinispanContainer.start();

return getRunningDevService(clientName, infinispanContainer.getContainerId(), infinispanContainer::close,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.infinispan.client.runtime;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
Expand Down Expand Up @@ -110,6 +111,12 @@ public class InfinispanDevServicesConfig {
@ConfigItem(name = "tracing.exporter.otlp.endpoint", defaultValue = "http://localhost:4317")
public Optional<String> exporterOtlpEndpoint;

/**
* Environment variables that are passed to the container.
*/
@ConfigItem
public Map<String, String> containerEnv;

@Override
public boolean equals(Object o) {
if (this == o)
Expand All @@ -122,12 +129,13 @@ public boolean equals(Object o) {
Objects.equals(shared, that.shared) &&
Objects.equals(serviceName, that.serviceName) &&
Objects.equals(imageName, that.imageName) &&
Objects.equals(artifacts, this.artifacts);
Objects.equals(artifacts, this.artifacts) &&
Objects.equals(containerEnv, that.containerEnv);
}

@Override
public int hashCode() {
return Objects.hash(enabled, port, shared, serviceName, imageName, artifacts);
return Objects.hash(enabled, port, shared, serviceName, imageName, artifacts, containerEnv);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class DevServicesMsSQLDatasourceTestCase {
public void testDatasource() throws Exception {
AgroalConnectionPoolConfiguration configuration = dataSource.getConfiguration().connectionPoolConfiguration();
assertTrue(configuration.connectionFactoryConfiguration().jdbcUrl().contains("jdbc:sqlserver:"));
assertEquals("SA", configuration.connectionFactoryConfiguration().principal().getName());
assertEquals("sa", configuration.connectionFactoryConfiguration().principal().getName());
assertEquals(20, configuration.maxSize());
assertThat(configuration.exceptionSorter()).isInstanceOf(MSSQLExceptionSorter.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ private RunningDevService startKafka(DockerStatusBuildItem dockerStatusBuildItem
launchMode.getLaunchMode() == LaunchMode.DEVELOPMENT ? config.serviceName : null,
useSharedNetwork, config.redpanda);
timeout.ifPresent(redpanda::withStartupTimeout);
redpanda.withEnv(config.containerEnv);
redpanda.start();

return new RunningDevService(Feature.KAFKA_CLIENT.getName(),
Expand All @@ -252,6 +253,7 @@ private RunningDevService startKafka(DockerStatusBuildItem dockerStatusBuildItem
strimzi.withPort(config.fixedExposedPort);
}
timeout.ifPresent(strimzi::withStartupTimeout);
strimzi.withEnv(config.containerEnv);

strimzi.start();
return new RunningDevService(Feature.KAFKA_CLIENT.getName(),
Expand All @@ -264,6 +266,7 @@ private RunningDevService startKafka(DockerStatusBuildItem dockerStatusBuildItem
launchMode.getLaunchMode() == LaunchMode.DEVELOPMENT ? config.serviceName : null,
useSharedNetwork);
timeout.ifPresent(kafkaNative::withStartupTimeout);
kafkaNative.withEnv(config.containerEnv);
kafkaNative.start();

return new RunningDevService(Feature.KAFKA_CLIENT.getName(),
Expand Down Expand Up @@ -314,6 +317,7 @@ private static final class KafkaDevServiceCfg {
private final String serviceName;
private final Map<String, Integer> topicPartitions;
private final Duration topicPartitionsTimeout;
private final Map<String, String> containerEnv;

private final KafkaDevServicesBuildTimeConfig.Provider provider;

Expand All @@ -328,6 +332,7 @@ public KafkaDevServiceCfg(KafkaDevServicesBuildTimeConfig config) {
this.serviceName = config.serviceName;
this.topicPartitions = config.topicPartitions;
this.topicPartitionsTimeout = config.topicPartitionsTimeout;
this.containerEnv = config.containerEnv;

this.redpanda = config.redpanda;
}
Expand All @@ -344,12 +349,13 @@ public boolean equals(Object o) {
return devServicesEnabled == that.devServicesEnabled
&& Objects.equals(provider, that.provider)
&& Objects.equals(imageName, that.imageName)
&& Objects.equals(fixedExposedPort, that.fixedExposedPort);
&& Objects.equals(fixedExposedPort, that.fixedExposedPort)
&& Objects.equals(containerEnv, that.containerEnv);
}

@Override
public int hashCode() {
return Objects.hash(devServicesEnabled, provider, imageName, fixedExposedPort);
return Objects.hash(devServicesEnabled, provider, imageName, fixedExposedPort, containerEnv);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ public String getDefaultImageName() {
@ConfigItem(defaultValue = "2S")
public Duration topicPartitionsTimeout;

/**
* Environment variables that are passed to the container.
*/
@ConfigItem
public Map<String, String> containerEnv;

/**
* Allows configuring the Red Panda broker.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ private void shutdownCluster() {
}
}

@SuppressWarnings("unchecked")
private RunningDevService startKubernetes(DockerStatusBuildItem dockerStatusBuildItem, KubernetesDevServiceCfg config,
LaunchModeBuildItem launchMode, boolean useSharedNetwork, Optional<Duration> timeout) {
if (!config.devServicesEnabled) {
Expand Down Expand Up @@ -226,6 +227,8 @@ private RunningDevService startKubernetes(DockerStatusBuildItem dockerStatusBuil
}
timeout.ifPresent(container::withStartupTimeout);

container.withEnv(config.containerEnv);

container.start();

KubeConfig kubeConfig = KubeConfigUtils.parseKubeConfig(container.getKubeconfig());
Expand Down Expand Up @@ -291,6 +294,7 @@ private static final class KubernetesDevServiceCfg {
public boolean overrideKubeconfig;
public boolean shared;
public String serviceName;
public Map<String, String> containerEnv;

public KubernetesDevServiceCfg(KubernetesDevServicesBuildTimeConfig config) {
this.devServicesEnabled = config.enabled;
Expand All @@ -299,11 +303,12 @@ public KubernetesDevServiceCfg(KubernetesDevServicesBuildTimeConfig config) {
this.overrideKubeconfig = config.overrideKubeconfig;
this.flavor = config.flavor;
this.shared = config.shared;
this.containerEnv = config.containerEnv;
}

@Override
public int hashCode() {
return Objects.hash(devServicesEnabled, flavor, apiVersion, overrideKubeconfig, shared, serviceName);
return Objects.hash(devServicesEnabled, flavor, apiVersion, overrideKubeconfig, shared, serviceName, containerEnv);
}

@Override
Expand All @@ -315,7 +320,8 @@ public boolean equals(Object obj) {
KubernetesDevServiceCfg other = (KubernetesDevServiceCfg) obj;
return devServicesEnabled == other.devServicesEnabled && flavor == other.flavor
&& Objects.equals(apiVersion, other.apiVersion) && overrideKubeconfig == other.overrideKubeconfig
&& shared == other.shared && Objects.equals(serviceName, other.serviceName);
&& shared == other.shared && Objects.equals(serviceName, other.serviceName)
&& Objects.equals(containerEnv, other.containerEnv);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.kubernetes.client.runtime;

import java.util.Map;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigGroup;
Expand Down Expand Up @@ -66,6 +67,12 @@ public class KubernetesDevServicesBuildTimeConfig {
@ConfigItem(defaultValue = "kubernetes")
public String serviceName;

/**
* Environment variables that are passed to the container.
*/
@ConfigItem
public Map<String, String> containerEnv;

public static enum Flavor {
/**
* kind (needs priviledge docker)
Expand Down
Loading

0 comments on commit 6256208

Please sign in to comment.