From 85a3483b4b9307341afe2ddefb7eacc43e951ccb Mon Sep 17 00:00:00 2001 From: Chris Telfer Date: Tue, 10 Apr 2018 00:36:19 -0400 Subject: [PATCH] Refactor [add|rm]LBBackend() to use lb struct This was passing extra information and adding confusion about the purpose of the load balancing structure. Signed-off-by: Chris Telfer --- libnetwork/service_common.go | 8 +++----- libnetwork/service_linux.go | 14 ++++++++++---- libnetwork/service_windows.go | 19 ++++++++++++++++--- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/libnetwork/service_common.go b/libnetwork/service_common.go index 8be2c38528..84ed4ed495 100644 --- a/libnetwork/service_common.go +++ b/libnetwork/service_common.go @@ -291,9 +291,7 @@ func (c *controller) addServiceBinding(svcName, svcID, nID, eID, containerName s // Add loadbalancer service and backend in all sandboxes in // the network only if vip is valid. - if len(vip) != 0 { - n.(*network).addLBBackend(ip, vip, lb, ingressPorts) - } + n.(*network).addLBBackend(ip, lb) // Add the appropriate name resolutions c.addEndpointNameResolution(svcName, svcID, nID, eID, containerName, vip, serviceAliases, taskAliases, ip, addService, "addServiceBinding") @@ -368,8 +366,8 @@ func (c *controller) rmServiceBinding(svcName, svcID, nID, eID, containerName st // Remove loadbalancer service(if needed) and backend in all // sandboxes in the network only if the vip is valid. - if len(vip) != 0 && entries == 0 { - n.(*network).rmLBBackend(ip, vip, lb, ingressPorts, rmService, fullRemove) + if entries == 0 { + n.(*network).rmLBBackend(ip, lb, rmService, fullRemove) } // Delete the name resolutions diff --git a/libnetwork/service_linux.go b/libnetwork/service_linux.go index a95ece8a08..bf6c0d4fb8 100644 --- a/libnetwork/service_linux.go +++ b/libnetwork/service_linux.go @@ -114,7 +114,10 @@ func (sb *sandbox) populateLoadbalancers(ep *endpoint) { // Add loadbalancer backend to all sandboxes which has a connection to // this network. If needed add the service as well. -func (n *network) addLBBackend(ip, vip net.IP, lb *loadBalancer, ingressPorts []*PortConfig) { +func (n *network) addLBBackend(ip net.IP, lb *loadBalancer) { + if len(lb.vip) == 0 { + return + } n.WalkEndpoints(func(e Endpoint) bool { ep := e.(*endpoint) if sb, ok := ep.getSandbox(); ok { @@ -127,7 +130,7 @@ func (n *network) addLBBackend(ip, vip net.IP, lb *loadBalancer, ingressPorts [] gwIP = ep.Iface().Address().IP } - sb.addLBBackend(ip, vip, lb.fwMark, ingressPorts, ep.Iface().Address(), gwIP, n.ingress) + sb.addLBBackend(ip, lb.vip, lb.fwMark, lb.service.ingressPorts, ep.Iface().Address(), gwIP, n.ingress) } return false @@ -137,7 +140,10 @@ func (n *network) addLBBackend(ip, vip net.IP, lb *loadBalancer, ingressPorts [] // Remove loadbalancer backend from all sandboxes which has a // connection to this network. If needed remove the service entry as // well, as specified by the rmService bool. -func (n *network) rmLBBackend(ip, vip net.IP, lb *loadBalancer, ingressPorts []*PortConfig, rmService bool, fullRemove bool) { +func (n *network) rmLBBackend(ip net.IP, lb *loadBalancer, rmService bool, fullRemove bool) { + if len(lb.vip) == 0 { + return + } n.WalkEndpoints(func(e Endpoint) bool { ep := e.(*endpoint) if sb, ok := ep.getSandbox(); ok { @@ -150,7 +156,7 @@ func (n *network) rmLBBackend(ip, vip net.IP, lb *loadBalancer, ingressPorts []* gwIP = ep.Iface().Address().IP } - sb.rmLBBackend(ip, vip, lb.fwMark, ingressPorts, ep.Iface().Address(), gwIP, rmService, fullRemove, n.ingress) + sb.rmLBBackend(ip, lb.vip, lb.fwMark, lb.service.ingressPorts, ep.Iface().Address(), gwIP, rmService, fullRemove, n.ingress) } return false diff --git a/libnetwork/service_windows.go b/libnetwork/service_windows.go index a9ce244cfd..e903147247 100644 --- a/libnetwork/service_windows.go +++ b/libnetwork/service_windows.go @@ -19,7 +19,13 @@ func init() { lbPolicylistMap = make(map[*loadBalancer]*policyLists) } -func (n *network) addLBBackend(ip, vip net.IP, lb *loadBalancer, ingressPorts []*PortConfig) { +func (n *network) addLBBackend(ip net.IP, lb *loadBalancer) { + if len(lb.vip) == 0 { + return + } + + vip := lb.vip + ingressPorts := lb.service.ingressPorts if system.GetOSVersion().Build > 16236 { lb.Lock() @@ -117,11 +123,18 @@ func (n *network) addLBBackend(ip, vip net.IP, lb *loadBalancer, ingressPorts [] } } -func (n *network) rmLBBackend(ip, vip net.IP, lb *loadBalancer, ingressPorts []*PortConfig, rmService bool, fullRemove bool) { +func (n *network) rmLBBackend(ip net.IP, lb *loadBalancer, rmService bool, fullRemove bool) { + if len(lb.vip) == 0 { + return + } + + vip := lb.vip + ingressPorts := lb.service.ingressPorts + if system.GetOSVersion().Build > 16236 { if numEnabledBackends(lb) > 0 { //Reprogram HNS (actually VFP) with the existing backends. - n.addLBBackend(ip, vip, lb, ingressPorts) + n.addLBBackend(ip, lb) } else { lb.Lock() defer lb.Unlock()