...

Source file src/k8s.io/kubernetes/pkg/volume/util/operationexecutor/fakegenerator.go

Documentation: k8s.io/kubernetes/pkg/volume/util/operationexecutor

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

View as plain text