Skip to content

Enable the specification of "platform" when configuring localstack container #50

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

Merged
merged 6 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/cloud/localstack/Localstack.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public void startup(LocalstackDockerConfiguration dockerConfiguration) {
dockerConfiguration.getPortElasticSearch(),
dockerConfiguration.getEnvironmentVariables(),
dockerConfiguration.getPortMappings(),
dockerConfiguration.getBindMounts()
dockerConfiguration.getBindMounts(),
dockerConfiguration.getPlatform()
);
loadServiceToPortMap();

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/cloud/localstack/docker/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cloud.localstack.Localstack;
import cloud.localstack.docker.command.*;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -54,11 +55,12 @@ public class Container {
* @param portMappings
* @param bindMounts Docker host to container volume mapping like /host/dir:/container/dir, be aware that the host
* directory must be an absolute path
* @param platform target platform for the localstack docker image
*/
public static Container createLocalstackContainer(
String externalHostName, boolean pullNewImage, boolean randomizePorts, String imageName, String imageTag, String portEdge,
String portElasticSearch, Map<String, String> environmentVariables, Map<Integer, Integer> portMappings,
Map<String, String> bindMounts) {
Map<String, String> bindMounts, String platform) {

environmentVariables = environmentVariables == null ? Collections.emptyMap() : environmentVariables;
bindMounts = bindMounts == null ? Collections.emptyMap() : bindMounts;
Expand Down Expand Up @@ -86,6 +88,9 @@ public static Container createLocalstackContainer(
.withEnvironmentVariables(environmentVariables)
.withBindMountedVolumes(bindMounts);

if(!StringUtils.isEmpty(platform))
runCommand = runCommand.withPlatform(platform);

for (Integer port : portMappings.keySet()) {
runCommand = runCommand.withExposedPorts("" + port, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private LocalstackDockerConfiguration processDockerPropertiesAnnotation(Localsta
.portEdge(getEnvOrDefault("LOCALSTACK_EDGE_PORT", properties.portEdge()))
.portElasticSearch(getEnvOrDefault("LOCALSTACK_ELASTICSEARCH_PORT", properties.portElasticSearch()))
.useSingleDockerContainer(properties.useSingleDockerContainer())
.platform(StringUtils.isEmpty(properties.platform()) ? null : properties.platform())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class LocalstackDockerConfiguration {

private final String imageName;
private final String imageTag;
private final String platform;

@Builder.Default
private final String portEdge = "4566";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,10 @@
* This can be used to run tests with an existing LocalStack container running on the host.
*/
boolean ignoreDockerRunErrors() default false;

/**
* Specifies a target platform for the localstack docker image. Value is used by the --platform flag in the
* docker run command
*/
String platform() default "";
}
5 changes: 5 additions & 0 deletions src/main/java/cloud/localstack/docker/command/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public RunCommand withEnvironmentVariables(Map<String, String> environmentVariab
return this;
}

public RunCommand withPlatform(String platform) {
addOptions("--platform", platform);
return this;
}

private void addEnvOption(String name, String value) {
addOptions("-e", String.format("%s=%s", name, value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void testAccessPredefinedPort() {
@Test
public void createLocalstackContainerWithRandomPorts() throws Exception {
Container container = Container.createLocalstackContainer(
EXTERNAL_HOST_NAME, pullNewImage, true, null, null, null, null, null, null, null);
EXTERNAL_HOST_NAME, pullNewImage, true, null, null, null, null, null, null, null, null);

try {
container.waitForAllPorts(EXTERNAL_HOST_NAME);
Expand All @@ -53,7 +53,7 @@ public void createLocalstackContainerWithRandomPorts() throws Exception {
@Test
public void createLocalstackContainerWithStaticPorts() throws Exception {
Container container = Container.createLocalstackContainer(
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, null, null, null, null, null);
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, null, null, null, null, null, null);

try {
container.waitForAllPorts(EXTERNAL_HOST_NAME);
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/cloud/localstack/docker/ContainerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void createLocalstackContainer() throws Exception {
HashMap<String, String> environmentVariables = new HashMap<>();
environmentVariables.put(MY_PROPERTY, MY_VALUE);
Container localStackContainer = Container.createLocalstackContainer(
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, null, null, environmentVariables, null, null);
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, null, null, environmentVariables, null, null, null);

try {
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);
Expand Down Expand Up @@ -60,7 +60,7 @@ public void createLocalstackContainerWithFullImage() {

String customImageName = "localstack/localstack-full";
Container localStackContainer = Container.createLocalstackContainer(
EXTERNAL_HOST_NAME, pullNewImage, false, customImageName, null, null, null, null, null, null);
EXTERNAL_HOST_NAME, pullNewImage, false, customImageName, null, null, null, null, null, null, null);

try {
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);
Expand All @@ -81,7 +81,7 @@ public void createLocalstackContainerWithScriptMounted() {

Container localStackContainer = Container.createLocalstackContainer(
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, null, null, null, null,
Collections.singletonMap(testFile("echo testmarker"), Localstack.INIT_SCRIPTS_PATH + "/test.sh"));
Collections.singletonMap(testFile("echo testmarker"), Localstack.INIT_SCRIPTS_PATH + "/test.sh"), null);

try {
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);
Expand Down Expand Up @@ -109,7 +109,7 @@ static String testFile(String content) {
@Test
public void createLocalstackContainerWithCustomPorts() throws Exception {
Container localStackContainer = Container.createLocalstackContainer(
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, "45660", "45710", null, null, null);
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, "45660", "45710", null, null, null, null);

try {
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);
Expand All @@ -126,7 +126,7 @@ public void createLocalstackContainerWithCustomPorts() throws Exception {
@Test
public void createLocalstackContainerWithRandomPorts() throws Exception {
Container localStackContainer = Container.createLocalstackContainer(
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, ":4566", ":4571", null, null, null);
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, ":4566", ":4571", null, null, null, null);

try {
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);
Expand Down