fakegenerator.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/types"
  18. "k8s.io/kubernetes/pkg/util/mount"
  19. "k8s.io/kubernetes/pkg/volume"
  20. volumetypes "k8s.io/kubernetes/pkg/volume/util/types"
  21. )
  22. // fakeOGCounter is a simple OperationGenerator which counts number of times a function
  23. // has been caled
  24. type fakeOGCounter struct {
  25. // calledFuncs stores name and count of functions
  26. calledFuncs map[string]int
  27. opFunc func() (error, error)
  28. }
  29. var _ OperationGenerator = &fakeOGCounter{}
  30. // NewFakeOGCounter returns a OperationGenerator
  31. func NewFakeOGCounter(opFunc func() (error, error)) OperationGenerator {
  32. return &fakeOGCounter{
  33. calledFuncs: map[string]int{},
  34. opFunc: opFunc,
  35. }
  36. }
  37. func (f *fakeOGCounter) GenerateMountVolumeFunc(waitForAttachTimeout time.Duration, volumeToMount VolumeToMount, actualStateOfWorldMounterUpdater ActualStateOfWorldMounterUpdater, isRemount bool) volumetypes.GeneratedOperations {
  38. return f.recordFuncCall("GenerateMountVolumeFunc")
  39. }
  40. func (f *fakeOGCounter) GenerateUnmountVolumeFunc(volumeToUnmount MountedVolume, actualStateOfWorld ActualStateOfWorldMounterUpdater, podsDir string) (volumetypes.GeneratedOperations, error) {
  41. return f.recordFuncCall("GenerateUnmountVolumeFunc"), nil
  42. }
  43. func (f *fakeOGCounter) GenerateAttachVolumeFunc(volumeToAttach VolumeToAttach, actualStateOfWorld ActualStateOfWorldAttacherUpdater) volumetypes.GeneratedOperations {
  44. return f.recordFuncCall("GenerateAttachVolumeFunc")
  45. }
  46. func (f *fakeOGCounter) GenerateDetachVolumeFunc(volumeToDetach AttachedVolume, verifySafeToDetach bool, actualStateOfWorld ActualStateOfWorldAttacherUpdater) (volumetypes.GeneratedOperations, error) {
  47. return f.recordFuncCall("GenerateDetachVolumeFunc"), nil
  48. }
  49. func (f *fakeOGCounter) GenerateVolumesAreAttachedFunc(attachedVolumes []AttachedVolume, nodeName types.NodeName, actualStateOfWorld ActualStateOfWorldAttacherUpdater) (volumetypes.GeneratedOperations, error) {
  50. return f.recordFuncCall("GenerateVolumesAreAttachedFunc"), nil
  51. }
  52. func (f *fakeOGCounter) GenerateUnmountDeviceFunc(deviceToDetach AttachedVolume, actualStateOfWorld ActualStateOfWorldMounterUpdater, mounter mount.Interface) (volumetypes.GeneratedOperations, error) {
  53. return f.recordFuncCall("GenerateUnmountDeviceFunc"), nil
  54. }
  55. func (f *fakeOGCounter) GenerateVerifyControllerAttachedVolumeFunc(volumeToMount VolumeToMount, nodeName types.NodeName, actualStateOfWorld ActualStateOfWorldAttacherUpdater) (volumetypes.GeneratedOperations, error) {
  56. return f.recordFuncCall("GenerateVerifyControllerAttachedVolumeFunc"), nil
  57. }
  58. func (f *fakeOGCounter) GenerateMapVolumeFunc(waitForAttachTimeout time.Duration, volumeToMount VolumeToMount, actualStateOfWorldMounterUpdater ActualStateOfWorldMounterUpdater) (volumetypes.GeneratedOperations, error) {
  59. return f.recordFuncCall("GenerateMapVolumeFunc"), nil
  60. }
  61. func (f *fakeOGCounter) GenerateUnmapVolumeFunc(volumeToUnmount MountedVolume, actualStateOfWorld ActualStateOfWorldMounterUpdater) (volumetypes.GeneratedOperations, error) {
  62. return f.recordFuncCall("GenerateUnmapVolumeFunc"), nil
  63. }
  64. func (f *fakeOGCounter) GenerateUnmapDeviceFunc(deviceToDetach AttachedVolume, actualStateOfWorld ActualStateOfWorldMounterUpdater, mounter mount.Interface) (volumetypes.GeneratedOperations, error) {
  65. return f.recordFuncCall("GenerateUnmapDeviceFunc"), nil
  66. }
  67. func (f *fakeOGCounter) GetVolumePluginMgr() *volume.VolumePluginMgr {
  68. return nil
  69. }
  70. func (f *fakeOGCounter) GenerateBulkVolumeVerifyFunc(
  71. map[types.NodeName][]*volume.Spec,
  72. string,
  73. map[*volume.Spec]v1.UniqueVolumeName, ActualStateOfWorldAttacherUpdater) (volumetypes.GeneratedOperations, error) {
  74. return f.recordFuncCall("GenerateBulkVolumeVerifyFunc"), nil
  75. }
  76. func (f *fakeOGCounter) GenerateExpandVolumeFunc(*v1.PersistentVolumeClaim, *v1.PersistentVolume) (volumetypes.GeneratedOperations, error) {
  77. return f.recordFuncCall("GenerateExpandVolumeFunc"), nil
  78. }
  79. func (f *fakeOGCounter) GenerateExpandInUseVolumeFunc(volumeToMount VolumeToMount, actualStateOfWorld ActualStateOfWorldMounterUpdater) (volumetypes.GeneratedOperations, error) {
  80. return f.recordFuncCall("GenerateExpandInUseVolumeFunc"), nil
  81. }
  82. func (f *fakeOGCounter) recordFuncCall(name string) volumetypes.GeneratedOperations {
  83. if _, ok := f.calledFuncs[name]; ok {
  84. f.calledFuncs[name]++
  85. }
  86. ops := volumetypes.GeneratedOperations{
  87. OperationName: name,
  88. OperationFunc: f.opFunc,
  89. }
  90. return ops
  91. }