node_ipam_controller.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Copyright 2014 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 nodeipam
  14. import (
  15. "net"
  16. "time"
  17. "k8s.io/klog"
  18. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  19. v1core "k8s.io/client-go/kubernetes/typed/core/v1"
  20. "k8s.io/client-go/tools/cache"
  21. "k8s.io/client-go/tools/record"
  22. "k8s.io/api/core/v1"
  23. coreinformers "k8s.io/client-go/informers/core/v1"
  24. clientset "k8s.io/client-go/kubernetes"
  25. corelisters "k8s.io/client-go/listers/core/v1"
  26. cloudprovider "k8s.io/cloud-provider"
  27. "k8s.io/kubernetes/pkg/controller"
  28. "k8s.io/kubernetes/pkg/controller/nodeipam/ipam"
  29. nodesync "k8s.io/kubernetes/pkg/controller/nodeipam/ipam/sync"
  30. "k8s.io/kubernetes/pkg/util/metrics"
  31. )
  32. const (
  33. // ipamResyncInterval is the amount of time between when the cloud and node
  34. // CIDR range assignments are synchronized.
  35. ipamResyncInterval = 30 * time.Second
  36. // ipamMaxBackoff is the maximum backoff for retrying synchronization of a
  37. // given in the error state.
  38. ipamMaxBackoff = 10 * time.Second
  39. // ipamInitialRetry is the initial retry interval for retrying synchronization of a
  40. // given in the error state.
  41. ipamInitialBackoff = 250 * time.Millisecond
  42. )
  43. // Controller is the controller that manages node ipam state.
  44. type Controller struct {
  45. allocatorType ipam.CIDRAllocatorType
  46. cloud cloudprovider.Interface
  47. clusterCIDR *net.IPNet
  48. serviceCIDR *net.IPNet
  49. kubeClient clientset.Interface
  50. // Method for easy mocking in unittest.
  51. lookupIP func(host string) ([]net.IP, error)
  52. nodeLister corelisters.NodeLister
  53. nodeInformerSynced cache.InformerSynced
  54. cidrAllocator ipam.CIDRAllocator
  55. forcefullyDeletePod func(*v1.Pod) error
  56. }
  57. // NewNodeIpamController returns a new node IP Address Management controller to
  58. // sync instances from cloudprovider.
  59. // This method returns an error if it is unable to initialize the CIDR bitmap with
  60. // podCIDRs it has already allocated to nodes. Since we don't allow podCIDR changes
  61. // currently, this should be handled as a fatal error.
  62. func NewNodeIpamController(
  63. nodeInformer coreinformers.NodeInformer,
  64. cloud cloudprovider.Interface,
  65. kubeClient clientset.Interface,
  66. clusterCIDR *net.IPNet,
  67. serviceCIDR *net.IPNet,
  68. nodeCIDRMaskSize int,
  69. allocatorType ipam.CIDRAllocatorType) (*Controller, error) {
  70. if kubeClient == nil {
  71. klog.Fatalf("kubeClient is nil when starting Controller")
  72. }
  73. eventBroadcaster := record.NewBroadcaster()
  74. eventBroadcaster.StartLogging(klog.Infof)
  75. klog.Infof("Sending events to api server.")
  76. eventBroadcaster.StartRecordingToSink(
  77. &v1core.EventSinkImpl{
  78. Interface: kubeClient.CoreV1().Events(""),
  79. })
  80. if kubeClient.CoreV1().RESTClient().GetRateLimiter() != nil {
  81. metrics.RegisterMetricAndTrackRateLimiterUsage("node_ipam_controller", kubeClient.CoreV1().RESTClient().GetRateLimiter())
  82. }
  83. if allocatorType != ipam.CloudAllocatorType {
  84. // Cloud CIDR allocator does not rely on clusterCIDR or nodeCIDRMaskSize for allocation.
  85. if clusterCIDR == nil {
  86. klog.Fatal("Controller: Must specify --cluster-cidr if --allocate-node-cidrs is set")
  87. }
  88. if maskSize, _ := clusterCIDR.Mask.Size(); maskSize > nodeCIDRMaskSize {
  89. klog.Fatal("Controller: Invalid --cluster-cidr, mask size of cluster CIDR must be less than --node-cidr-mask-size")
  90. }
  91. }
  92. ic := &Controller{
  93. cloud: cloud,
  94. kubeClient: kubeClient,
  95. lookupIP: net.LookupIP,
  96. clusterCIDR: clusterCIDR,
  97. serviceCIDR: serviceCIDR,
  98. allocatorType: allocatorType,
  99. }
  100. // TODO: Abstract this check into a generic controller manager should run method.
  101. if ic.allocatorType == ipam.IPAMFromClusterAllocatorType || ic.allocatorType == ipam.IPAMFromCloudAllocatorType {
  102. cfg := &ipam.Config{
  103. Resync: ipamResyncInterval,
  104. MaxBackoff: ipamMaxBackoff,
  105. InitialRetry: ipamInitialBackoff,
  106. }
  107. switch ic.allocatorType {
  108. case ipam.IPAMFromClusterAllocatorType:
  109. cfg.Mode = nodesync.SyncFromCluster
  110. case ipam.IPAMFromCloudAllocatorType:
  111. cfg.Mode = nodesync.SyncFromCloud
  112. }
  113. ipamc, err := ipam.NewController(cfg, kubeClient, cloud, clusterCIDR, serviceCIDR, nodeCIDRMaskSize)
  114. if err != nil {
  115. klog.Fatalf("Error creating ipam controller: %v", err)
  116. }
  117. if err := ipamc.Start(nodeInformer); err != nil {
  118. klog.Fatalf("Error trying to Init(): %v", err)
  119. }
  120. } else {
  121. var err error
  122. ic.cidrAllocator, err = ipam.New(
  123. kubeClient, cloud, nodeInformer, ic.allocatorType, ic.clusterCIDR, ic.serviceCIDR, nodeCIDRMaskSize)
  124. if err != nil {
  125. return nil, err
  126. }
  127. }
  128. ic.nodeLister = nodeInformer.Lister()
  129. ic.nodeInformerSynced = nodeInformer.Informer().HasSynced
  130. return ic, nil
  131. }
  132. // Run starts an asynchronous loop that monitors the status of cluster nodes.
  133. func (nc *Controller) Run(stopCh <-chan struct{}) {
  134. defer utilruntime.HandleCrash()
  135. klog.Infof("Starting ipam controller")
  136. defer klog.Infof("Shutting down ipam controller")
  137. if !controller.WaitForCacheSync("node", stopCh, nc.nodeInformerSynced) {
  138. return
  139. }
  140. if nc.allocatorType != ipam.IPAMFromClusterAllocatorType && nc.allocatorType != ipam.IPAMFromCloudAllocatorType {
  141. go nc.cidrAllocator.Run(stopCh)
  142. }
  143. <-stopCh
  144. }