mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
reworked allocatePorts
Signed-off-by: Benjamin Böhmke <benjamin@boehmke.net>
This commit is contained in:
parent
9f98bd79d8
commit
648d891827
3 changed files with 24 additions and 21 deletions
|
@ -26,23 +26,28 @@ func (n *bridgeNetwork) allocatePorts(ep *bridgeEndpoint, reqDefBindIP net.IP, u
|
||||||
defHostIP = reqDefBindIP
|
defHostIP = reqDefBindIP
|
||||||
}
|
}
|
||||||
|
|
||||||
var pb []types.PortBinding
|
// IPv4 port binding including user land proxy
|
||||||
|
pb, err := n.allocatePortsInternal(ep.extConnConfig.PortBindings, ep.addr.IP, defHostIP, ulPxyEnabled)
|
||||||
if ep.addrv6 != nil {
|
if err != nil {
|
||||||
pb, _ = n.allocatePortsInternal(ep.extConnConfig.PortBindings, ep.addrv6.IP, defaultBindingIPV6, ulPxyEnabled, nil)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return n.allocatePortsInternal(ep.extConnConfig.PortBindings, ep.addr.IP, defHostIP, ulPxyEnabled, pb)
|
// IPv6 port binding excluding user land proxy
|
||||||
|
if n.driver.config.EnableIP6Tables && ep.addrv6 != nil {
|
||||||
|
pbv6, err := n.allocatePortsInternal(ep.extConnConfig.PortBindings, ep.addrv6.IP, defaultBindingIPV6, false)
|
||||||
|
if err != nil {
|
||||||
|
// ensure we clear the previous allocated IPv4 ports
|
||||||
|
n.releasePortsInternal(pb)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
pb = append(pb, pbv6...)
|
||||||
|
}
|
||||||
|
return pb, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *bridgeNetwork) allocatePortsInternal(bindings []types.PortBinding, containerIP, defHostIP net.IP, ulPxyEnabled bool, existingPortBindings []types.PortBinding) ([]types.PortBinding, error) {
|
func (n *bridgeNetwork) allocatePortsInternal(bindings []types.PortBinding, containerIP, defHostIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) {
|
||||||
|
bs := make([]types.PortBinding, 0, len(bindings))
|
||||||
bs := existingPortBindings
|
|
||||||
|
|
||||||
if existingPortBindings == nil {
|
|
||||||
bs = make([]types.PortBinding, 0, len(bindings))
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, c := range bindings {
|
for _, c := range bindings {
|
||||||
b := c.GetCopy()
|
b := c.GetCopy()
|
||||||
if err := n.allocatePort(&b, containerIP, defHostIP, ulPxyEnabled); err != nil {
|
if err := n.allocatePort(&b, containerIP, defHostIP, ulPxyEnabled); err != nil {
|
||||||
|
|
|
@ -195,7 +195,7 @@ func TestBridge(t *testing.T) {
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("Unexpected format for port mapping in endpoint operational data")
|
t.Fatalf("Unexpected format for port mapping in endpoint operational data")
|
||||||
}
|
}
|
||||||
if len(pm) != 10 {
|
if len(pm) != 5 {
|
||||||
t.Fatalf("Incomplete data for port mapping in endpoint operational data: %d", len(pm))
|
t.Fatalf("Incomplete data for port mapping in endpoint operational data: %d", len(pm))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -160,7 +160,7 @@ func (pm *PortMapper) MapRange(container net.Addr, hostIP net.IP, hostPortStart,
|
||||||
cleanup := func() error {
|
cleanup := func() error {
|
||||||
// need to undo the iptables rules before we return
|
// need to undo the iptables rules before we return
|
||||||
m.userlandProxy.Stop()
|
m.userlandProxy.Stop()
|
||||||
if hostIP.To4() != nil {
|
if hostIP.To4() != nil || hostIP.To16() != nil {
|
||||||
pm.DeleteForwardingTableEntry(m.proto, hostIP, allocatedHostPort, containerIP.String(), containerPort)
|
pm.DeleteForwardingTableEntry(m.proto, hostIP, allocatedHostPort, containerIP.String(), containerPort)
|
||||||
if err := pm.Allocator.ReleasePort(hostIP, m.proto, allocatedHostPort); err != nil {
|
if err := pm.Allocator.ReleasePort(hostIP, m.proto, allocatedHostPort); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -170,13 +170,11 @@ func (pm *PortMapper) MapRange(container net.Addr, hostIP net.IP, hostPortStart,
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if hostIP.To4() != nil {
|
if err := m.userlandProxy.Start(); err != nil {
|
||||||
if err := m.userlandProxy.Start(); err != nil {
|
if err := cleanup(); err != nil {
|
||||||
if err := cleanup(); err != nil {
|
return nil, fmt.Errorf("Error during port allocation cleanup: %v", err)
|
||||||
return nil, fmt.Errorf("Error during port allocation cleanup: %v", err)
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pm.currentMappings[key] = m
|
pm.currentMappings[key] = m
|
||||||
|
|
Loading…
Add table
Reference in a new issue