route_network_windows.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2018 flannel authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package backend
  15. import (
  16. "strings"
  17. "sync"
  18. "time"
  19. "github.com/flannel-io/flannel/pkg/routing"
  20. "github.com/flannel-io/flannel/subnet"
  21. "golang.org/x/net/context"
  22. log "k8s.io/klog"
  23. )
  24. const (
  25. routeCheckRetries = 10
  26. )
  27. type RouteNetwork struct {
  28. SimpleNetwork
  29. Name string
  30. BackendType string
  31. SM subnet.Manager
  32. GetRoute func(lease *subnet.Lease) *routing.Route
  33. Mtu int
  34. LinkIndex int
  35. routes []routing.Route
  36. }
  37. func (n *RouteNetwork) MTU() int {
  38. return n.Mtu
  39. }
  40. func (n *RouteNetwork) Run(ctx context.Context) {
  41. wg := sync.WaitGroup{}
  42. log.Info("Watching for new subnet leases")
  43. evts := make(chan []subnet.Event)
  44. wg.Add(1)
  45. go func() {
  46. subnet.WatchLeases(ctx, n.SM, n.SubnetLease, evts)
  47. wg.Done()
  48. }()
  49. n.routes = make([]routing.Route, 0, 10)
  50. wg.Add(1)
  51. go func() {
  52. n.routeCheck(ctx)
  53. wg.Done()
  54. }()
  55. defer wg.Wait()
  56. for {
  57. select {
  58. case evtBatch, ok := <-evts:
  59. if !ok {
  60. log.Infof("evts chan closed")
  61. return
  62. }
  63. n.handleSubnetEvents(evtBatch)
  64. }
  65. }
  66. }
  67. func (n *RouteNetwork) handleSubnetEvents(batch []subnet.Event) {
  68. router := routing.RouterWindows{}
  69. for _, evt := range batch {
  70. leaseSubnet := evt.Lease.Subnet
  71. leaseAttrs := evt.Lease.Attrs
  72. if !strings.EqualFold(leaseAttrs.BackendType, n.BackendType) {
  73. log.Warningf("Ignoring non-%v subnet(%v): type=%v", n.BackendType, leaseSubnet, leaseAttrs.BackendType)
  74. continue
  75. }
  76. expectedRoute := n.GetRoute(&evt.Lease)
  77. switch evt.Type {
  78. case subnet.EventAdded:
  79. log.Infof("Subnet added: %v via %v", leaseSubnet, leaseAttrs.PublicIP)
  80. existingRoutes, _ := router.GetRoutesFromInterfaceToSubnet(expectedRoute.InterfaceIndex, expectedRoute.DestinationSubnet)
  81. if len(existingRoutes) > 0 {
  82. existingRoute := existingRoutes[0]
  83. if existingRoute.Equal(*expectedRoute) {
  84. continue
  85. }
  86. log.Warningf("Replacing existing route %v via %v with %v via %v", leaseSubnet, existingRoute.GatewayAddress, leaseSubnet, leaseAttrs.PublicIP)
  87. err := router.DeleteRoute(existingRoute.InterfaceIndex, existingRoute.DestinationSubnet, existingRoute.GatewayAddress)
  88. if err != nil {
  89. log.Errorf("Error removing route: %v", err)
  90. continue
  91. }
  92. }
  93. err := router.CreateRoute(expectedRoute.InterfaceIndex, expectedRoute.DestinationSubnet, expectedRoute.GatewayAddress)
  94. if err != nil {
  95. log.Errorf("Error creating route: %v", err)
  96. continue
  97. }
  98. n.addToRouteList(expectedRoute)
  99. case subnet.EventRemoved:
  100. log.Infof("Subnet removed: %v", leaseSubnet)
  101. existingRoutes, _ := router.GetRoutesFromInterfaceToSubnet(expectedRoute.InterfaceIndex, expectedRoute.DestinationSubnet)
  102. if len(existingRoutes) > 0 {
  103. existingRoute := existingRoutes[0]
  104. if existingRoute.Equal(*expectedRoute) {
  105. log.Infof("Removing existing route %v via %v", leaseSubnet, existingRoute.GatewayAddress)
  106. err := router.DeleteRoute(existingRoute.InterfaceIndex, existingRoute.DestinationSubnet, existingRoute.GatewayAddress)
  107. if err != nil {
  108. log.Warningf("Error removing route: %v", err)
  109. }
  110. }
  111. }
  112. n.removeFromRouteList(expectedRoute)
  113. default:
  114. log.Error("Internal error: unknown event type: ", int(evt.Type))
  115. }
  116. }
  117. }
  118. func (n *RouteNetwork) addToRouteList(newRoute *routing.Route) {
  119. for _, route := range n.routes {
  120. if route.Equal(*newRoute) {
  121. return
  122. }
  123. }
  124. n.routes = append(n.routes, *newRoute)
  125. }
  126. func (n *RouteNetwork) removeFromRouteList(oldRoute *routing.Route) {
  127. for index, route := range n.routes {
  128. if route.Equal(*oldRoute) {
  129. n.routes = append(n.routes[:index], n.routes[index+1:]...)
  130. return
  131. }
  132. }
  133. }
  134. func (n *RouteNetwork) routeCheck(ctx context.Context) {
  135. for {
  136. select {
  137. case <-ctx.Done():
  138. return
  139. case <-time.After(routeCheckRetries * time.Second):
  140. n.checkSubnetExistInRoutes()
  141. }
  142. }
  143. }
  144. func (n *RouteNetwork) checkSubnetExistInRoutes() {
  145. router := routing.RouterWindows{}
  146. existingRoutes, err := router.GetAllRoutes()
  147. if err != nil {
  148. log.Errorf("Error enumerating routes: %v", err)
  149. return
  150. }
  151. for _, expectedRoute := range n.routes {
  152. exist := false
  153. for _, existingRoute := range existingRoutes {
  154. if expectedRoute.Equal(existingRoute) {
  155. exist = true
  156. break
  157. }
  158. }
  159. if !exist {
  160. err := router.CreateRoute(expectedRoute.InterfaceIndex, expectedRoute.DestinationSubnet, expectedRoute.GatewayAddress)
  161. if err != nil {
  162. log.Warningf("Error recovering route to %v via %v on %v (%v).", expectedRoute.DestinationSubnet, expectedRoute.GatewayAddress, expectedRoute.InterfaceIndex, err)
  163. continue
  164. }
  165. log.Infof("Recovered route to %v via %v on %v.", expectedRoute.DestinationSubnet, expectedRoute.GatewayAddress, expectedRoute.InterfaceIndex)
  166. }
  167. }
  168. }