|
@@ -15,7 +15,7 @@ var (
|
|
defaultBindingIP = net.IPv4(0, 0, 0, 0)
|
|
defaultBindingIP = net.IPv4(0, 0, 0, 0)
|
|
)
|
|
)
|
|
|
|
|
|
-func allocatePorts(epConfig *endpointConfiguration, intf *sandbox.Interface, reqDefBindIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) {
|
|
|
|
|
|
+func (n *bridgeNetwork) allocatePorts(epConfig *endpointConfiguration, intf *sandbox.Interface, reqDefBindIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) {
|
|
if epConfig == nil || epConfig.PortBindings == nil {
|
|
if epConfig == nil || epConfig.PortBindings == nil {
|
|
return nil, nil
|
|
return nil, nil
|
|
}
|
|
}
|
|
@@ -25,16 +25,16 @@ func allocatePorts(epConfig *endpointConfiguration, intf *sandbox.Interface, req
|
|
defHostIP = reqDefBindIP
|
|
defHostIP = reqDefBindIP
|
|
}
|
|
}
|
|
|
|
|
|
- return allocatePortsInternal(epConfig.PortBindings, intf.Address.IP, defHostIP, ulPxyEnabled)
|
|
|
|
|
|
+ return n.allocatePortsInternal(epConfig.PortBindings, intf.Address.IP, defHostIP, ulPxyEnabled)
|
|
}
|
|
}
|
|
|
|
|
|
-func allocatePortsInternal(bindings []types.PortBinding, containerIP, defHostIP net.IP, ulPxyEnabled bool) ([]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 := make([]types.PortBinding, 0, len(bindings))
|
|
for _, c := range bindings {
|
|
for _, c := range bindings {
|
|
b := c.GetCopy()
|
|
b := c.GetCopy()
|
|
- if err := allocatePort(&b, containerIP, defHostIP, ulPxyEnabled); err != nil {
|
|
|
|
|
|
+ if err := n.allocatePort(&b, containerIP, defHostIP, ulPxyEnabled); err != nil {
|
|
// On allocation failure, release previously allocated ports. On cleanup error, just log a warning message
|
|
// On allocation failure, release previously allocated ports. On cleanup error, just log a warning message
|
|
- if cuErr := releasePortsInternal(bs); cuErr != nil {
|
|
|
|
|
|
+ if cuErr := n.releasePortsInternal(bs); cuErr != nil {
|
|
logrus.Warnf("Upon allocation failure for %v, failed to clear previously allocated port bindings: %v", b, cuErr)
|
|
logrus.Warnf("Upon allocation failure for %v, failed to clear previously allocated port bindings: %v", b, cuErr)
|
|
}
|
|
}
|
|
return nil, err
|
|
return nil, err
|
|
@@ -44,7 +44,7 @@ func allocatePortsInternal(bindings []types.PortBinding, containerIP, defHostIP
|
|
return bs, nil
|
|
return bs, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func allocatePort(bnd *types.PortBinding, containerIP, defHostIP net.IP, ulPxyEnabled bool) error {
|
|
|
|
|
|
+func (n *bridgeNetwork) allocatePort(bnd *types.PortBinding, containerIP, defHostIP net.IP, ulPxyEnabled bool) error {
|
|
var (
|
|
var (
|
|
host net.Addr
|
|
host net.Addr
|
|
err error
|
|
err error
|
|
@@ -66,7 +66,7 @@ func allocatePort(bnd *types.PortBinding, containerIP, defHostIP net.IP, ulPxyEn
|
|
|
|
|
|
// Try up to maxAllocatePortAttempts times to get a port that's not already allocated.
|
|
// Try up to maxAllocatePortAttempts times to get a port that's not already allocated.
|
|
for i := 0; i < maxAllocatePortAttempts; i++ {
|
|
for i := 0; i < maxAllocatePortAttempts; i++ {
|
|
- if host, err = portMapper.Map(container, bnd.HostIP, int(bnd.HostPort), ulPxyEnabled); err == nil {
|
|
|
|
|
|
+ if host, err = n.portMapper.Map(container, bnd.HostIP, int(bnd.HostPort), ulPxyEnabled); err == nil {
|
|
break
|
|
break
|
|
}
|
|
}
|
|
// There is no point in immediately retrying to map an explicitly chosen port.
|
|
// There is no point in immediately retrying to map an explicitly chosen port.
|
|
@@ -94,16 +94,16 @@ func allocatePort(bnd *types.PortBinding, containerIP, defHostIP net.IP, ulPxyEn
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func releasePorts(ep *bridgeEndpoint) error {
|
|
|
|
- return releasePortsInternal(ep.portMapping)
|
|
|
|
|
|
+func (n *bridgeNetwork) releasePorts(ep *bridgeEndpoint) error {
|
|
|
|
+ return n.releasePortsInternal(ep.portMapping)
|
|
}
|
|
}
|
|
|
|
|
|
-func releasePortsInternal(bindings []types.PortBinding) error {
|
|
|
|
|
|
+func (n *bridgeNetwork) releasePortsInternal(bindings []types.PortBinding) error {
|
|
var errorBuf bytes.Buffer
|
|
var errorBuf bytes.Buffer
|
|
|
|
|
|
// Attempt to release all port bindings, do not stop on failure
|
|
// Attempt to release all port bindings, do not stop on failure
|
|
for _, m := range bindings {
|
|
for _, m := range bindings {
|
|
- if err := releasePort(m); err != nil {
|
|
|
|
|
|
+ if err := n.releasePort(m); err != nil {
|
|
errorBuf.WriteString(fmt.Sprintf("\ncould not release %v because of %v", m, err))
|
|
errorBuf.WriteString(fmt.Sprintf("\ncould not release %v because of %v", m, err))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -114,11 +114,11 @@ func releasePortsInternal(bindings []types.PortBinding) error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func releasePort(bnd types.PortBinding) error {
|
|
|
|
|
|
+func (n *bridgeNetwork) releasePort(bnd types.PortBinding) error {
|
|
// Construct the host side transport address
|
|
// Construct the host side transport address
|
|
host, err := bnd.HostAddr()
|
|
host, err := bnd.HostAddr()
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- return portMapper.Unmap(host)
|
|
|
|
|
|
+ return n.portMapper.Unmap(host)
|
|
}
|
|
}
|