cidr_allocator.go 4.9 KB

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