Skip to content

Commit 64a0760

Browse files
committed
handler: use container.Ports when KnownPortsBindings empty
1 parent d9d127f commit 64a0760

File tree

4 files changed

+102
-23
lines changed

4 files changed

+102
-23
lines changed

agent/api/container.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,10 @@ func (c *Container) GetLabels() map[string]string {
493493
return c.labels
494494
}
495495

496-
// SetKnownPortBindings gets the ports for a container
496+
// SetKnownPortBindings sets the ports for a container
497497
func (c *Container) SetKnownPortBindings(ports []PortBinding) {
498-
c.lock.RLock()
499-
defer c.lock.RUnlock()
498+
c.lock.Lock()
499+
defer c.lock.Unlock()
500500

501501
c.KnownPortBindingsUnsafe = ports
502502
}

agent/handlers/types/v1/response.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ type ContainerResponse struct {
4545
DockerId string
4646
DockerName string
4747
Name string
48-
Ports []v2.PortResponse
49-
Networks []containermetadata.Network
48+
Ports []v2.PortResponse `json:",omitempty"`
49+
Networks []containermetadata.Network `json:",omitempty"`
5050
}

agent/handlers/v1_handlers.go

+28-12
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,33 @@ func newContainerResponse(dockerContainer *api.DockerContainer, eni *api.ENI) v1
104104
DockerName: dockerContainer.DockerName,
105105
}
106106

107-
for _, binding := range container.GetKnownPortBindings() {
107+
resp.Ports = newPortBindingsResponse(dockerContainer, eni)
108+
109+
if eni != nil {
110+
resp.Networks = []containermetadata.Network{
111+
{
112+
NetworkMode: networkModeAwsvpc,
113+
IPv4Addresses: eni.GetIPV4Addresses(),
114+
IPv6Addresses: eni.GetIPV6Addresses(),
115+
},
116+
}
117+
}
118+
return resp
119+
}
120+
121+
func newPortBindingsResponse(dockerContainer *api.DockerContainer, eni *api.ENI) []v2.PortResponse {
122+
container := dockerContainer.Container
123+
resp := []v2.PortResponse{}
124+
125+
bindings := container.GetKnownPortBindings()
126+
127+
// if KnownPortBindings list is empty, then we use the port mapping
128+
// information that was passed down from ACS.
129+
if len(bindings) == 0 {
130+
bindings = container.Ports
131+
}
132+
133+
for _, binding := range bindings {
108134
port := v2.PortResponse{
109135
ContainerPort: binding.ContainerPort,
110136
Protocol: binding.Protocol.String(),
@@ -116,17 +142,7 @@ func newContainerResponse(dockerContainer *api.DockerContainer, eni *api.ENI) v1
116142
port.HostPort = port.ContainerPort
117143
}
118144

119-
resp.Ports = append(resp.Ports, port)
120-
}
121-
122-
if eni != nil {
123-
resp.Networks = []containermetadata.Network{
124-
{
125-
NetworkMode: networkModeAwsvpc,
126-
IPv4Addresses: eni.GetIPV4Addresses(),
127-
IPv6Addresses: eni.GetIPV6Addresses(),
128-
},
129-
}
145+
resp = append(resp, port)
130146
}
131147
return resp
132148
}

agent/handlers/v1_handlers_test.go

+69-6
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,41 @@ func TestGetAWSVPCTaskByTaskArn(t *testing.T) {
138138
resp := v1.TasksResponse{Tasks: []*v1.TaskResponse{&taskResponse}}
139139

140140
assert.Equal(t, eniIPV4Address, resp.Tasks[0].Containers[0].Networks[0].IPv4Addresses[0])
141+
taskDiffHelper(t, []*api.Task{testTasks[3]}, resp)
142+
}
143+
144+
func TestGetHostNeworkingTaskByTaskArn(t *testing.T) {
145+
recorder := performMockRequest(t, "/v1/tasks?taskarn=hostModeNetworkingTask")
146+
147+
var taskResponse v1.TaskResponse
148+
err := json.Unmarshal(recorder.Body.Bytes(), &taskResponse)
149+
if err != nil {
150+
t.Fatal(err)
151+
}
152+
153+
resp := v1.TasksResponse{Tasks: []*v1.TaskResponse{&taskResponse}}
154+
141155
assert.Equal(t, uint16(80), resp.Tasks[0].Containers[0].Ports[0].ContainerPort)
142156
assert.Equal(t, "tcp", resp.Tasks[0].Containers[0].Ports[0].Protocol)
143157

144-
taskDiffHelper(t, []*api.Task{testTasks[3]}, resp)
158+
taskDiffHelper(t, []*api.Task{testTasks[4]}, resp)
159+
}
160+
161+
func TestGetBridgeNeworkingTaskByTaskArn(t *testing.T) {
162+
recorder := performMockRequest(t, "/v1/tasks?taskarn=bridgeModeNetworkingTask")
163+
164+
var taskResponse v1.TaskResponse
165+
err := json.Unmarshal(recorder.Body.Bytes(), &taskResponse)
166+
if err != nil {
167+
t.Fatal(err)
168+
}
169+
170+
resp := v1.TasksResponse{Tasks: []*v1.TaskResponse{&taskResponse}}
171+
172+
assert.Equal(t, uint16(80), resp.Tasks[0].Containers[0].Ports[0].ContainerPort)
173+
assert.Equal(t, "tcp", resp.Tasks[0].Containers[0].Ports[0].Protocol)
174+
175+
taskDiffHelper(t, []*api.Task{testTasks[5]}, resp)
145176
}
146177

147178
func TestGetTaskByTaskArnNotFound(t *testing.T) {
@@ -331,18 +362,50 @@ var testTasks = []*api.Task{
331362
Containers: []*api.Container{
332363
{
333364
Name: "awsvpc",
334-
KnownPortBindingsUnsafe: []api.PortBinding{
365+
},
366+
},
367+
ENI: &api.ENI{
368+
IPV4Addresses: []*api.ENIIPV4Address{
369+
{
370+
Address: eniIPV4Address,
371+
},
372+
},
373+
},
374+
},
375+
{
376+
Arn: "hostModeNetworkingTask",
377+
DesiredStatusUnsafe: api.TaskRunning,
378+
KnownStatusUnsafe: api.TaskRunning,
379+
Family: "test",
380+
Version: "1",
381+
Containers: []*api.Container{
382+
{
383+
Name: "awsvpc",
384+
Ports: []api.PortBinding{
335385
{
336386
ContainerPort: 80,
387+
HostPort: 80,
337388
Protocol: api.TransportProtocolTCP,
338389
},
339390
},
340391
},
341392
},
342-
ENI: &api.ENI{
343-
IPV4Addresses: []*api.ENIIPV4Address{
344-
{
345-
Address: eniIPV4Address,
393+
},
394+
{
395+
Arn: "bridgeModeNetworkingTask",
396+
DesiredStatusUnsafe: api.TaskRunning,
397+
KnownStatusUnsafe: api.TaskRunning,
398+
Family: "test",
399+
Version: "1",
400+
Containers: []*api.Container{
401+
{
402+
Name: "awsvpc",
403+
KnownPortBindingsUnsafe: []api.PortBinding{
404+
{
405+
ContainerPort: 80,
406+
HostPort: 80,
407+
Protocol: api.TransportProtocolTCP,
408+
},
346409
},
347410
},
348411
},

0 commit comments

Comments
 (0)