Merge pull request #94494 from SergeyKanzhelev/hostportConflicts

Allow to map the same container port to different host ports
This commit is contained in:
Kubernetes Prow Robot 2020-09-22 12:23:40 -07:00 committed by GitHub
commit e6444e01ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 3 deletions

View File

@ -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.

View File

@ -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 {