k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/common/wait_for_pvs.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 defaultWaitForPVsTimeout = 60 * time.Second 30 defaultWaitForPVsInterval = 5 * time.Second 31 waitForAvailablePVsMeasurementName = "WaitForAvailablePVs" 32 ) 33 34 func init() { 35 if err := measurement.Register(waitForAvailablePVsMeasurementName, createWaitForAvailablePVsMeasurement); err != nil { 36 klog.Fatalf("Cannot register %s: %v", waitForAvailablePVsMeasurementName, err) 37 } 38 } 39 40 func createWaitForAvailablePVsMeasurement() measurement.Measurement { 41 return &waitForAvailablePVsMeasurement{} 42 } 43 44 type waitForAvailablePVsMeasurement struct{} 45 46 // Execute waits until desired number of PVs are Available or until timeout happens. 47 // PVs can be specified by field and/or label selectors. 48 func (w *waitForAvailablePVsMeasurement) Execute(config *measurement.Config) ([]measurement.Summary, error) { 49 desiredPVCount, err := util.GetInt(config.Params, "desiredPVCount") 50 if err != nil { 51 return nil, err 52 } 53 provisioner, err := util.GetString(config.Params, "provisioner") 54 if err != nil { 55 return nil, err 56 } 57 selector := util.NewObjectSelector() 58 if err := selector.Parse(config.Params); err != nil { 59 return nil, err 60 } 61 timeout, err := util.GetDurationOrDefault(config.Params, "timeout", defaultWaitForPVsTimeout) 62 if err != nil { 63 return nil, err 64 } 65 66 stopCh := make(chan struct{}) 67 time.AfterFunc(timeout, func() { 68 close(stopCh) 69 }) 70 options := &measurementutil.WaitForPVOptions{ 71 Selector: selector, 72 DesiredPVCount: desiredPVCount, 73 Provisioner: provisioner, 74 CallerName: w.String(), 75 WaitForPVsInterval: defaultWaitForPVsInterval, 76 } 77 return nil, measurementutil.WaitForPVs(config.ClusterFramework.GetClientSets().GetClient(), stopCh, options) 78 } 79 80 // Dispose cleans up after the measurement. 81 func (*waitForAvailablePVsMeasurement) Dispose() {} 82 83 // String returns a string representation of the measurement. 84 func (*waitForAvailablePVsMeasurement) String() string { 85 return waitForAvailablePVsMeasurementName 86 }