service_windows.go 4.0 KB

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