k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/volume/util/operationexecutor/fakegenerator.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 operationexecutor
    18  
    19  import (
    20  	"time"
    21  
    22  	"k8s.io/klog/v2"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/api/resource"
    26  	"k8s.io/apimachinery/pkg/types"
    27  	csitrans "k8s.io/csi-translation-lib"
    28  	"k8s.io/kubernetes/pkg/volume"
    29  	"k8s.io/kubernetes/pkg/volume/util/hostutil"
    30  	volumetypes "k8s.io/kubernetes/pkg/volume/util/types"
    31  )
    32  
    33  // fakeOGCounter is a simple OperationGenerator which counts number of times a function
    34  // has been caled
    35  type fakeOGCounter struct {
    36  	// calledFuncs stores name and count of functions
    37  	calledFuncs map[string]int
    38  	opFunc      func() volumetypes.OperationContext
    39  }
    40  
    41  var _ OperationGenerator = &fakeOGCounter{}
    42  
    43  // NewFakeOGCounter returns a OperationGenerator
    44  func NewFakeOGCounter(opFunc func() volumetypes.OperationContext) OperationGenerator {
    45  	return &fakeOGCounter{
    46  		calledFuncs: map[string]int{},
    47  		opFunc:      opFunc,
    48  	}
    49  }
    50  
    51  func (f *fakeOGCounter) GenerateMountVolumeFunc(waitForAttachTimeout time.Duration, volumeToMount VolumeToMount, actualStateOfWorldMounterUpdater ActualStateOfWorldMounterUpdater, isRemount bool) volumetypes.GeneratedOperations {
    52  	return f.recordFuncCall("GenerateMountVolumeFunc")
    53  }
    54  
    55  func (f *fakeOGCounter) GenerateUnmountVolumeFunc(volumeToUnmount MountedVolume, actualStateOfWorld ActualStateOfWorldMounterUpdater, podsDir string) (volumetypes.GeneratedOperations, error) {
    56  	return f.recordFuncCall("GenerateUnmountVolumeFunc"), nil
    57  }
    58  
    59  func (f *fakeOGCounter) GenerateAttachVolumeFunc(logger klog.Logger, volumeToAttach VolumeToAttach, actualStateOfWorld ActualStateOfWorldAttacherUpdater) volumetypes.GeneratedOperations {
    60  	return f.recordFuncCall("GenerateAttachVolumeFunc")
    61  }
    62  
    63  func (f *fakeOGCounter) GenerateDetachVolumeFunc(logger klog.Logger, volumeToDetach AttachedVolume, verifySafeToDetach bool, actualStateOfWorld ActualStateOfWorldAttacherUpdater) (volumetypes.GeneratedOperations, error) {
    64  	return f.recordFuncCall("GenerateDetachVolumeFunc"), nil
    65  }
    66  
    67  func (f *fakeOGCounter) GenerateVolumesAreAttachedFunc(attachedVolumes []AttachedVolume, nodeName types.NodeName, actualStateOfWorld ActualStateOfWorldAttacherUpdater) (volumetypes.GeneratedOperations, error) {
    68  	return f.recordFuncCall("GenerateVolumesAreAttachedFunc"), nil
    69  }
    70  
    71  func (f *fakeOGCounter) GenerateUnmountDeviceFunc(deviceToDetach AttachedVolume, actualStateOfWorld ActualStateOfWorldMounterUpdater, hu hostutil.HostUtils) (volumetypes.GeneratedOperations, error) {
    72  	return f.recordFuncCall("GenerateUnmountDeviceFunc"), nil
    73  }
    74  
    75  func (f *fakeOGCounter) GenerateVerifyControllerAttachedVolumeFunc(logger klog.Logger, volumeToMount VolumeToMount, nodeName types.NodeName, actualStateOfWorld ActualStateOfWorldAttacherUpdater) (volumetypes.GeneratedOperations, error) {
    76  	return f.recordFuncCall("GenerateVerifyControllerAttachedVolumeFunc"), nil
    77  }
    78  
    79  func (f *fakeOGCounter) GenerateMapVolumeFunc(waitForAttachTimeout time.Duration, volumeToMount VolumeToMount, actualStateOfWorldMounterUpdater ActualStateOfWorldMounterUpdater) (volumetypes.GeneratedOperations, error) {
    80  	return f.recordFuncCall("GenerateMapVolumeFunc"), nil
    81  }
    82  
    83  func (f *fakeOGCounter) GenerateUnmapVolumeFunc(volumeToUnmount MountedVolume, actualStateOfWorld ActualStateOfWorldMounterUpdater) (volumetypes.GeneratedOperations, error) {
    84  	return f.recordFuncCall("GenerateUnmapVolumeFunc"), nil
    85  }
    86  
    87  func (f *fakeOGCounter) GenerateUnmapDeviceFunc(deviceToDetach AttachedVolume, actualStateOfWorld ActualStateOfWorldMounterUpdater, hu hostutil.HostUtils) (volumetypes.GeneratedOperations, error) {
    88  	return f.recordFuncCall("GenerateUnmapDeviceFunc"), nil
    89  }
    90  
    91  func (f *fakeOGCounter) GetVolumePluginMgr() *volume.VolumePluginMgr {
    92  	return nil
    93  }
    94  
    95  func (f *fakeOGCounter) GetCSITranslator() InTreeToCSITranslator {
    96  	return csitrans.New()
    97  }
    98  
    99  func (f *fakeOGCounter) GenerateExpandVolumeFunc(*v1.PersistentVolumeClaim, *v1.PersistentVolume) (volumetypes.GeneratedOperations, error) {
   100  	return f.recordFuncCall("GenerateExpandVolumeFunc"), nil
   101  }
   102  
   103  func (f *fakeOGCounter) GenerateExpandAndRecoverVolumeFunc(*v1.PersistentVolumeClaim, *v1.PersistentVolume, string) (volumetypes.GeneratedOperations, error) {
   104  	return f.recordFuncCall("GenerateExpandVolumeFunc"), nil
   105  }
   106  
   107  func (f *fakeOGCounter) GenerateExpandInUseVolumeFunc(volumeToMount VolumeToMount, actualStateOfWorld ActualStateOfWorldMounterUpdater, currentSize resource.Quantity) (volumetypes.GeneratedOperations, error) {
   108  	return f.recordFuncCall("GenerateExpandInUseVolumeFunc"), nil
   109  }
   110  
   111  func (f *fakeOGCounter) recordFuncCall(name string) volumetypes.GeneratedOperations {
   112  	if _, ok := f.calledFuncs[name]; ok {
   113  		f.calledFuncs[name]++
   114  	}
   115  	ops := volumetypes.GeneratedOperations{
   116  		OperationName: name,
   117  		OperationFunc: f.opFunc,
   118  	}
   119  	return ops
   120  }