k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/util/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 util
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"time"
    23  
    24  	clientset "k8s.io/client-go/kubernetes"
    25  	"k8s.io/klog/v2"
    26  	"k8s.io/perf-tests/clusterloader2/pkg/util"
    27  )
    28  
    29  // WaitForPVCOptions is an options used by WaitForPVCs methods.
    30  type WaitForPVCOptions struct {
    31  	Selector            *util.ObjectSelector
    32  	DesiredPVCCount     int
    33  	CallerName          string
    34  	WaitForPVCsInterval time.Duration
    35  }
    36  
    37  // WaitForPVCs waits till desired number of PVCs is running.
    38  // PVCs are be specified by namespace, field and/or label selectors.
    39  // If stopCh is closed before all PVCs are running, the error will be returned.
    40  func WaitForPVCs(clientSet clientset.Interface, stopCh <-chan struct{}, options *WaitForPVCOptions) error {
    41  	ps, err := NewPVCStore(clientSet, options.Selector)
    42  	if err != nil {
    43  		return fmt.Errorf("PVC store creation error: %v", err)
    44  	}
    45  	defer ps.Stop()
    46  
    47  	oldPVCs := ps.List()
    48  	scaling := uninitialized
    49  	var pvcsStatus PVCsStartupStatus
    50  
    51  	switch {
    52  	case len(oldPVCs) == options.DesiredPVCCount:
    53  		scaling = none
    54  	case len(oldPVCs) < options.DesiredPVCCount:
    55  		scaling = up
    56  	case len(oldPVCs) > options.DesiredPVCCount:
    57  		scaling = down
    58  	}
    59  
    60  	for {
    61  		select {
    62  		case <-stopCh:
    63  			return fmt.Errorf("timeout while waiting for %d PVCs to be running in namespace '%v' with labels '%v' and fields '%v' - only %d found bound",
    64  				options.DesiredPVCCount, options.Selector.Namespace, options.Selector.LabelSelector, options.Selector.FieldSelector, pvcsStatus.Bound)
    65  		case <-time.After(options.WaitForPVCsInterval):
    66  			pvcs := ps.List()
    67  			pvcsStatus = ComputePVCsStartupStatus(pvcs, options.DesiredPVCCount)
    68  
    69  			diff := DiffPVCs(oldPVCs, pvcs)
    70  			deletedPVCs := diff.DeletedPVCs()
    71  			if scaling != down && len(deletedPVCs) > 0 {
    72  				klog.Errorf("%s: %s: %d PVCs disappeared: %v", options.CallerName, options.Selector.String(), len(deletedPVCs), strings.Join(deletedPVCs, ", "))
    73  			}
    74  			addedPVCs := diff.AddedPVCs()
    75  			if scaling != up && len(addedPVCs) > 0 {
    76  				klog.Errorf("%s: %s: %d PVCs appeared: %v", options.CallerName, options.Selector.String(), len(deletedPVCs), strings.Join(deletedPVCs, ", "))
    77  			}
    78  			klog.V(2).Infof("%s: %s: %s", options.CallerName, options.Selector.String(), pvcsStatus.String())
    79  			// We wait until there is a desired number of PVCs bound and all other PVCs are pending.
    80  			if len(pvcs) == (pvcsStatus.Bound+pvcsStatus.Pending) && pvcsStatus.Bound == options.DesiredPVCCount {
    81  				return nil
    82  			}
    83  			oldPVCs = pvcs
    84  		}
    85  	}
    86  }