Skip to content

Commit e6444e0

Browse files
authored
Merge pull request kubernetes#94494 from SergeyKanzhelev/hostportConflicts
Allow to map the same container port to different host ports
2 parents 1bb1702 + 1c379b1 commit e6444e0

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

pkg/kubelet/container/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func MakePortMappings(container *v1.Container) (ports []PortMapping) {
335335

336336
var name string = p.Name
337337
if name == "" {
338-
name = fmt.Sprintf("%s-%s:%d", family, p.Protocol, p.ContainerPort)
338+
name = fmt.Sprintf("%s-%s-%s:%d:%d", family, p.Protocol, p.HostIP, p.ContainerPort, p.HostPort)
339339
}
340340

341341
// Protect against a port name being used more than once in a container.

pkg/kubelet/container/helpers_test.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,6 @@ func TestMakePortMappings(t *testing.T) {
580580
port("foo", v1.ProtocolUDP, 555, 5555, ""),
581581
// Duplicated, should be ignored.
582582
port("foo", v1.ProtocolUDP, 888, 8888, ""),
583-
// Duplicated, should be ignored.
584-
port("", v1.ProtocolTCP, 80, 8888, "127.0.0.1"),
585583
// Duplicated with different address family, shouldn't be ignored
586584
port("", v1.ProtocolTCP, 80, 8080, "::"),
587585
// No address family specified
@@ -596,6 +594,47 @@ func TestMakePortMappings(t *testing.T) {
596594
portMapping(v1.ProtocolTCP, 1234, 5678, ""),
597595
},
598596
},
597+
{
598+
// The same container port can be mapped to different host ports
599+
&v1.Container{
600+
Name: "fooContainer",
601+
Ports: []v1.ContainerPort{
602+
port("", v1.ProtocolTCP, 443, 4343, "192.168.0.1"),
603+
port("", v1.ProtocolTCP, 4343, 4343, "192.168.0.1"),
604+
},
605+
},
606+
[]PortMapping{
607+
portMapping(v1.ProtocolTCP, 443, 4343, "192.168.0.1"),
608+
portMapping(v1.ProtocolTCP, 4343, 4343, "192.168.0.1"),
609+
},
610+
},
611+
{
612+
// The same container port AND same container host is not OK
613+
&v1.Container{
614+
Name: "fooContainer",
615+
Ports: []v1.ContainerPort{
616+
port("", v1.ProtocolTCP, 443, 4343, ""),
617+
port("", v1.ProtocolTCP, 443, 4343, ""),
618+
},
619+
},
620+
[]PortMapping{
621+
portMapping(v1.ProtocolTCP, 443, 4343, ""),
622+
},
623+
},
624+
{
625+
// multihomed nodes - multiple IP scenario
626+
&v1.Container{
627+
Name: "fooContainer",
628+
Ports: []v1.ContainerPort{
629+
port("", v1.ProtocolTCP, 443, 4343, "192.168.0.1"),
630+
port("", v1.ProtocolTCP, 443, 4343, "172.16.0.1"),
631+
},
632+
},
633+
[]PortMapping{
634+
portMapping(v1.ProtocolTCP, 443, 4343, "192.168.0.1"),
635+
portMapping(v1.ProtocolTCP, 443, 4343, "172.16.0.1"),
636+
},
637+
},
599638
}
600639

601640
for i, tt := range tests {

0 commit comments

Comments
 (0)