Skip to content

Commit

Permalink
fix(ecs): If in "host" network mode, host and container port should b…
Browse files Browse the repository at this point in the history
…e the same (#3776)
  • Loading branch information
Justin Lee authored and ezimanyi committed Jun 14, 2019
1 parent cd9fb53 commit ebbe104
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class CreateServerGroupAtomicOperation

private static final String NECESSARY_TRUSTED_SERVICE = "ecs-tasks.amazonaws.com";
protected static final String AWSVPC_NETWORK_MODE = "awsvpc";
protected static final String HOST_NETWORK_MODE = "host";
protected static final String FARGATE_LAUNCH_TYPE = "FARGATE";
protected static final String NO_IAM_ROLE = "None (No IAM role)";
protected static final String NO_IMAGE_CREDENTIALS = "None (No registry credentials)";
Expand Down Expand Up @@ -166,7 +167,8 @@ protected RegisterTaskDefinitionRequest makeTaskDefinitionRequest(
.withProtocol(
description.getPortProtocol() != null ? description.getPortProtocol() : "tcp");

if (AWSVPC_NETWORK_MODE.equals(description.getNetworkMode())) {
if (AWSVPC_NETWORK_MODE.equals(description.getNetworkMode())
|| HOST_NETWORK_MODE.equals(description.getNetworkMode())) {
portMapping
.withHostPort(description.getContainerPort())
.withContainerPort(description.getContainerPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,21 @@ class CreateServerGroupAtomicOperationSpec extends CommonAtomicOperation {
environments.get("ENVIRONMENT_1") == "test1"
environments.get("ENVIRONMENT_2") == "test2"
}

def 'should use same port for host and container in host mode'() {
given:
def description = Mock(CreateServerGroupDescription)
description.getContainerPort() >> 10000
description.getNetworkMode() >> 'host'
def operation = new CreateServerGroupAtomicOperation(description)

when:
def request = operation.makeTaskDefinitionRequest('arn:aws:iam::test:test-role', 'mygreatapp-stack1-details2-v0011')

then:
def portMapping = request.getContainerDefinitions().get(0).getPortMappings().get(0)
portMapping.getHostPort() == 10000
portMapping.getContainerPort() == 10000
portMapping.getProtocol() == 'tcp'
}
}

1 comment on commit ebbe104

@yishan-lin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work @justinrlee!!!

Please sign in to comment.