service_windows.go 4.1 KB

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