mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 07:47:56 +00:00
Merge pull request #13384 from ZJU-SEL/portsbindings
Allow multiple host ports map to the same container port
This commit is contained in:
commit
a92c8b6886
@ -22,6 +22,7 @@ import (
|
|||||||
"hash/adler32"
|
"hash/adler32"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -698,42 +699,81 @@ func TestMakePortsAndBindings(t *testing.T) {
|
|||||||
HostPort: 445,
|
HostPort: 445,
|
||||||
Protocol: "foobar",
|
Protocol: "foobar",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
ContainerPort: 443,
|
||||||
|
HostPort: 446,
|
||||||
|
Protocol: "tcp",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ContainerPort: 443,
|
||||||
|
HostPort: 446,
|
||||||
|
Protocol: "udp",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
exposedPorts, bindings := makePortsAndBindings(ports)
|
exposedPorts, bindings := makePortsAndBindings(ports)
|
||||||
if len(ports) != len(exposedPorts) ||
|
|
||||||
len(ports) != len(bindings) {
|
// Count the expected exposed ports and bindings
|
||||||
|
expectedExposedPorts := map[string]struct{}{}
|
||||||
|
|
||||||
|
for _, binding := range ports {
|
||||||
|
dockerKey := strconv.Itoa(binding.ContainerPort) + "/" + string(binding.Protocol)
|
||||||
|
expectedExposedPorts[dockerKey] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should expose right ports in docker
|
||||||
|
if len(expectedExposedPorts) != len(exposedPorts) {
|
||||||
t.Errorf("Unexpected ports and bindings, %#v %#v %#v", ports, exposedPorts, bindings)
|
t.Errorf("Unexpected ports and bindings, %#v %#v %#v", ports, exposedPorts, bindings)
|
||||||
}
|
}
|
||||||
for key, value := range bindings {
|
|
||||||
switch value[0].HostPort {
|
// Construct expected bindings
|
||||||
case "8080":
|
expectPortBindings := map[string][]docker.PortBinding{
|
||||||
if !reflect.DeepEqual(docker.Port("80/tcp"), key) {
|
"80/tcp": {
|
||||||
t.Errorf("Unexpected docker port: %#v", key)
|
docker.PortBinding{
|
||||||
}
|
HostPort: "8080",
|
||||||
if value[0].HostIP != "127.0.0.1" {
|
HostIP: "127.0.0.1",
|
||||||
t.Errorf("Unexpected host IP: %s", value[0].HostIP)
|
},
|
||||||
}
|
},
|
||||||
case "443":
|
"443/tcp": {
|
||||||
if !reflect.DeepEqual(docker.Port("443/tcp"), key) {
|
docker.PortBinding{
|
||||||
t.Errorf("Unexpected docker port: %#v", key)
|
HostPort: "443",
|
||||||
}
|
HostIP: "",
|
||||||
if value[0].HostIP != "" {
|
},
|
||||||
t.Errorf("Unexpected host IP: %s", value[0].HostIP)
|
docker.PortBinding{
|
||||||
}
|
HostPort: "446",
|
||||||
case "444":
|
HostIP: "",
|
||||||
if !reflect.DeepEqual(docker.Port("444/udp"), key) {
|
},
|
||||||
t.Errorf("Unexpected docker port: %#v", key)
|
},
|
||||||
}
|
"443/udp": {
|
||||||
if value[0].HostIP != "" {
|
docker.PortBinding{
|
||||||
t.Errorf("Unexpected host IP: %s", value[0].HostIP)
|
HostPort: "446",
|
||||||
}
|
HostIP: "",
|
||||||
case "445":
|
},
|
||||||
if !reflect.DeepEqual(docker.Port("445/tcp"), key) {
|
},
|
||||||
t.Errorf("Unexpected docker port: %#v", key)
|
"444/udp": {
|
||||||
}
|
docker.PortBinding{
|
||||||
if value[0].HostIP != "" {
|
HostPort: "444",
|
||||||
t.Errorf("Unexpected host IP: %s", value[0].HostIP)
|
HostIP: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"445/tcp": {
|
||||||
|
docker.PortBinding{
|
||||||
|
HostPort: "445",
|
||||||
|
HostIP: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// interate the bindings by dockerPort, and check its portBindings
|
||||||
|
for dockerPort, portBindings := range bindings {
|
||||||
|
switch dockerPort {
|
||||||
|
case "80/tcp", "443/tcp", "443/udp", "444/udp", "445/tcp":
|
||||||
|
if !reflect.DeepEqual(expectPortBindings[string(dockerPort)], portBindings) {
|
||||||
|
t.Errorf("Unexpected portbindings for %#v, expected: %#v, but got: %#v",
|
||||||
|
dockerPort, expectPortBindings[string(dockerPort)], portBindings)
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
t.Errorf("Unexpected docker port: %#v with portbindings: %#v", dockerPort, portBindings)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -584,13 +584,24 @@ func makePortsAndBindings(portMappings []kubecontainer.PortMapping) (map[docker.
|
|||||||
glog.Warningf("Unknown protocol %q: defaulting to TCP", port.Protocol)
|
glog.Warningf("Unknown protocol %q: defaulting to TCP", port.Protocol)
|
||||||
protocol = "/tcp"
|
protocol = "/tcp"
|
||||||
}
|
}
|
||||||
|
|
||||||
dockerPort := docker.Port(strconv.Itoa(interiorPort) + protocol)
|
dockerPort := docker.Port(strconv.Itoa(interiorPort) + protocol)
|
||||||
exposedPorts[dockerPort] = struct{}{}
|
exposedPorts[dockerPort] = struct{}{}
|
||||||
portBindings[dockerPort] = []docker.PortBinding{
|
|
||||||
{
|
hostBinding := docker.PortBinding{
|
||||||
HostPort: strconv.Itoa(exteriorPort),
|
HostPort: strconv.Itoa(exteriorPort),
|
||||||
HostIP: port.HostIP,
|
HostIP: port.HostIP,
|
||||||
},
|
}
|
||||||
|
|
||||||
|
// Allow multiple host ports bind to same docker port
|
||||||
|
if existedBindings, ok := portBindings[dockerPort]; ok {
|
||||||
|
// If a docker port already map to a host port, just append the host ports
|
||||||
|
portBindings[dockerPort] = append(existedBindings, hostBinding)
|
||||||
|
} else {
|
||||||
|
// Otherwise, it's fresh new port binding
|
||||||
|
portBindings[dockerPort] = []docker.PortBinding{
|
||||||
|
hostBinding,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return exposedPorts, portBindings
|
return exposedPorts, portBindings
|
||||||
|
Loading…
Reference in New Issue
Block a user