Skip to content

Commit b42ed50

Browse files
author
Willian S. de Souza
committed
fix: enhance host configuration port binding
This commit refactors the mergePortBinding function to accommodate exposed ports with their respective protocols.
1 parent ab60437 commit b42ed50

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

lifecycle.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import (
55
"errors"
66
"fmt"
77
"io"
8-
"strings"
98

109
"github.com/docker/docker/api/types/container"
1110
"github.com/docker/docker/api/types/network"
1211
"github.com/docker/go-connections/nat"
13-
"golang.org/x/exp/slices"
1412
)
1513

1614
// ContainerRequestHook is a hook that will be called before a container is created.
@@ -458,7 +456,7 @@ func (p *DockerProvider) preCreateContainerHook(ctx context.Context, req Contain
458456
if len(exposedPorts) == 0 && !hostConfig.NetworkMode.IsContainer() {
459457
hostConfig.PortBindings = exposedPortMap
460458
} else {
461-
hostConfig.PortBindings = mergePortBindings(hostConfig.PortBindings, exposedPortMap, req.ExposedPorts)
459+
hostConfig.PortBindings = mergePortBindings(hostConfig.PortBindings, exposedPortMap)
462460
}
463461

464462
return nil
@@ -525,15 +523,13 @@ func combineContainerHooks(defaultHooks, userDefinedHooks []ContainerLifecycleHo
525523
}
526524
}
527525

528-
func mergePortBindings(configPortMap, exposedPortMap nat.PortMap, exposedPorts []string) nat.PortMap {
526+
func mergePortBindings(configPortMap, exposedPortMap nat.PortMap) nat.PortMap {
529527
if exposedPortMap == nil {
530528
exposedPortMap = make(map[nat.Port][]nat.PortBinding)
531529
}
532530

533531
for k, v := range configPortMap {
534-
if slices.Contains(exposedPorts, strings.Split(string(k), "/")[0]) {
535-
exposedPortMap[k] = v
536-
}
532+
exposedPortMap[k] = v
537533
}
538534
return exposedPortMap
539535
}

lifecycle_test.go

+33-21
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func TestPreCreateModifierHook(t *testing.T) {
304304
)
305305
})
306306

307-
t.Run("Request contains exposed port modifiers", func(t *testing.T) {
307+
t.Run("Request contains exposed port modifiers without protocol", func(t *testing.T) {
308308
req := ContainerRequest{
309309
Image: nginxAlpineImage, // alpine image does expose port 80
310310
HostConfigModifier: func(hostConfig *container.HostConfig) {
@@ -334,6 +334,37 @@ func TestPreCreateModifierHook(t *testing.T) {
334334
assert.Equal(t, "localhost", inputHostConfig.PortBindings["80/tcp"][0].HostIP)
335335
assert.Equal(t, "8080", inputHostConfig.PortBindings["80/tcp"][0].HostPort)
336336
})
337+
338+
t.Run("Request contains exposed port modifiers with protocol", func(t *testing.T) {
339+
req := ContainerRequest{
340+
Image: nginxAlpineImage, // alpine image does expose port 80
341+
HostConfigModifier: func(hostConfig *container.HostConfig) {
342+
hostConfig.PortBindings = nat.PortMap{
343+
"80/tcp": []nat.PortBinding{
344+
{
345+
HostIP: "localhost",
346+
HostPort: "8080",
347+
},
348+
},
349+
}
350+
},
351+
ExposedPorts: []string{"80/tcp"},
352+
}
353+
354+
// define empty inputs to be overwritten by the pre create hook
355+
inputConfig := &container.Config{
356+
Image: req.Image,
357+
}
358+
inputHostConfig := &container.HostConfig{}
359+
inputNetworkingConfig := &network.NetworkingConfig{}
360+
361+
err = provider.preCreateContainerHook(ctx, req, inputConfig, inputHostConfig, inputNetworkingConfig)
362+
require.NoError(t, err)
363+
364+
// assertions
365+
assert.Equal(t, "localhost", inputHostConfig.PortBindings["80/tcp"][0].HostIP)
366+
assert.Equal(t, "8080", inputHostConfig.PortBindings["80/tcp"][0].HostPort)
367+
})
337368
}
338369

339370
func TestMergePortBindings(t *testing.T) {
@@ -352,7 +383,6 @@ func TestMergePortBindings(t *testing.T) {
352383
arg: arg{
353384
configPortMap: nil,
354385
parsedPortMap: nil,
355-
exposedPorts: nil,
356386
},
357387
expected: map[nat.Port][]nat.PortBinding{},
358388
},
@@ -363,7 +393,6 @@ func TestMergePortBindings(t *testing.T) {
363393
"80/tcp": {{HostIP: "1", HostPort: "2"}},
364394
},
365395
parsedPortMap: nil,
366-
exposedPorts: nil,
367396
},
368397
expected: map[nat.Port][]nat.PortBinding{},
369398
},
@@ -374,22 +403,6 @@ func TestMergePortBindings(t *testing.T) {
374403
parsedPortMap: map[nat.Port][]nat.PortBinding{
375404
"80/tcp": {{HostIP: "", HostPort: ""}},
376405
},
377-
exposedPorts: nil,
378-
},
379-
expected: map[nat.Port][]nat.PortBinding{
380-
"80/tcp": {{HostIP: "", HostPort: ""}},
381-
},
382-
},
383-
{
384-
name: "parsed and configured but not exposed",
385-
arg: arg{
386-
configPortMap: map[nat.Port][]nat.PortBinding{
387-
"80/tcp": {{HostIP: "1", HostPort: "2"}},
388-
},
389-
parsedPortMap: map[nat.Port][]nat.PortBinding{
390-
"80/tcp": {{HostIP: "", HostPort: ""}},
391-
},
392-
exposedPorts: nil,
393406
},
394407
expected: map[nat.Port][]nat.PortBinding{
395408
"80/tcp": {{HostIP: "", HostPort: ""}},
@@ -407,7 +420,6 @@ func TestMergePortBindings(t *testing.T) {
407420
"80/tcp": {{HostIP: "", HostPort: ""}},
408421
"90/tcp": {{HostIP: "", HostPort: ""}},
409422
},
410-
exposedPorts: []string{"70", "80"},
411423
},
412424
expected: map[nat.Port][]nat.PortBinding{
413425
"70/tcp": {{HostIP: "1", HostPort: "2"}},
@@ -419,7 +431,7 @@ func TestMergePortBindings(t *testing.T) {
419431

420432
for _, c := range cases {
421433
t.Run(c.name, func(t *testing.T) {
422-
res := mergePortBindings(c.arg.configPortMap, c.arg.parsedPortMap, c.arg.exposedPorts)
434+
res := mergePortBindings(c.arg.configPortMap, c.arg.parsedPortMap)
423435
assert.Equal(t, c.expected, res)
424436
})
425437
}

0 commit comments

Comments
 (0)