From 47aac5e2992fa647b7f7ca9e87dad1e8a7d8945f Mon Sep 17 00:00:00 2001 From: Chris Telfer Date: Mon, 16 Apr 2018 12:55:10 -0400 Subject: [PATCH] Refactor loadbalancing naming + fix golint issues The load balancing naming required refactoring and also external visibility in order to be referenced by moby. Signed-off-by: Chris Telfer --- network.go | 27 ++++++++++++++++---------- sandbox.go | 4 ++-- service_linux.go | 49 +++++++++++++++++++++++++----------------------- 3 files changed, 45 insertions(+), 35 deletions(-) diff --git a/network.go b/network.go index 0c421a53ab..d7f2b839a6 100644 --- a/network.go +++ b/network.go @@ -74,7 +74,7 @@ type NetworkInfo interface { Internal() bool Attachable() bool Ingress() bool - LoadBalanceMode() string + LoadBalacingMode() string ConfigFrom() string ConfigOnly() bool Labels() map[string]string @@ -244,8 +244,16 @@ type network struct { } const ( - lbModeMesh = "mesh" - lbModeEndpoint = "endpoint" + // LBModeMesh is the load balancing mode where every container in + // a network gets the load balancing configuration of all services + // in the network. + LBModeMesh = "mesh" + + // LBModeEndpoint is the load balancing mode where each node that + // instantiates the network allocates an endpoint to perform load + // balancing for all containers running on the node accessing + // services in said network. + LBModeEndpoint = "endpoint" ) func (n *network) Name() string { @@ -987,7 +995,7 @@ func (n *network) delete(force bool, delIngress bool) error { } // Check that the network is empty - var emptyCount uint64 = 0 + var emptyCount uint64 if n.hasLoadBalancerEndpoint() { emptyCount = 1 } @@ -1075,7 +1083,7 @@ func (n *network) delete(force bool, delIngress bool) error { // XXX This is ONLY needed for mesh-mode of load balancing // because this comes for free above when we remove the // load balancing sandbox and endpoint. - if n.LoadBalanceMode() == lbModeMesh { + if n.LoadBalacingMode() == LBModeMesh { c.cleanupServiceBindings(n.ID()) } @@ -1869,15 +1877,14 @@ func (n *network) Ingress() bool { return n.ingress } -func (n *network) LoadBalanceMode() string { +func (n *network) LoadBalacingMode() string { n.Lock() defer n.Unlock() if n.hasLoadBalancerEndpoint() { - return lbModeEndpoint - } else { - return lbModeMesh + return LBModeEndpoint } + return LBModeMesh } func (n *network) Dynamic() bool { @@ -2159,7 +2166,7 @@ func (n *network) createLoadBalancerSandbox() error { CreateOptionIpam(n.loadBalancerIP, nil, nil, nil), CreateOptionLoadBalancer(), } - if n.LoadBalanceMode() == lbModeEndpoint && !n.ingress { + if n.LoadBalacingMode() == LBModeEndpoint && !n.ingress { epOptions = append(epOptions, CreateOptionAnonymous()) } ep, err := n.createEndpoint(endpointName, epOptions...) diff --git a/sandbox.go b/sandbox.go index 9cf4d11dca..a0e7668a66 100644 --- a/sandbox.go +++ b/sandbox.go @@ -730,7 +730,7 @@ func releaseOSSboxResources(osSbox osl.Sandbox, ep *endpoint) { n := ep.network ep.Unlock() - if len(vip) != 0 && n.LoadBalanceMode() == lbModeMesh { + if len(vip) != 0 && n.LoadBalacingMode() == LBModeMesh { loopName := osSbox.GetLoopbackIfaceName() if err := osSbox.RemoveAliasIP(loopName, &net.IPNet{IP: vip, Mask: net.CIDRMask(32, 32)}); err != nil { logrus.Warnf("Remove virtual IP %v failed: %v", vip, err) @@ -850,7 +850,7 @@ func (sb *sandbox) populateNetworkResources(ep *endpoint) error { } } - if len(ep.virtualIP) != 0 && n.LoadBalanceMode() == lbModeMesh { + if len(ep.virtualIP) != 0 && n.LoadBalacingMode() == LBModeMesh { loopName := sb.osSbox.GetLoopbackIfaceName() err := sb.osSbox.AddAliasIP(loopName, &net.IPNet{IP: ep.virtualIP, Mask: net.CIDRMask(32, 32)}) if err != nil { diff --git a/service_linux.go b/service_linux.go index 12a3cc2b52..2b1f37086b 100644 --- a/service_linux.go +++ b/service_linux.go @@ -78,7 +78,7 @@ func (sb *sandbox) populateLoadBalancers(ep *endpoint) { } } - if n.LoadBalanceMode() != lbModeMesh { + if n.LoadBalacingMode() != LBModeMesh { return } @@ -169,13 +169,13 @@ func (n *network) addLBBackend(ip net.IP, lb *loadBalancer) { if len(lb.vip) == 0 { return } - switch n.LoadBalanceMode() { - case lbModeEndpoint: + switch n.LoadBalacingMode() { + case LBModeEndpoint: n.addLBBackendToEndpoint(ip, lb) - case lbModeMesh: + case LBModeMesh: n.addLBBackendToMesh(ip, lb) default: - logrus.Errorf("addLBBackend: network %s is in unknown load balancer mode (%s)!", n.ID(), n.LoadBalanceMode()) + logrus.Errorf("addLBBackend: network %s is in unknown load balancer mode (%s)!", n.ID(), n.LoadBalacingMode()) } } @@ -233,7 +233,7 @@ func (n *network) addLBBackendToEndpoint(ip net.IP, lb *loadBalancer) { } logrus.Debugf("Creating service for vip %s fwMark %d ingressPorts %#v in sbox %s (%s)", lb.vip, lb.fwMark, lb.service.ingressPorts, sb.ID()[0:7], sb.ContainerID()[0:7]) - if err := invokeFWMarker(sb.Key(), lb.vip, lb.fwMark, lb.service.ingressPorts, eIP, false, lbModeEndpoint); err != nil { + if err := invokeFWMarker(sb.Key(), lb.vip, lb.fwMark, lb.service.ingressPorts, eIP, false, LBModeEndpoint); err != nil { logrus.Errorf("Failed to add firewall mark rule in sbox %s (%s): %v", sb.ID()[0:7], sb.ContainerID()[0:7], err) return } @@ -282,13 +282,13 @@ func (n *network) rmLBBackend(ip net.IP, lb *loadBalancer, rmService bool, fullR if len(lb.vip) == 0 { return } - switch n.LoadBalanceMode() { - case lbModeEndpoint: + switch n.LoadBalacingMode() { + case LBModeEndpoint: n.rmLBBackendFromEndpoint(ip, lb, rmService, fullRemove) - case lbModeMesh: + case LBModeMesh: n.rmLBBackendFromMesh(ip, lb, rmService, fullRemove) default: - logrus.Errorf("rmLBBackend: network %s is in unknown load balancer mode (%s)!", n.ID(), n.LoadBalanceMode()) + logrus.Errorf("rmLBBackend: network %s is in unknown load balancer mode (%s)!", n.ID(), n.LoadBalacingMode()) } } @@ -358,7 +358,7 @@ func (n *network) rmLBBackendFromEndpoint(ip net.IP, lb *loadBalancer, rmService } } - if err := invokeFWMarker(sb.Key(), lb.vip, lb.fwMark, lb.service.ingressPorts, eIP, true, lbModeEndpoint); err != nil { + if err := invokeFWMarker(sb.Key(), lb.vip, lb.fwMark, lb.service.ingressPorts, eIP, true, LBModeEndpoint); err != nil { logrus.Errorf("Failed to delete firewall mark rule in sbox %s (%s): %v", sb.ID()[0:7], sb.ContainerID()[0:7], err) } @@ -433,7 +433,7 @@ func (sb *sandbox) addLBBackend(ip, vip net.IP, fwMark uint32, ingressPorts []*P } logrus.Debugf("Creating service for vip %s fwMark %d ingressPorts %#v in sbox %s (%s)", vip, fwMark, ingressPorts, sb.ID()[0:7], sb.ContainerID()[0:7]) - if err := invokeFWMarker(sb.Key(), vip, fwMark, ingressPorts, eIP, false, lbModeMesh); err != nil { + if err := invokeFWMarker(sb.Key(), vip, fwMark, ingressPorts, eIP, false, LBModeMesh); err != nil { logrus.Errorf("Failed to add firewall mark rule in sbox %s (%s): %v", sb.ID()[0:7], sb.ContainerID()[0:7], err) return } @@ -511,7 +511,7 @@ func (sb *sandbox) rmLBBackend(ip, vip net.IP, fwMark uint32, ingressPorts []*Po } } - if err := invokeFWMarker(sb.Key(), vip, fwMark, ingressPorts, eIP, true, lbModeMesh); err != nil { + if err := invokeFWMarker(sb.Key(), vip, fwMark, ingressPorts, eIP, true, LBModeMesh); err != nil { logrus.Errorf("Failed to delete firewall mark rule in sbox %s (%s): %v", sb.ID()[0:7], sb.ContainerID()[0:7], err) } } @@ -860,7 +860,7 @@ func fwMarker() { ingressPorts, err = readPortsFromFile(os.Args[5]) if err != nil { logrus.Errorf("Failed reading ingress ports file: %v", err) - os.Exit(6) + os.Exit(2) } } @@ -868,7 +868,7 @@ func fwMarker() { fwMark, err := strconv.ParseUint(os.Args[3], 10, 32) if err != nil { logrus.Errorf("bad fwmark value(%s) passed: %v", os.Args[3], err) - os.Exit(2) + os.Exit(3) } addDelOpt := os.Args[4] @@ -882,20 +882,20 @@ func fwMarker() { ns, err := netns.GetFromPath(os.Args[1]) if err != nil { logrus.Errorf("failed get network namespace %q: %v", os.Args[1], err) - os.Exit(3) + os.Exit(4) } defer ns.Close() if err := netns.Set(ns); err != nil { logrus.Errorf("setting into container net ns %v failed, %v", os.Args[1], err) - os.Exit(4) + os.Exit(5) } if addDelOpt == "-A" { eIP, subnet, err := net.ParseCIDR(os.Args[6]) if err != nil { logrus.Errorf("Failed to parse endpoint IP %s: %v", os.Args[6], err) - os.Exit(9) + os.Exit(6) } ruleParams := strings.Fields(fmt.Sprintf("-m ipvs --ipvs -d %s -j SNAT --to-source %s", subnet, eIP)) @@ -906,27 +906,30 @@ func fwMarker() { err := ioutil.WriteFile("/proc/sys/net/ipv4/vs/conntrack", []byte{'1', '\n'}, 0644) if err != nil { logrus.Errorf("Failed to write to /proc/sys/net/ipv4/vs/conntrack: %v", err) - os.Exit(8) + os.Exit(7) } } } - if os.Args[7] == lbModeMesh { + switch os.Args[7] { + case LBModeMesh: rule := strings.Fields(fmt.Sprintf("-t mangle %s OUTPUT -d %s/32 -j MARK --set-mark %d", addDelOpt, vip, fwMark)) rules = append(rules, rule) rule = strings.Fields(fmt.Sprintf("-t nat %s OUTPUT -p icmp --icmp echo-request -d %s -j DNAT --to 127.0.0.1", addDelOpt, vip)) rules = append(rules, rule) - } else { - // lbModeEndpoint + case LBModeEndpoint: rule := strings.Fields(fmt.Sprintf("-t mangle %s INPUT -d %s/32 -j MARK --set-mark %d", addDelOpt, vip, fwMark)) rules = append(rules, rule) + default: + logrus.Errorf("fwMarker: received unknown load balancing mode (%s)!", os.Args[7]) + os.Exit(8) } for _, rule := range rules { if err := iptables.RawCombinedOutputNative(rule...); err != nil { logrus.Errorf("setting up rule failed, %v: %v", rule, err) - os.Exit(5) + os.Exit(9) } } }