cidr_allocator.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright 2016 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 ipam
  14. import (
  15. "fmt"
  16. "net"
  17. "time"
  18. "k8s.io/klog"
  19. "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/fields"
  22. "k8s.io/apimachinery/pkg/labels"
  23. "k8s.io/apimachinery/pkg/util/wait"
  24. informers "k8s.io/client-go/informers/core/v1"
  25. clientset "k8s.io/client-go/kubernetes"
  26. cloudprovider "k8s.io/cloud-provider"
  27. )
  28. type nodeAndCIDR struct {
  29. cidr *net.IPNet
  30. nodeName string
  31. }
  32. // CIDRAllocatorType is the type of the allocator to use.
  33. type CIDRAllocatorType string
  34. const (
  35. // RangeAllocatorType is the allocator that uses an internal CIDR
  36. // range allocator to do node CIDR range allocations.
  37. RangeAllocatorType CIDRAllocatorType = "RangeAllocator"
  38. // CloudAllocatorType is the allocator that uses cloud platform
  39. // support to do node CIDR range allocations.
  40. CloudAllocatorType CIDRAllocatorType = "CloudAllocator"
  41. // IPAMFromClusterAllocatorType uses the ipam controller sync'ing the node
  42. // CIDR range allocations from the cluster to the cloud.
  43. IPAMFromClusterAllocatorType = "IPAMFromCluster"
  44. // IPAMFromCloudAllocatorType uses the ipam controller sync'ing the node
  45. // CIDR range allocations from the cloud to the cluster.
  46. IPAMFromCloudAllocatorType = "IPAMFromCloud"
  47. )
  48. // TODO: figure out the good setting for those constants.
  49. const (
  50. // The amount of time the nodecontroller polls on the list nodes endpoint.
  51. apiserverStartupGracePeriod = 10 * time.Minute
  52. // The no. of NodeSpec updates NC can process concurrently.
  53. cidrUpdateWorkers = 30
  54. // The max no. of NodeSpec updates that can be enqueued.
  55. cidrUpdateQueueSize = 5000
  56. // cidrUpdateRetries is the no. of times a NodeSpec update will be retried before dropping it.
  57. cidrUpdateRetries = 3
  58. // updateRetryTimeout is the time to wait before requeing a failed node for retry
  59. updateRetryTimeout = 250 * time.Millisecond
  60. // maxUpdateRetryTimeout is the maximum amount of time between timeouts.
  61. maxUpdateRetryTimeout = 5 * time.Second
  62. // updateMaxRetries is the max retries for a failed node
  63. updateMaxRetries = 10
  64. )
  65. // CIDRAllocator is an interface implemented by things that know how
  66. // to allocate/occupy/recycle CIDR for nodes.
  67. type CIDRAllocator interface {
  68. // AllocateOrOccupyCIDR looks at the given node, assigns it a valid
  69. // CIDR if it doesn't currently have one or mark the CIDR as used if
  70. // the node already have one.
  71. AllocateOrOccupyCIDR(node *v1.Node) error
  72. // ReleaseCIDR releases the CIDR of the removed node
  73. ReleaseCIDR(node *v1.Node) error
  74. // Run starts all the working logic of the allocator.
  75. Run(stopCh <-chan struct{})
  76. }
  77. // New creates a new CIDR range allocator.
  78. func New(kubeClient clientset.Interface, cloud cloudprovider.Interface, nodeInformer informers.NodeInformer, allocatorType CIDRAllocatorType, clusterCIDR, serviceCIDR *net.IPNet, nodeCIDRMaskSize int) (CIDRAllocator, error) {
  79. nodeList, err := listNodes(kubeClient)
  80. if err != nil {
  81. return nil, err
  82. }
  83. switch allocatorType {
  84. case RangeAllocatorType:
  85. return NewCIDRRangeAllocator(kubeClient, nodeInformer, clusterCIDR, serviceCIDR, nodeCIDRMaskSize, nodeList)
  86. case CloudAllocatorType:
  87. return NewCloudCIDRAllocator(kubeClient, cloud, nodeInformer)
  88. default:
  89. return nil, fmt.Errorf("Invalid CIDR allocator type: %v", allocatorType)
  90. }
  91. }
  92. func listNodes(kubeClient clientset.Interface) (*v1.NodeList, error) {
  93. var nodeList *v1.NodeList
  94. // We must poll because apiserver might not be up. This error causes
  95. // controller manager to restart.
  96. if pollErr := wait.Poll(10*time.Second, apiserverStartupGracePeriod, func() (bool, error) {
  97. var err error
  98. nodeList, err = kubeClient.CoreV1().Nodes().List(metav1.ListOptions{
  99. FieldSelector: fields.Everything().String(),
  100. LabelSelector: labels.Everything().String(),
  101. })
  102. if err != nil {
  103. klog.Errorf("Failed to list all nodes: %v", err)
  104. return false, nil
  105. }
  106. return true, nil
  107. }); pollErr != nil {
  108. return nil, fmt.Errorf("Failed to list all nodes in %v, cannot proceed without updating CIDR map",
  109. apiserverStartupGracePeriod)
  110. }
  111. return nodeList, nil
  112. }