Skip to content
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

Add Host IP to port response for v4 container response #3136

Merged
merged 1 commit into from Mar 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions agent/handlers/v1/response.go
Expand Up @@ -67,6 +67,7 @@ type PortResponse struct {
ContainerPort uint16 `json:"ContainerPort,omitempty"`
Protocol string `json:"Protocol,omitempty"`
HostPort uint16 `json:"HostPort,omitempty"`
HostIp string `json:"HostIp,omitempty"`
}

// NewTaskResponse creates a TaskResponse for a task.
Expand Down
3 changes: 3 additions & 0 deletions agent/handlers/v2/response.go
Expand Up @@ -281,6 +281,9 @@ func NewContainerResponse(
} else {
port.HostPort = port.ContainerPort
}
if includeV4Metadata {
port.HostIp = binding.BindIP
}

resp.Ports = append(resp.Ports, port)
}
Expand Down
44 changes: 33 additions & 11 deletions agent/handlers/v2/response_test.go
Expand Up @@ -54,6 +54,7 @@ const (
volDestination = "/volume"
availabilityZone = "us-west-2b"
containerInstanceArn = "containerInstance-test"
hostIp = "0.0.0.0"
)

func TestTaskResponse(t *testing.T) {
Expand Down Expand Up @@ -462,6 +463,19 @@ func TestTaskResponseMarshal(t *testing.T) {
}

func TestContainerResponseMarshal(t *testing.T) {
testCases := []struct {
description string
includeV4Metadata bool
}{
{
"task container response without v4 metadata",
false,
},
{
"task container response with v4 metadata",
true,
},
}
timeRFC3339, _ := time.Parse(time.RFC3339, "2014-11-12T11:45:26Z")

expectedContainerResponseMap := map[string]interface{}{
Expand Down Expand Up @@ -549,20 +563,28 @@ func TestContainerResponseMarshal(t *testing.T) {
},
},
}
gomock.InOrder(
state.EXPECT().ContainerByID(containerID).Return(dockerContainer, true),
state.EXPECT().TaskByID(containerID).Return(task, true),
)

containerResponse, err := NewContainerResponseFromState(containerID, state, false)
assert.NoError(t, err)
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
gomock.InOrder(
state.EXPECT().ContainerByID(containerID).Return(dockerContainer, true),
state.EXPECT().TaskByID(containerID).Return(task, true),
)
if tc.includeV4Metadata {
container.KnownPortBindingsUnsafe[0].BindIP = hostIp
expectedContainerResponseMap["Ports"].([]interface{})[0].(map[string]interface{})["HostIp"] = hostIp
}
containerResponse, err := NewContainerResponseFromState(containerID, state, tc.includeV4Metadata)
assert.NoError(t, err)

containerResponseJSON, err := json.Marshal(containerResponse)
assert.NoError(t, err)
containerResponseJSON, err := json.Marshal(containerResponse)
assert.NoError(t, err)

containerResponseMap := make(map[string]interface{})
json.Unmarshal(containerResponseJSON, &containerResponseMap)
assert.Equal(t, expectedContainerResponseMap, containerResponseMap)
containerResponseMap := make(map[string]interface{})
json.Unmarshal(containerResponseJSON, &containerResponseMap)
assert.Equal(t, expectedContainerResponseMap, containerResponseMap)
})
}
}

func TestTaskResponseWithV4TagsError(t *testing.T) {
Expand Down