k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/common/wait_for_pvcs.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 defaultWaitForPVCsTimeout = 60 * time.Second 30 defaultWaitForPVCsInterval = 5 * time.Second 31 waitForBoundPVCsMeasurementName = "WaitForBoundPVCs" 32 ) 33 34 func init() { 35 if err := measurement.Register(waitForBoundPVCsMeasurementName, createWaitForBoundPVCsMeasurement); err != nil { 36 klog.Fatalf("Cannot register %s: %v", waitForBoundPVCsMeasurementName, err) 37 } 38 } 39 40 func createWaitForBoundPVCsMeasurement() measurement.Measurement { 41 return &waitForBoundPVCsMeasurement{} 42 } 43 44 type waitForBoundPVCsMeasurement struct{} 45 46 // Execute waits until desired number of PVCs are bound or until timeout happens. 47 // PVCs can be specified by field and/or label selectors. 48 // If namespace is not passed by parameter, all-namespace scope is assumed. 49 func (w *waitForBoundPVCsMeasurement) Execute(config *measurement.Config) ([]measurement.Summary, error) { 50 desiredPVCCount, err := util.GetInt(config.Params, "desiredPVCCount") 51 if err != nil { 52 return nil, err 53 } 54 selector := util.NewObjectSelector() 55 if err := selector.Parse(config.Params); err != nil { 56 return nil, err 57 } 58 timeout, err := util.GetDurationOrDefault(config.Params, "timeout", defaultWaitForPVCsTimeout) 59 if err != nil { 60 return nil, err 61 } 62 63 stopCh := make(chan struct{}) 64 time.AfterFunc(timeout, func() { 65 close(stopCh) 66 }) 67 options := &measurementutil.WaitForPVCOptions{ 68 Selector: selector, 69 DesiredPVCCount: desiredPVCCount, 70 CallerName: w.String(), 71 WaitForPVCsInterval: defaultWaitForPVCsInterval, 72 } 73 return nil, measurementutil.WaitForPVCs(config.ClusterFramework.GetClientSets().GetClient(), stopCh, options) 74 } 75 76 // Dispose cleans up after the measurement. 77 func (*waitForBoundPVCsMeasurement) Dispose() {} 78 79 // String returns a string representation of the measurement. 80 func (*waitForBoundPVCsMeasurement) String() string { 81 return waitForBoundPVCsMeasurementName 82 }