|
@@ -4,7 +4,6 @@ import (
|
|
|
"net"
|
|
|
|
|
|
"github.com/Microsoft/hcsshim"
|
|
|
- "github.com/Microsoft/hcsshim/osversion"
|
|
|
"github.com/sirupsen/logrus"
|
|
|
)
|
|
|
|
|
@@ -23,98 +22,96 @@ func (n *network) addLBBackend(ip net.IP, lb *loadBalancer) {
|
|
|
vip := lb.vip
|
|
|
ingressPorts := lb.service.ingressPorts
|
|
|
|
|
|
- if osversion.Build() > 16236 {
|
|
|
- lb.Lock()
|
|
|
- defer lb.Unlock()
|
|
|
- //find the load balancer IP for the network.
|
|
|
- var sourceVIP string
|
|
|
- for _, e := range n.Endpoints() {
|
|
|
- epInfo := e.Info()
|
|
|
- if epInfo == nil {
|
|
|
- continue
|
|
|
- }
|
|
|
- if epInfo.LoadBalancer() {
|
|
|
- sourceVIP = epInfo.Iface().Address().IP.String()
|
|
|
- break
|
|
|
- }
|
|
|
+ lb.Lock()
|
|
|
+ defer lb.Unlock()
|
|
|
+ //find the load balancer IP for the network.
|
|
|
+ var sourceVIP string
|
|
|
+ for _, e := range n.Endpoints() {
|
|
|
+ epInfo := e.Info()
|
|
|
+ if epInfo == nil {
|
|
|
+ continue
|
|
|
}
|
|
|
-
|
|
|
- if sourceVIP == "" {
|
|
|
- logrus.Errorf("Failed to find load balancer IP for network %s", n.Name())
|
|
|
- return
|
|
|
+ if epInfo.LoadBalancer() {
|
|
|
+ sourceVIP = epInfo.Iface().Address().IP.String()
|
|
|
+ break
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- var endpoints []hcsshim.HNSEndpoint
|
|
|
+ if sourceVIP == "" {
|
|
|
+ logrus.Errorf("Failed to find load balancer IP for network %s", n.Name())
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- for eid, be := range lb.backEnds {
|
|
|
- if be.disabled {
|
|
|
- continue
|
|
|
- }
|
|
|
- //Call HNS to get back ID (GUID) corresponding to the endpoint.
|
|
|
- hnsEndpoint, err := hcsshim.GetHNSEndpointByName(eid)
|
|
|
- if err != nil {
|
|
|
- logrus.Errorf("Failed to find HNS ID for endpoint %v: %v", eid, err)
|
|
|
- return
|
|
|
- }
|
|
|
+ var endpoints []hcsshim.HNSEndpoint
|
|
|
|
|
|
- endpoints = append(endpoints, *hnsEndpoint)
|
|
|
+ for eid, be := range lb.backEnds {
|
|
|
+ if be.disabled {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ //Call HNS to get back ID (GUID) corresponding to the endpoint.
|
|
|
+ hnsEndpoint, err := hcsshim.GetHNSEndpointByName(eid)
|
|
|
+ if err != nil {
|
|
|
+ logrus.Errorf("Failed to find HNS ID for endpoint %v: %v", eid, err)
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
- if policies, ok := lbPolicylistMap[lb]; ok {
|
|
|
+ endpoints = append(endpoints, *hnsEndpoint)
|
|
|
+ }
|
|
|
|
|
|
- if policies.ilb != nil {
|
|
|
- policies.ilb.Delete()
|
|
|
- policies.ilb = nil
|
|
|
- }
|
|
|
+ if policies, ok := lbPolicylistMap[lb]; ok {
|
|
|
|
|
|
- if policies.elb != nil {
|
|
|
- policies.elb.Delete()
|
|
|
- policies.elb = nil
|
|
|
- }
|
|
|
- delete(lbPolicylistMap, lb)
|
|
|
+ if policies.ilb != nil {
|
|
|
+ policies.ilb.Delete()
|
|
|
+ policies.ilb = nil
|
|
|
}
|
|
|
|
|
|
- ilbPolicy, err := hcsshim.AddLoadBalancer(endpoints, true, sourceVIP, vip.String(), 0, 0, 0)
|
|
|
- if err != nil {
|
|
|
- logrus.Errorf("Failed to add ILB policy for service %s (%s) with endpoints %v using load balancer IP %s on network %s: %v",
|
|
|
- lb.service.name, vip.String(), endpoints, sourceVIP, n.Name(), err)
|
|
|
- return
|
|
|
+ if policies.elb != nil {
|
|
|
+ policies.elb.Delete()
|
|
|
+ policies.elb = nil
|
|
|
}
|
|
|
+ delete(lbPolicylistMap, lb)
|
|
|
+ }
|
|
|
|
|
|
- lbPolicylistMap[lb] = &policyLists{
|
|
|
- ilb: ilbPolicy,
|
|
|
- }
|
|
|
+ ilbPolicy, err := hcsshim.AddLoadBalancer(endpoints, true, sourceVIP, vip.String(), 0, 0, 0)
|
|
|
+ if err != nil {
|
|
|
+ logrus.Errorf("Failed to add ILB policy for service %s (%s) with endpoints %v using load balancer IP %s on network %s: %v",
|
|
|
+ lb.service.name, vip.String(), endpoints, sourceVIP, n.Name(), err)
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- publishedPorts := make(map[uint32]uint32)
|
|
|
+ lbPolicylistMap[lb] = &policyLists{
|
|
|
+ ilb: ilbPolicy,
|
|
|
+ }
|
|
|
|
|
|
- for i, port := range ingressPorts {
|
|
|
- protocol := uint16(6)
|
|
|
+ publishedPorts := make(map[uint32]uint32)
|
|
|
|
|
|
- // Skip already published port
|
|
|
- if publishedPorts[port.PublishedPort] == port.TargetPort {
|
|
|
- continue
|
|
|
- }
|
|
|
+ for i, port := range ingressPorts {
|
|
|
+ protocol := uint16(6)
|
|
|
|
|
|
- if port.Protocol == ProtocolUDP {
|
|
|
- protocol = 17
|
|
|
- }
|
|
|
+ // Skip already published port
|
|
|
+ if publishedPorts[port.PublishedPort] == port.TargetPort {
|
|
|
+ continue
|
|
|
+ }
|
|
|
|
|
|
- // check if already has udp matching to add wild card publishing
|
|
|
- for j := i + 1; j < len(ingressPorts); j++ {
|
|
|
- if ingressPorts[j].TargetPort == port.TargetPort &&
|
|
|
- ingressPorts[j].PublishedPort == port.PublishedPort {
|
|
|
- protocol = 0
|
|
|
- }
|
|
|
+ if port.Protocol == ProtocolUDP {
|
|
|
+ protocol = 17
|
|
|
+ }
|
|
|
+
|
|
|
+ // check if already has udp matching to add wild card publishing
|
|
|
+ for j := i + 1; j < len(ingressPorts); j++ {
|
|
|
+ if ingressPorts[j].TargetPort == port.TargetPort &&
|
|
|
+ ingressPorts[j].PublishedPort == port.PublishedPort {
|
|
|
+ protocol = 0
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- publishedPorts[port.PublishedPort] = port.TargetPort
|
|
|
+ publishedPorts[port.PublishedPort] = port.TargetPort
|
|
|
|
|
|
- lbPolicylistMap[lb].elb, err = hcsshim.AddLoadBalancer(endpoints, false, sourceVIP, "", protocol, uint16(port.TargetPort), uint16(port.PublishedPort))
|
|
|
- if err != nil {
|
|
|
- logrus.Errorf("Failed to add ELB policy for service %s (ip:%s target port:%v published port:%v) with endpoints %v using load balancer IP %s on network %s: %v",
|
|
|
- lb.service.name, vip.String(), uint16(port.TargetPort), uint16(port.PublishedPort), endpoints, sourceVIP, n.Name(), err)
|
|
|
- return
|
|
|
- }
|
|
|
+ lbPolicylistMap[lb].elb, err = hcsshim.AddLoadBalancer(endpoints, false, sourceVIP, "", protocol, uint16(port.TargetPort), uint16(port.PublishedPort))
|
|
|
+ if err != nil {
|
|
|
+ logrus.Errorf("Failed to add ELB policy for service %s (ip:%s target port:%v published port:%v) with endpoints %v using load balancer IP %s on network %s: %v",
|
|
|
+ lb.service.name, vip.String(), uint16(port.TargetPort), uint16(port.PublishedPort), endpoints, sourceVIP, n.Name(), err)
|
|
|
+ return
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -124,30 +121,28 @@ func (n *network) rmLBBackend(ip net.IP, lb *loadBalancer, rmService bool, fullR
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- if osversion.Build() > 16236 {
|
|
|
- if numEnabledBackends(lb) > 0 {
|
|
|
- //Reprogram HNS (actually VFP) with the existing backends.
|
|
|
- n.addLBBackend(ip, lb)
|
|
|
- } else {
|
|
|
- lb.Lock()
|
|
|
- defer lb.Unlock()
|
|
|
- logrus.Debugf("No more backends for service %s (ip:%s). Removing all policies", lb.service.name, lb.vip.String())
|
|
|
-
|
|
|
- if policyLists, ok := lbPolicylistMap[lb]; ok {
|
|
|
- if policyLists.ilb != nil {
|
|
|
- policyLists.ilb.Delete()
|
|
|
- policyLists.ilb = nil
|
|
|
- }
|
|
|
-
|
|
|
- if policyLists.elb != nil {
|
|
|
- policyLists.elb.Delete()
|
|
|
- policyLists.elb = nil
|
|
|
- }
|
|
|
- delete(lbPolicylistMap, lb)
|
|
|
-
|
|
|
- } else {
|
|
|
- logrus.Errorf("Failed to find policies for service %s (%s)", lb.service.name, lb.vip.String())
|
|
|
+ if numEnabledBackends(lb) > 0 {
|
|
|
+ // Reprogram HNS (actually VFP) with the existing backends.
|
|
|
+ n.addLBBackend(ip, lb)
|
|
|
+ } else {
|
|
|
+ lb.Lock()
|
|
|
+ defer lb.Unlock()
|
|
|
+ logrus.Debugf("No more backends for service %s (ip:%s). Removing all policies", lb.service.name, lb.vip.String())
|
|
|
+
|
|
|
+ if policyLists, ok := lbPolicylistMap[lb]; ok {
|
|
|
+ if policyLists.ilb != nil {
|
|
|
+ policyLists.ilb.Delete()
|
|
|
+ policyLists.ilb = nil
|
|
|
}
|
|
|
+
|
|
|
+ if policyLists.elb != nil {
|
|
|
+ policyLists.elb.Delete()
|
|
|
+ policyLists.elb = nil
|
|
|
+ }
|
|
|
+ delete(lbPolicylistMap, lb)
|
|
|
+
|
|
|
+ } else {
|
|
|
+ logrus.Errorf("Failed to find policies for service %s (%s)", lb.service.name, lb.vip.String())
|
|
|
}
|
|
|
}
|
|
|
}
|