k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/util/wait_for_nodes.go (about) 1 /* 2 Copyright 2019 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 util 18 19 import ( 20 "fmt" 21 "time" 22 23 v1 "k8s.io/api/core/v1" 24 clientset "k8s.io/client-go/kubernetes" 25 "k8s.io/klog/v2" 26 "k8s.io/perf-tests/clusterloader2/pkg/util" 27 ) 28 29 // WaitForNodeOptions is an options object used by WaitForNodes methods. 30 type WaitForNodeOptions struct { 31 Selector *util.ObjectSelector 32 MinDesiredNodeCount int 33 MaxDesiredNodeCount int 34 CallerName string 35 WaitForNodesInterval time.Duration 36 } 37 38 // WaitForNodes waits till the desired number of nodes is ready. 39 // If stopCh is closed before all nodes are ready, the error will be returned. 40 func WaitForNodes(clientSet clientset.Interface, stopCh <-chan struct{}, options *WaitForNodeOptions) error { 41 ps, err := NewNodeStore(clientSet, options.Selector) 42 if err != nil { 43 return fmt.Errorf("node store creation error: %v", err) 44 } 45 defer ps.Stop() 46 47 nodeCount := getNumReadyNodes(ps.List()) 48 for { 49 select { 50 case <-stopCh: 51 return fmt.Errorf("timeout while waiting for [%d-%d] Nodes with selector '%v' to be ready - currently there is %d Nodes", 52 options.MinDesiredNodeCount, options.MaxDesiredNodeCount, options.Selector.String(), nodeCount) 53 case <-time.After(options.WaitForNodesInterval): 54 nodeCount = getNumReadyNodes(ps.List()) 55 klog.V(2).Infof("%s: node count (selector = %v): %d", options.CallerName, options.Selector.String(), nodeCount) 56 if options.MinDesiredNodeCount <= nodeCount && nodeCount <= options.MaxDesiredNodeCount { 57 return nil 58 } 59 } 60 } 61 } 62 63 func getNumReadyNodes(nodes []*v1.Node) int { 64 nReady := 0 65 for _, n := range nodes { 66 if util.IsNodeSchedulableAndUntainted(n) { 67 nReady++ 68 } 69 } 70 return nReady 71 }