mirror of https://github.com/k3s-io/k3s
Allow multiple host ports map to the same port in container
parent
093614ec32
commit
18315db025
|
@ -698,6 +698,11 @@ func TestMakePortsAndBindings(t *testing.T) {
|
||||||
HostPort: 445,
|
HostPort: 445,
|
||||||
Protocol: "foobar",
|
Protocol: "foobar",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
ContainerPort: 443,
|
||||||
|
HostPort: 446,
|
||||||
|
Protocol: "tcp",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
exposedPorts, bindings := makePortsAndBindings(ports)
|
exposedPorts, bindings := makePortsAndBindings(ports)
|
||||||
if len(ports) != len(exposedPorts) ||
|
if len(ports) != len(exposedPorts) ||
|
||||||
|
@ -734,6 +739,14 @@ func TestMakePortsAndBindings(t *testing.T) {
|
||||||
if value[0].HostIP != "" {
|
if value[0].HostIP != "" {
|
||||||
t.Errorf("Unexpected host IP: %s", 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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -573,11 +573,21 @@ func makePortsAndBindings(portMappings []kubecontainer.PortMapping) (map[docker.
|
||||||
}
|
}
|
||||||
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 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
|
return exposedPorts, portBindings
|
||||||
|
|
Loading…
Reference in New Issue