Skip to content

Commit

Permalink
chore: Add setting to enable/disable Kubedock
Browse files Browse the repository at this point in the history
- Setting allows to explicitly disable Kubedock
- When running YAKS locally with Testcontainers Kubedock may be disabled
  • Loading branch information
christophd committed May 25, 2023
1 parent 3877b2e commit 65201ef
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private void setConnectionSettings(KafkaContainer kafkaContainer, TestContext co
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_PORT", String.valueOf(kafkaContainer.getMappedPort(KafkaContainer.KAFKA_PORT)));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_LOCAL_BOOTSTRAP_SERVERS", kafkaContainer.getBootstrapServers());

if (YaksSettings.isLocal()) {
if (YaksSettings.isLocal() || !TestContainersSettings.isKubedockEnabled()) {
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_SERVICE_NAME", "kafka");
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_BOOTSTRAP_SERVERS", kafkaContainer.getBootstrapServers());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ private void exposeConnectionSettings(AWS2Container aws2Container, TestContext c
context.setVariable(getEnvVarName("ACCESS_KEY"), aws2Container.getAccessKey());
context.setVariable(getEnvVarName("SECRET_KEY"), aws2Container.getSecretKey());
context.setVariable(getEnvVarName("SERVICE_PORT"), serviceEndpoint.getPort());
context.setVariable(getEnvVarName("SERVICE_LOCAL_URL"), String.format("http://%s:%s", aws2Container.getHostIpAddress(), serviceEndpoint.getPort()));
context.setVariable(getEnvVarName("SERVICE_LOCAL_URL"), String.format("http://localhost:%s", serviceEndpoint.getPort()));

if (YaksSettings.isLocal()) {
if (YaksSettings.isLocal() || !TestContainersSettings.isKubedockEnabled()) {
context.setVariable(getEnvVarName("SERVICE_NAME"), "localstack");
context.setVariable(getEnvVarName("SERVICE_URL"), String.format("http://%s:%s", aws2Container.getHostIpAddress(), serviceEndpoint.getPort()));
} else {
Expand All @@ -159,24 +159,21 @@ private void exposeConnectionSettings(AWS2Container aws2Container, TestContext c
services.forEach(service -> {
String serviceName = service.getServiceName().toUpperCase(Locale.US);

if (YaksSettings.isLocal()) {
if (YaksSettings.isLocal() || !TestContainersSettings.isKubedockEnabled()) {
context.setVariable(getEnvVarName(String.format("%s_URL", serviceName)), String.format("http://%s:%s", aws2Container.getHostIpAddress(), serviceEndpoint.getPort()));
} else {
if (service == AWS2Container.AWS2Service.S3) {
// Explicitly use cluster IP address in order to enable path-style access on S3 service
Optional<String> clusterIp = KubernetesSupport.getServiceClusterIp(citrus, String.format("kd-%s", containerId), getNamespace(context));
if (clusterIp.isPresent()) {
context.setVariable(getEnvVarName(String.format("%s_URL", serviceName)), String.format("http://%s:%s", clusterIp.get(), serviceEndpoint.getPort()));
} else {
context.setVariable(getEnvVarName(String.format("%s_URL", serviceName)), String.format("http://kd-%s:%s", containerId, serviceEndpoint.getPort()));
}
} else if (service == AWS2Container.AWS2Service.S3) {
// Explicitly use cluster IP address in order to enable path-style access on S3 service
Optional<String> clusterIp = KubernetesSupport.getServiceClusterIp(citrus, String.format("kd-%s", containerId), getNamespace(context));
if (clusterIp.isPresent()) {
context.setVariable(getEnvVarName(String.format("%s_URL", serviceName)), String.format("http://%s:%s", clusterIp.get(), serviceEndpoint.getPort()));
} else {
context.setVariable(getEnvVarName(String.format("%s_URL", serviceName)), String.format("http://kd-%s:%s", containerId, serviceEndpoint.getPort()));
}

} else {
context.setVariable(getEnvVarName(String.format("%s_URL", serviceName)), String.format("http://kd-%s:%s", containerId, serviceEndpoint.getPort()));
}

context.setVariable(getEnvVarName(String.format("%s_LOCAL_URL", serviceName)), String.format("http://%s:%s", aws2Container.getHostIpAddress(), serviceEndpoint.getPort()));
context.setVariable(getEnvVarName(String.format("%s_LOCAL_URL", serviceName)), String.format("http://localhost:%s", serviceEndpoint.getPort()));
context.setVariable(getEnvVarName(String.format("%s_PORT", serviceName)), serviceEndpoint.getPort());
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private void setConnectionSettings(MongoDBContainer mongoDBContainer, TestContex
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_SERVICE_LOCAL_URL", mongoDBContainer.getReplicaSetUrl());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_LOCAL_URL", mongoDBContainer.getReplicaSetUrl());

if (YaksSettings.isLocal()) {
if (YaksSettings.isLocal() || !TestContainersSettings.isKubedockEnabled()) {
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_SERVICE_NAME", "mongodb");
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_SERVICE_URL", mongoDBContainer.getReplicaSetUrl());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_URL", mongoDBContainer.getReplicaSetUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private void setConnectionSettings(PostgreSQLContainer<?> postgreSQLContainer, T
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_SERVICE_LOCAL_URL", postgreSQLContainer.getJdbcUrl());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_LOCAL_URL", postgreSQLContainer.getJdbcUrl());

if (YaksSettings.isLocal()) {
if (YaksSettings.isLocal() || !TestContainersSettings.isKubedockEnabled()) {
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_SERVICE_NAME", "postgresql");
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_SERVICE_URL", postgreSQLContainer.getJdbcUrl());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_URL", postgreSQLContainer.getJdbcUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private void setConnectionSettings(RedpandaContainer redpandaContainer, TestCont
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "REDPANDA_PORT", String.valueOf(redpandaContainer.getMappedPort(REDPANDA_PORT)));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "REDPANDA_LOCAL_BOOTSTRAP_SERVERS", redpandaContainer.getBootstrapServers());

if (YaksSettings.isLocal()) {
if (YaksSettings.isLocal() || !TestContainersSettings.isKubedockEnabled()) {
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "REDPANDA_SERVICE_NAME", "redpanda");
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "REDPANDA_BOOTSTRAP_SERVERS", redpandaContainer.getBootstrapServers());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.util.Optional;

import org.citrusframework.yaks.YaksSettings;

/**
* @author Christoph Deppisch
*/
Expand All @@ -31,6 +33,9 @@ public class TestContainersSettings {
private static final String AUTO_REMOVE_RESOURCES_ENV = TESTCONTAINERS_ENV_PREFIX + "AUTO_REMOVE_RESOURCES";
private static final String AUTO_REMOVE_RESOURCES_DEFAULT = "true";

private static final String KUBEDOCK_ENABLED_PROPERTY = TESTCONTAINERS_PROPERTY_PREFIX + "kubedock.enabled";
private static final String KUBEDOCK_ENABLED_ENV = TESTCONTAINERS_ENV_PREFIX + "KUBEDOCK_ENABLED";

private static final String TEST_ID_PROPERTY = "yaks.test.id";
private static final String TEST_ID_ENV = "YAKS_TEST_ID";
private static final String TEST_ID_DEFAULT = "yaks-test";
Expand All @@ -53,6 +58,15 @@ public static boolean isAutoRemoveResources() {
System.getenv(AUTO_REMOVE_RESOURCES_ENV) != null ? System.getenv(AUTO_REMOVE_RESOURCES_ENV) : AUTO_REMOVE_RESOURCES_DEFAULT));
}

/**
* True when using KubeDock services.
* @return
*/
public static boolean isKubedockEnabled() {
return Boolean.parseBoolean(System.getProperty(KUBEDOCK_ENABLED_PROPERTY,
Optional.ofNullable(System.getenv(KUBEDOCK_ENABLED_ENV)).orElseGet(() -> String.valueOf(!YaksSettings.isLocal()))));
}

/**
* Current test id that is also set as label on the Pod running the test.
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import software.amazon.awssdk.services.kinesis.KinesisClient

KinesisClient kinesisClient = KinesisClient
.builder()
.endpointOverride(URI.create("${YAKS_TESTCONTAINERS_LOCALSTACK_KINESIS_LOCAL_URL}"))
.endpointOverride(URI.create("${YAKS_TESTCONTAINERS_LOCALSTACK_SERVICE_URL}"))
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(
"${YAKS_TESTCONTAINERS_LOCALSTACK_ACCESS_KEY}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import software.amazon.awssdk.services.s3.S3Client

S3Client s3 = S3Client
.builder()
.endpointOverride(URI.create("${YAKS_TESTCONTAINERS_LOCALSTACK_S3_LOCAL_URL}"))
.endpointOverride(URI.create("${YAKS_TESTCONTAINERS_LOCALSTACK_SERVICE_URL}"))
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(
"${YAKS_TESTCONTAINERS_LOCALSTACK_ACCESS_KEY}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
#

citrus.default.message.type=PLAINTEXT

yaks.testcontainers.kubedock.enabled=false

0 comments on commit 65201ef

Please sign in to comment.