diff --git a/pkg/kubelet/dockertools/docker_test.go b/pkg/kubelet/dockertools/docker_test.go index 22161b13d0c..2eb2ea7db11 100644 --- a/pkg/kubelet/dockertools/docker_test.go +++ b/pkg/kubelet/dockertools/docker_test.go @@ -698,6 +698,11 @@ func TestMakePortsAndBindings(t *testing.T) { HostPort: 445, Protocol: "foobar", }, + { + ContainerPort: 443, + HostPort: 446, + Protocol: "tcp", + }, } exposedPorts, bindings := makePortsAndBindings(ports) if len(ports) != len(exposedPorts) || @@ -734,6 +739,14 @@ func TestMakePortsAndBindings(t *testing.T) { if value[0].HostIP != "" { t.Errorf("Unexpected host IP: %s", value[0].HostIP) } + case "446": + // allow multiple host ports bind to same container port + if !reflect.DeepEqual(docker.Port("443/tcp"), key) { + t.Errorf("Unexpected docker port: %#v", key) + } + if value[0].HostIP != "" { + t.Errorf("Unexpected host IP: %s", value[0].HostIP) + } } } } diff --git a/pkg/kubelet/dockertools/manager.go b/pkg/kubelet/dockertools/manager.go index 4f51b492f2b..2bf3dfd0eab 100644 --- a/pkg/kubelet/dockertools/manager.go +++ b/pkg/kubelet/dockertools/manager.go @@ -573,11 +573,21 @@ func makePortsAndBindings(portMappings []kubecontainer.PortMapping) (map[docker. } dockerPort := docker.Port(strconv.Itoa(interiorPort) + protocol) exposedPorts[dockerPort] = struct{}{} - portBindings[dockerPort] = []docker.PortBinding{ - { - HostPort: strconv.Itoa(exteriorPort), - HostIP: port.HostIP, - }, + + hostBinding := docker.PortBinding{ + HostPort: strconv.Itoa(exteriorPort), + HostIP: port.HostIP, + } + + // Allow multiple host ports bind to same container port + if existedBindings := portBindings[dockerPort]; len(existedBindings) != 0 { + // If a container port already map to a host port, append to the host ports + portBindings[dockerPort] = append(existedBindings, hostBinding) + } else { + // Otherwise, it's fresh new port binding + portBindings[dockerPort] = []docker.PortBinding{ + hostBinding, + } } } return exposedPorts, portBindings