fakegenerator.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package operationexecutor
  14. import (
  15. "time"
  16. v1 "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/types"
  18. csitrans "k8s.io/csi-translation-lib"
  19. "k8s.io/kubernetes/pkg/volume"
  20. "k8s.io/kubernetes/pkg/volume/util/hostutil"
  21. volumetypes "k8s.io/kubernetes/pkg/volume/util/types"
  22. )
  23. // fakeOGCounter is a simple OperationGenerator which counts number of times a function
  24. // has been caled
  25. type fakeOGCounter struct {
  26. // calledFuncs stores name and count of functions
  27. calledFuncs map[string]int
  28. opFunc func() (error, error)
  29. }
  30. var _ OperationGenerator = &fakeOGCounter{}
  31. // NewFakeOGCounter returns a OperationGenerator
  32. func NewFakeOGCounter(opFunc func() (error, error)) OperationGenerator {
  33. return &fakeOGCounter{
  34. calledFuncs: map[string]int{},
  35. opFunc: opFunc,
  36. }
  37. }
  38. func (f *fakeOGCounter) GenerateMountVolumeFunc(waitForAttachTimeout time.Duration, volumeToMount VolumeToMount, actualStateOfWorldMounterUpdater ActualStateOfWorldMounterUpdater, isRemount bool) volumetypes.GeneratedOperations {
  39. return f.recordFuncCall("GenerateMountVolumeFunc")
  40. }
  41. func (f *fakeOGCounter) GenerateUnmountVolumeFunc(volumeToUnmount MountedVolume, actualStateOfWorld ActualStateOfWorldMounterUpdater, podsDir string) (volumetypes.GeneratedOperations, error) {
  42. return f.recordFuncCall("GenerateUnmountVolumeFunc"), nil
  43. }
  44. func (f *fakeOGCounter) GenerateAttachVolumeFunc(volumeToAttach VolumeToAttach, actualStateOfWorld ActualStateOfWorldAttacherUpdater) volumetypes.GeneratedOperations {
  45. return f.recordFuncCall("GenerateAttachVolumeFunc")
  46. }
  47. func (f *fakeOGCounter) GenerateDetachVolumeFunc(volumeToDetach AttachedVolume, verifySafeToDetach bool, actualStateOfWorld ActualStateOfWorldAttacherUpdater) (volumetypes.GeneratedOperations, error) {
  48. return f.recordFuncCall("GenerateDetachVolumeFunc"), nil
  49. }
  50. func (f *fakeOGCounter) GenerateVolumesAreAttachedFunc(attachedVolumes []AttachedVolume, nodeName types.NodeName, actualStateOfWorld ActualStateOfWorldAttacherUpdater) (volumetypes.GeneratedOperations, error) {
  51. return f.recordFuncCall("GenerateVolumesAreAttachedFunc"), nil
  52. }
  53. func (f *fakeOGCounter) GenerateUnmountDeviceFunc(deviceToDetach AttachedVolume, actualStateOfWorld ActualStateOfWorldMounterUpdater, hu hostutil.HostUtils) (volumetypes.GeneratedOperations, error) {
  54. return f.recordFuncCall("GenerateUnmountDeviceFunc"), nil
  55. }
  56. func (f *fakeOGCounter) GenerateVerifyControllerAttachedVolumeFunc(volumeToMount VolumeToMount, nodeName types.NodeName, actualStateOfWorld ActualStateOfWorldAttacherUpdater) (volumetypes.GeneratedOperations, error) {
  57. return f.recordFuncCall("GenerateVerifyControllerAttachedVolumeFunc"), nil
  58. }
  59. func (f *fakeOGCounter) GenerateMapVolumeFunc(waitForAttachTimeout time.Duration, volumeToMount VolumeToMount, actualStateOfWorldMounterUpdater ActualStateOfWorldMounterUpdater) (volumetypes.GeneratedOperations, error) {
  60. return f.recordFuncCall("GenerateMapVolumeFunc"), nil
  61. }
  62. func (f *fakeOGCounter) GenerateUnmapVolumeFunc(volumeToUnmount MountedVolume, actualStateOfWorld ActualStateOfWorldMounterUpdater) (volumetypes.GeneratedOperations, error) {
  63. return f.recordFuncCall("GenerateUnmapVolumeFunc"), nil
  64. }
  65. func (f *fakeOGCounter) GenerateUnmapDeviceFunc(deviceToDetach AttachedVolume, actualStateOfWorld ActualStateOfWorldMounterUpdater, hu hostutil.HostUtils) (volumetypes.GeneratedOperations, error) {
  66. return f.recordFuncCall("GenerateUnmapDeviceFunc"), nil
  67. }
  68. func (f *fakeOGCounter) GetVolumePluginMgr() *volume.VolumePluginMgr {
  69. return nil
  70. }
  71. func (f *fakeOGCounter) GetCSITranslator() InTreeToCSITranslator {
  72. return csitrans.New()
  73. }
  74. func (f *fakeOGCounter) GenerateBulkVolumeVerifyFunc(
  75. map[types.NodeName][]*volume.Spec,
  76. string,
  77. map[*volume.Spec]v1.UniqueVolumeName, ActualStateOfWorldAttacherUpdater) (volumetypes.GeneratedOperations, error) {
  78. return f.recordFuncCall("GenerateBulkVolumeVerifyFunc"), nil
  79. }
  80. func (f *fakeOGCounter) GenerateExpandVolumeFunc(*v1.PersistentVolumeClaim, *v1.PersistentVolume) (volumetypes.GeneratedOperations, error) {
  81. return f.recordFuncCall("GenerateExpandVolumeFunc"), nil
  82. }
  83. func (f *fakeOGCounter) GenerateExpandInUseVolumeFunc(volumeToMount VolumeToMount, actualStateOfWorld ActualStateOfWorldMounterUpdater) (volumetypes.GeneratedOperations, error) {
  84. return f.recordFuncCall("GenerateExpandInUseVolumeFunc"), nil
  85. }
  86. func (f *fakeOGCounter) recordFuncCall(name string) volumetypes.GeneratedOperations {
  87. if _, ok := f.calledFuncs[name]; ok {
  88. f.calledFuncs[name]++
  89. }
  90. ops := volumetypes.GeneratedOperations{
  91. OperationName: name,
  92. OperationFunc: f.opFunc,
  93. }
  94. return ops
  95. }