k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/common/wait_for_pods.go (about)

     1  /*
     2  Copyright 2018 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  	"context"
    21  	"time"
    22  
    23  	"k8s.io/klog/v2"
    24  	"k8s.io/perf-tests/clusterloader2/pkg/measurement"
    25  	measurementutil "k8s.io/perf-tests/clusterloader2/pkg/measurement/util"
    26  	"k8s.io/perf-tests/clusterloader2/pkg/util"
    27  )
    28  
    29  const (
    30  	defaultWaitForPodsTimeout         = 60 * time.Second
    31  	defaultWaitForPodsInterval        = 5 * time.Second
    32  	waitForRunningPodsMeasurementName = "WaitForRunningPods"
    33  )
    34  
    35  func init() {
    36  	if err := measurement.Register(waitForRunningPodsMeasurementName, createWaitForRunningPodsMeasurement); err != nil {
    37  		klog.Fatalf("Cannot register %s: %v", waitForRunningPodsMeasurementName, err)
    38  	}
    39  }
    40  
    41  func createWaitForRunningPodsMeasurement() measurement.Measurement {
    42  	return &waitForRunningPodsMeasurement{}
    43  }
    44  
    45  type waitForRunningPodsMeasurement struct{}
    46  
    47  // Execute waits until desired number of pods are running or until timeout happens.
    48  // Pods can be specified by field and/or label selectors.
    49  // If namespace is not passed by parameter, all-namespace scope is assumed.
    50  func (w *waitForRunningPodsMeasurement) Execute(config *measurement.Config) ([]measurement.Summary, error) {
    51  	desiredPodCount, err := util.GetInt(config.Params, "desiredPodCount")
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	selector := util.NewObjectSelector()
    56  	if err := selector.Parse(config.Params); err != nil {
    57  		return nil, err
    58  	}
    59  	timeout, err := util.GetDurationOrDefault(config.Params, "timeout", defaultWaitForPodsTimeout)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	ctx, cancel := context.WithTimeout(context.TODO(), timeout)
    65  	defer cancel()
    66  	options := &measurementutil.WaitForPodOptions{
    67  		DesiredPodCount:     func() int { return desiredPodCount },
    68  		CallerName:          w.String(),
    69  		WaitForPodsInterval: defaultWaitForPodsInterval,
    70  	}
    71  	podStore, err := measurementutil.NewPodStore(config.ClusterFramework.GetClientSets().GetClient(), selector)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	_, err = measurementutil.WaitForPods(ctx, podStore, options)
    76  	return nil, err
    77  }
    78  
    79  // Dispose cleans up after the measurement.
    80  func (*waitForRunningPodsMeasurement) Dispose() {}
    81  
    82  // String returns a string representation of the measurement.
    83  func (*waitForRunningPodsMeasurement) String() string {
    84  	return waitForRunningPodsMeasurementName
    85  }