github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/libnetwork/service_windows.go (about) 1 package libnetwork 2 3 import ( 4 "net" 5 6 "github.com/Microsoft/hcsshim" 7 "github.com/sirupsen/logrus" 8 ) 9 10 type policyLists struct { 11 ilb *hcsshim.PolicyList 12 elb *hcsshim.PolicyList 13 } 14 15 var lbPolicylistMap = make(map[*loadBalancer]*policyLists) 16 17 func (n *network) addLBBackend(ip net.IP, lb *loadBalancer) { 18 if len(lb.vip) == 0 { 19 return 20 } 21 22 vip := lb.vip 23 ingressPorts := lb.service.ingressPorts 24 25 lb.Lock() 26 defer lb.Unlock() 27 //find the load balancer IP for the network. 28 var sourceVIP string 29 for _, e := range n.Endpoints() { 30 epInfo := e.Info() 31 if epInfo == nil { 32 continue 33 } 34 if epInfo.LoadBalancer() { 35 sourceVIP = epInfo.Iface().Address().IP.String() 36 break 37 } 38 } 39 40 if sourceVIP == "" { 41 logrus.Errorf("Failed to find load balancer IP for network %s", n.Name()) 42 return 43 } 44 45 var endpoints []hcsshim.HNSEndpoint 46 47 for eid, be := range lb.backEnds { 48 if be.disabled { 49 continue 50 } 51 //Call HNS to get back ID (GUID) corresponding to the endpoint. 52 hnsEndpoint, err := hcsshim.GetHNSEndpointByName(eid) 53 if err != nil { 54 logrus.Errorf("Failed to find HNS ID for endpoint %v: %v", eid, err) 55 return 56 } 57 58 endpoints = append(endpoints, *hnsEndpoint) 59 } 60 61 if policies, ok := lbPolicylistMap[lb]; ok { 62 63 if policies.ilb != nil { 64 policies.ilb.Delete() 65 policies.ilb = nil 66 } 67 68 if policies.elb != nil { 69 policies.elb.Delete() 70 policies.elb = nil 71 } 72 delete(lbPolicylistMap, lb) 73 } 74 75 ilbPolicy, err := hcsshim.AddLoadBalancer(endpoints, true, sourceVIP, vip.String(), 0, 0, 0) 76 if err != nil { 77 logrus.Errorf("Failed to add ILB policy for service %s (%s) with endpoints %v using load balancer IP %s on network %s: %v", 78 lb.service.name, vip.String(), endpoints, sourceVIP, n.Name(), err) 79 return 80 } 81 82 lbPolicylistMap[lb] = &policyLists{ 83 ilb: ilbPolicy, 84 } 85 86 publishedPorts := make(map[uint32]uint32) 87 88 for i, port := range ingressPorts { 89 protocol := uint16(6) 90 91 // Skip already published port 92 if publishedPorts[port.PublishedPort] == port.TargetPort { 93 continue 94 } 95 96 if port.Protocol == ProtocolUDP { 97 protocol = 17 98 } 99 100 // check if already has udp matching to add wild card publishing 101 for j := i + 1; j < len(ingressPorts); j++ { 102 if ingressPorts[j].TargetPort == port.TargetPort && 103 ingressPorts[j].PublishedPort == port.PublishedPort { 104 protocol = 0 105 } 106 } 107 108 publishedPorts[port.PublishedPort] = port.TargetPort 109 110 lbPolicylistMap[lb].elb, err = hcsshim.AddLoadBalancer(endpoints, false, sourceVIP, "", protocol, uint16(port.TargetPort), uint16(port.PublishedPort)) 111 if err != nil { 112 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", 113 lb.service.name, vip.String(), uint16(port.TargetPort), uint16(port.PublishedPort), endpoints, sourceVIP, n.Name(), err) 114 return 115 } 116 } 117 } 118 119 func (n *network) rmLBBackend(ip net.IP, lb *loadBalancer, rmService bool, fullRemove bool) { 120 if len(lb.vip) == 0 { 121 return 122 } 123 124 if numEnabledBackends(lb) > 0 { 125 // Reprogram HNS (actually VFP) with the existing backends. 126 n.addLBBackend(ip, lb) 127 } else { 128 lb.Lock() 129 defer lb.Unlock() 130 logrus.Debugf("No more backends for service %s (ip:%s). Removing all policies", lb.service.name, lb.vip.String()) 131 132 if policyLists, ok := lbPolicylistMap[lb]; ok { 133 if policyLists.ilb != nil { 134 if _, err := policyLists.ilb.Delete(); err != nil { 135 logrus.Errorf("Failed to remove HNS ILB policylist %s: %s", policyLists.ilb.ID, err) 136 } 137 policyLists.ilb = nil 138 } 139 140 if policyLists.elb != nil { 141 if _, err := policyLists.elb.Delete(); err != nil { 142 logrus.Errorf("Failed to remove HNS ELB policylist %s: %s", policyLists.elb.ID, err) 143 } 144 policyLists.elb = nil 145 } 146 delete(lbPolicylistMap, lb) 147 148 } else { 149 logrus.Errorf("Failed to find policies for service %s (%s)", lb.service.name, lb.vip.String()) 150 } 151 } 152 } 153 154 func numEnabledBackends(lb *loadBalancer) int { 155 nEnabled := 0 156 for _, be := range lb.backEnds { 157 if !be.disabled { 158 nEnabled++ 159 } 160 } 161 return nEnabled 162 } 163 164 func (sb *sandbox) populateLoadBalancers(ep *endpoint) { 165 } 166 167 func arrangeIngressFilterRule() { 168 }