From 1c379b12818870bafe66b09760279e3f927a3beb Mon Sep 17 00:00:00 2001 From: Sergey Kanzhelev Date: Thu, 3 Sep 2020 22:21:18 +0000 Subject: [PATCH] allow to map the same container port to different host ports --- pkg/kubelet/container/helpers.go | 2 +- pkg/kubelet/container/helpers_test.go | 43 +++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/pkg/kubelet/container/helpers.go b/pkg/kubelet/container/helpers.go index dd746bc2e2a..45e003bbc9d 100644 --- a/pkg/kubelet/container/helpers.go +++ b/pkg/kubelet/container/helpers.go @@ -335,7 +335,7 @@ func MakePortMappings(container *v1.Container) (ports []PortMapping) { var name string = p.Name if name == "" { - name = fmt.Sprintf("%s-%s:%d", family, p.Protocol, p.ContainerPort) + name = fmt.Sprintf("%s-%s-%s:%d:%d", family, p.Protocol, p.HostIP, p.ContainerPort, p.HostPort) } // Protect against a port name being used more than once in a container. diff --git a/pkg/kubelet/container/helpers_test.go b/pkg/kubelet/container/helpers_test.go index aa77b2680d1..bcb49adfcd8 100644 --- a/pkg/kubelet/container/helpers_test.go +++ b/pkg/kubelet/container/helpers_test.go @@ -580,8 +580,6 @@ func TestMakePortMappings(t *testing.T) { port("foo", v1.ProtocolUDP, 555, 5555, ""), // Duplicated, should be ignored. port("foo", v1.ProtocolUDP, 888, 8888, ""), - // Duplicated, should be ignored. - port("", v1.ProtocolTCP, 80, 8888, "127.0.0.1"), // Duplicated with different address family, shouldn't be ignored port("", v1.ProtocolTCP, 80, 8080, "::"), // No address family specified @@ -596,6 +594,47 @@ func TestMakePortMappings(t *testing.T) { portMapping(v1.ProtocolTCP, 1234, 5678, ""), }, }, + { + // The same container port can be mapped to different host ports + &v1.Container{ + Name: "fooContainer", + Ports: []v1.ContainerPort{ + port("", v1.ProtocolTCP, 443, 4343, "192.168.0.1"), + port("", v1.ProtocolTCP, 4343, 4343, "192.168.0.1"), + }, + }, + []PortMapping{ + portMapping(v1.ProtocolTCP, 443, 4343, "192.168.0.1"), + portMapping(v1.ProtocolTCP, 4343, 4343, "192.168.0.1"), + }, + }, + { + // The same container port AND same container host is not OK + &v1.Container{ + Name: "fooContainer", + Ports: []v1.ContainerPort{ + port("", v1.ProtocolTCP, 443, 4343, ""), + port("", v1.ProtocolTCP, 443, 4343, ""), + }, + }, + []PortMapping{ + portMapping(v1.ProtocolTCP, 443, 4343, ""), + }, + }, + { + // multihomed nodes - multiple IP scenario + &v1.Container{ + Name: "fooContainer", + Ports: []v1.ContainerPort{ + port("", v1.ProtocolTCP, 443, 4343, "192.168.0.1"), + port("", v1.ProtocolTCP, 443, 4343, "172.16.0.1"), + }, + }, + []PortMapping{ + portMapping(v1.ProtocolTCP, 443, 4343, "192.168.0.1"), + portMapping(v1.ProtocolTCP, 443, 4343, "172.16.0.1"), + }, + }, } for i, tt := range tests {