routecontroller.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package route
  14. import (
  15. "fmt"
  16. "net"
  17. "sync"
  18. "time"
  19. "github.com/golang/glog"
  20. "k8s.io/kubernetes/pkg/api"
  21. "k8s.io/kubernetes/pkg/api/errors"
  22. "k8s.io/kubernetes/pkg/api/unversioned"
  23. clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
  24. "k8s.io/kubernetes/pkg/cloudprovider"
  25. "k8s.io/kubernetes/pkg/util/metrics"
  26. nodeutil "k8s.io/kubernetes/pkg/util/node"
  27. "k8s.io/kubernetes/pkg/util/wait"
  28. )
  29. const (
  30. // Maximal number of concurrent CreateRoute API calls.
  31. // TODO: This should be per-provider.
  32. maxConcurrentRouteCreations int = 200
  33. // Maximum number of retries of route creations.
  34. maxRetries int = 5
  35. // Maximum number of retries of node status update.
  36. updateNodeStatusMaxRetries int = 3
  37. )
  38. type RouteController struct {
  39. routes cloudprovider.Routes
  40. kubeClient clientset.Interface
  41. clusterName string
  42. clusterCIDR *net.IPNet
  43. }
  44. func New(routes cloudprovider.Routes, kubeClient clientset.Interface, clusterName string, clusterCIDR *net.IPNet) *RouteController {
  45. if kubeClient != nil && kubeClient.Core().GetRESTClient().GetRateLimiter() != nil {
  46. metrics.RegisterMetricAndTrackRateLimiterUsage("route_controller", kubeClient.Core().GetRESTClient().GetRateLimiter())
  47. }
  48. return &RouteController{
  49. routes: routes,
  50. kubeClient: kubeClient,
  51. clusterName: clusterName,
  52. clusterCIDR: clusterCIDR,
  53. }
  54. }
  55. func (rc *RouteController) Run(syncPeriod time.Duration) {
  56. // TODO: If we do just the full Resync every 5 minutes (default value)
  57. // that means that we may wait up to 5 minutes before even starting
  58. // creating a route for it. This is bad.
  59. // We should have a watch on node and if we observe a new node (with CIDR?)
  60. // trigger reconciliation for that node.
  61. go wait.NonSlidingUntil(func() {
  62. if err := rc.reconcileNodeRoutes(); err != nil {
  63. glog.Errorf("Couldn't reconcile node routes: %v", err)
  64. }
  65. }, syncPeriod, wait.NeverStop)
  66. }
  67. func (rc *RouteController) reconcileNodeRoutes() error {
  68. routeList, err := rc.routes.ListRoutes(rc.clusterName)
  69. if err != nil {
  70. return fmt.Errorf("error listing routes: %v", err)
  71. }
  72. // TODO (cjcullen): use pkg/controller/framework.NewInformer to watch this
  73. // and reduce the number of lists needed.
  74. nodeList, err := rc.kubeClient.Core().Nodes().List(api.ListOptions{})
  75. if err != nil {
  76. return fmt.Errorf("error listing nodes: %v", err)
  77. }
  78. return rc.reconcile(nodeList.Items, routeList)
  79. }
  80. func (rc *RouteController) reconcile(nodes []api.Node, routes []*cloudprovider.Route) error {
  81. // nodeCIDRs maps nodeName->nodeCIDR
  82. nodeCIDRs := make(map[string]string)
  83. // routeMap maps routeTargetInstance->route
  84. routeMap := make(map[string]*cloudprovider.Route)
  85. for _, route := range routes {
  86. routeMap[route.TargetInstance] = route
  87. }
  88. wg := sync.WaitGroup{}
  89. rateLimiter := make(chan struct{}, maxConcurrentRouteCreations)
  90. for _, node := range nodes {
  91. // Skip if the node hasn't been assigned a CIDR yet.
  92. if node.Spec.PodCIDR == "" {
  93. continue
  94. }
  95. // Check if we have a route for this node w/ the correct CIDR.
  96. r := routeMap[node.Name]
  97. if r == nil || r.DestinationCIDR != node.Spec.PodCIDR {
  98. // If not, create the route.
  99. route := &cloudprovider.Route{
  100. TargetInstance: node.Name,
  101. DestinationCIDR: node.Spec.PodCIDR,
  102. }
  103. nameHint := string(node.UID)
  104. wg.Add(1)
  105. go func(nodeName string, nameHint string, route *cloudprovider.Route) {
  106. defer wg.Done()
  107. for i := 0; i < maxRetries; i++ {
  108. startTime := time.Now()
  109. // Ensure that we don't have more than maxConcurrentRouteCreations
  110. // CreateRoute calls in flight.
  111. rateLimiter <- struct{}{}
  112. glog.Infof("Creating route for node %s %s with hint %s, throttled %v", nodeName, route.DestinationCIDR, nameHint, time.Now().Sub(startTime))
  113. err := rc.routes.CreateRoute(rc.clusterName, nameHint, route)
  114. <-rateLimiter
  115. rc.updateNetworkingCondition(nodeName, err == nil)
  116. if err != nil {
  117. glog.Errorf("Could not create route %s %s for node %s after %v: %v", nameHint, route.DestinationCIDR, nodeName, time.Now().Sub(startTime), err)
  118. } else {
  119. glog.Infof("Created route for node %s %s with hint %s after %v", nodeName, route.DestinationCIDR, nameHint, time.Now().Sub(startTime))
  120. return
  121. }
  122. }
  123. }(node.Name, nameHint, route)
  124. } else {
  125. rc.updateNetworkingCondition(node.Name, true)
  126. }
  127. nodeCIDRs[node.Name] = node.Spec.PodCIDR
  128. }
  129. for _, route := range routes {
  130. if rc.isResponsibleForRoute(route) {
  131. // Check if this route applies to a node we know about & has correct CIDR.
  132. if nodeCIDRs[route.TargetInstance] != route.DestinationCIDR {
  133. wg.Add(1)
  134. // Delete the route.
  135. go func(route *cloudprovider.Route, startTime time.Time) {
  136. glog.Infof("Deleting route %s %s", route.Name, route.DestinationCIDR)
  137. if err := rc.routes.DeleteRoute(rc.clusterName, route); err != nil {
  138. glog.Errorf("Could not delete route %s %s after %v: %v", route.Name, route.DestinationCIDR, time.Now().Sub(startTime), err)
  139. } else {
  140. glog.Infof("Deleted route %s %s after %v", route.Name, route.DestinationCIDR, time.Now().Sub(startTime))
  141. }
  142. wg.Done()
  143. }(route, time.Now())
  144. }
  145. }
  146. }
  147. wg.Wait()
  148. return nil
  149. }
  150. func (rc *RouteController) updateNetworkingCondition(nodeName string, routeCreated bool) error {
  151. var err error
  152. for i := 0; i < updateNodeStatusMaxRetries; i++ {
  153. // Patch could also fail, even though the chance is very slim. So we still do
  154. // patch in the retry loop.
  155. currentTime := unversioned.Now()
  156. if routeCreated {
  157. err = nodeutil.SetNodeCondition(rc.kubeClient, nodeName, api.NodeCondition{
  158. Type: api.NodeNetworkUnavailable,
  159. Status: api.ConditionFalse,
  160. Reason: "RouteCreated",
  161. Message: "RouteController created a route",
  162. LastTransitionTime: currentTime,
  163. })
  164. } else {
  165. err = nodeutil.SetNodeCondition(rc.kubeClient, nodeName, api.NodeCondition{
  166. Type: api.NodeNetworkUnavailable,
  167. Status: api.ConditionTrue,
  168. Reason: "NoRouteCreated",
  169. Message: "RouteController failed to create a route",
  170. LastTransitionTime: currentTime,
  171. })
  172. }
  173. if err == nil {
  174. return nil
  175. }
  176. if i == updateNodeStatusMaxRetries || !errors.IsConflict(err) {
  177. glog.Errorf("Error updating node %s: %v", nodeName, err)
  178. return err
  179. }
  180. glog.Errorf("Error updating node %s, retrying: %v", nodeName, err)
  181. }
  182. return err
  183. }
  184. func (rc *RouteController) isResponsibleForRoute(route *cloudprovider.Route) bool {
  185. _, cidr, err := net.ParseCIDR(route.DestinationCIDR)
  186. if err != nil {
  187. glog.Errorf("Ignoring route %s, unparsable CIDR: %v", route.Name, err)
  188. return false
  189. }
  190. // Not responsible if this route's CIDR is not within our clusterCIDR
  191. lastIP := make([]byte, len(cidr.IP))
  192. for i := range lastIP {
  193. lastIP[i] = cidr.IP[i] | ^cidr.Mask[i]
  194. }
  195. if !rc.clusterCIDR.Contains(cidr.IP) || !rc.clusterCIDR.Contains(lastIP) {
  196. return false
  197. }
  198. return true
  199. }