k8s.io/kubernetes@v1.29.3/pkg/controller/nodeipam/ipam/cidr_allocator.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package ipam 18 19 import ( 20 "context" 21 "fmt" 22 "net" 23 "time" 24 25 v1 "k8s.io/api/core/v1" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/fields" 28 "k8s.io/apimachinery/pkg/labels" 29 "k8s.io/apimachinery/pkg/util/wait" 30 informers "k8s.io/client-go/informers/core/v1" 31 clientset "k8s.io/client-go/kubernetes" 32 cloudprovider "k8s.io/cloud-provider" 33 "k8s.io/klog/v2" 34 ) 35 36 // CIDRAllocatorType is the type of the allocator to use. 37 type CIDRAllocatorType string 38 39 const ( 40 // RangeAllocatorType is the allocator that uses an internal CIDR 41 // range allocator to do node CIDR range allocations. 42 RangeAllocatorType CIDRAllocatorType = "RangeAllocator" 43 // CloudAllocatorType is the allocator that uses cloud platform 44 // support to do node CIDR range allocations. 45 CloudAllocatorType CIDRAllocatorType = "CloudAllocator" 46 // IPAMFromClusterAllocatorType uses the ipam controller sync'ing the node 47 // CIDR range allocations from the cluster to the cloud. 48 IPAMFromClusterAllocatorType = "IPAMFromCluster" 49 // IPAMFromCloudAllocatorType uses the ipam controller sync'ing the node 50 // CIDR range allocations from the cloud to the cluster. 51 IPAMFromCloudAllocatorType = "IPAMFromCloud" 52 ) 53 54 // TODO: figure out the good setting for those constants. 55 const ( 56 // The amount of time the nodecontroller polls on the list nodes endpoint. 57 apiserverStartupGracePeriod = 10 * time.Minute 58 59 // The no. of NodeSpec updates NC can process concurrently. 60 cidrUpdateWorkers = 30 61 62 // The max no. of NodeSpec updates that can be enqueued. 63 cidrUpdateQueueSize = 5000 64 65 // cidrUpdateRetries is the no. of times a NodeSpec update will be retried before dropping it. 66 cidrUpdateRetries = 3 67 68 // updateRetryTimeout is the time to wait before requeing a failed node for retry 69 updateRetryTimeout = 250 * time.Millisecond 70 71 // maxUpdateRetryTimeout is the maximum amount of time between timeouts. 72 maxUpdateRetryTimeout = 5 * time.Second 73 74 // updateMaxRetries is the max retries for a failed node 75 updateMaxRetries = 10 76 ) 77 78 // nodePollInterval is used in listing node 79 // This is a variable instead of a const to enable testing. 80 var nodePollInterval = 10 * time.Second 81 82 // CIDRAllocator is an interface implemented by things that know how 83 // to allocate/occupy/recycle CIDR for nodes. 84 type CIDRAllocator interface { 85 // AllocateOrOccupyCIDR looks at the given node, assigns it a valid 86 // CIDR if it doesn't currently have one or mark the CIDR as used if 87 // the node already have one. 88 AllocateOrOccupyCIDR(logger klog.Logger, node *v1.Node) error 89 // ReleaseCIDR releases the CIDR of the removed node. 90 ReleaseCIDR(logger klog.Logger, node *v1.Node) error 91 // Run starts all the working logic of the allocator. 92 Run(ctx context.Context) 93 } 94 95 // CIDRAllocatorParams is parameters that's required for creating new 96 // cidr range allocator. 97 type CIDRAllocatorParams struct { 98 // ClusterCIDRs is list of cluster cidrs. 99 ClusterCIDRs []*net.IPNet 100 // ServiceCIDR is primary service cidr for cluster. 101 ServiceCIDR *net.IPNet 102 // SecondaryServiceCIDR is secondary service cidr for cluster. 103 SecondaryServiceCIDR *net.IPNet 104 // NodeCIDRMaskSizes is list of node cidr mask sizes. 105 NodeCIDRMaskSizes []int 106 } 107 108 // CIDRs are reserved, then node resource is patched with them. 109 // nodeReservedCIDRs holds the reservation info for a node. 110 type nodeReservedCIDRs struct { 111 allocatedCIDRs []*net.IPNet 112 nodeName string 113 } 114 115 // New creates a new CIDR range allocator. 116 func New(ctx context.Context, kubeClient clientset.Interface, cloud cloudprovider.Interface, nodeInformer informers.NodeInformer, allocatorType CIDRAllocatorType, allocatorParams CIDRAllocatorParams) (CIDRAllocator, error) { 117 logger := klog.FromContext(ctx) 118 nodeList, err := listNodes(logger, kubeClient) 119 if err != nil { 120 return nil, err 121 } 122 123 switch allocatorType { 124 case RangeAllocatorType: 125 return NewCIDRRangeAllocator(logger, kubeClient, nodeInformer, allocatorParams, nodeList) 126 case CloudAllocatorType: 127 return NewCloudCIDRAllocator(logger, kubeClient, cloud, nodeInformer) 128 default: 129 return nil, fmt.Errorf("invalid CIDR allocator type: %v", allocatorType) 130 } 131 } 132 133 func listNodes(logger klog.Logger, kubeClient clientset.Interface) (*v1.NodeList, error) { 134 var nodeList *v1.NodeList 135 // We must poll because apiserver might not be up. This error causes 136 // controller manager to restart. 137 if pollErr := wait.Poll(nodePollInterval, apiserverStartupGracePeriod, func() (bool, error) { 138 var err error 139 nodeList, err = kubeClient.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{ 140 FieldSelector: fields.Everything().String(), 141 LabelSelector: labels.Everything().String(), 142 }) 143 if err != nil { 144 logger.Error(err, "Failed to list all nodes") 145 return false, nil 146 } 147 return true, nil 148 }); pollErr != nil { 149 return nil, fmt.Errorf("failed to list all nodes in %v, cannot proceed without updating CIDR map", 150 apiserverStartupGracePeriod) 151 } 152 return nodeList, nil 153 } 154 155 // ipnetToStringList converts a slice of net.IPNet into a list of CIDR in string format 156 func ipnetToStringList(inCIDRs []*net.IPNet) []string { 157 outCIDRs := make([]string, len(inCIDRs)) 158 for idx, inCIDR := range inCIDRs { 159 outCIDRs[idx] = inCIDR.String() 160 } 161 return outCIDRs 162 }