Skip to content

Commit

Permalink
Refactor loadbalancing naming + fix golint issues
Browse files Browse the repository at this point in the history
The load balancing naming required refactoring and also external
visibility in order to be referenced by moby.

Signed-off-by: Chris Telfer <ctelfer@docker.com>
  • Loading branch information
ctelfer committed Apr 18, 2018
1 parent eed026e commit 47aac5e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
27 changes: 17 additions & 10 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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...)
Expand Down
4 changes: 2 additions & 2 deletions sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
49 changes: 26 additions & 23 deletions service_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (sb *sandbox) populateLoadBalancers(ep *endpoint) {
}
}

if n.LoadBalanceMode() != lbModeMesh {
if n.LoadBalacingMode() != LBModeMesh {
return
}

Expand Down Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -860,15 +860,15 @@ 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)
}
}

vip := os.Args[2]
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]

Expand All @@ -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))
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit 47aac5e

Please sign in to comment.