k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/common/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 common
    18  
    19  import (
    20  	"time"
    21  
    22  	"k8s.io/klog/v2"
    23  	"k8s.io/perf-tests/clusterloader2/pkg/measurement"
    24  	measurementutil "k8s.io/perf-tests/clusterloader2/pkg/measurement/util"
    25  	"k8s.io/perf-tests/clusterloader2/pkg/util"
    26  )
    27  
    28  const (
    29  	defaultWaitForNodesTimeout  = 30 * time.Minute
    30  	defaultWaitForNodesInterval = 30 * time.Second
    31  	waitForNodesMeasurementName = "WaitForNodes"
    32  )
    33  
    34  func init() {
    35  	if err := measurement.Register(waitForNodesMeasurementName, createWaitForNodesMeasurement); err != nil {
    36  		klog.Fatalf("Cannot register %s: %v", waitForNodesMeasurementName, err)
    37  	}
    38  }
    39  
    40  func createWaitForNodesMeasurement() measurement.Measurement {
    41  	return &waitForNodesMeasurement{}
    42  }
    43  
    44  type waitForNodesMeasurement struct{}
    45  
    46  // Execute waits until desired number of Nodes are ready or until a
    47  // timeout happens. Nodes can be optionally specified by field and/or label
    48  // selectors.
    49  func (w *waitForNodesMeasurement) Execute(config *measurement.Config) ([]measurement.Summary, error) {
    50  	minNodeCount, maxNodeCount, err := getMinMaxDesiredNodeCount(config.Params)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	selector := util.NewObjectSelector()
    56  	if err := selector.Parse(config.Params); err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	timeout, err := util.GetDurationOrDefault(config.Params, "timeout", defaultWaitForNodesTimeout)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	stopCh := make(chan struct{})
    65  	time.AfterFunc(timeout, func() {
    66  		close(stopCh)
    67  	})
    68  
    69  	options := &measurementutil.WaitForNodeOptions{
    70  		Selector:             selector,
    71  		MinDesiredNodeCount:  minNodeCount,
    72  		MaxDesiredNodeCount:  maxNodeCount,
    73  		CallerName:           w.String(),
    74  		WaitForNodesInterval: defaultWaitForNodesInterval,
    75  	}
    76  	return nil, measurementutil.WaitForNodes(config.ClusterFramework.GetClientSets().GetClient(), stopCh, options)
    77  }
    78  
    79  // Dispose cleans up after the measurement.
    80  func (*waitForNodesMeasurement) Dispose() {}
    81  
    82  // String returns a string representation of the measurement.
    83  func (*waitForNodesMeasurement) String() string {
    84  	return waitForNodesMeasurementName
    85  }
    86  
    87  func getMinMaxDesiredNodeCount(params map[string]interface{}) (minDesiredNodeCount, maxDesiredNodeCount int, err error) {
    88  	minDesiredNodeCount, err = util.GetInt(params, "minDesiredNodeCount")
    89  	if err != nil {
    90  		return
    91  	}
    92  	maxDesiredNodeCount, err = util.GetInt(params, "maxDesiredNodeCount")
    93  	return
    94  }