@@ -46,7 +46,8 @@ import (
46
46
)
47
47
48
48
const (
49
- unknownVolumePlugin string = "UnknownVolumePlugin"
49
+ unknownVolumePlugin string = "UnknownVolumePlugin"
50
+ unknownAttachableVolumePlugin string = "UnknownAttachableVolumePlugin"
50
51
)
51
52
52
53
var _ OperationGenerator = & operationGenerator {}
@@ -97,7 +98,7 @@ type OperationGenerator interface {
97
98
GenerateUnmountVolumeFunc (volumeToUnmount MountedVolume , actualStateOfWorld ActualStateOfWorldMounterUpdater , podsDir string ) (volumetypes.GeneratedOperations , error )
98
99
99
100
// Generates the AttachVolume function needed to perform attach of a volume plugin
100
- GenerateAttachVolumeFunc (volumeToAttach VolumeToAttach , actualStateOfWorld ActualStateOfWorldAttacherUpdater ) ( volumetypes.GeneratedOperations , error )
101
+ GenerateAttachVolumeFunc (volumeToAttach VolumeToAttach , actualStateOfWorld ActualStateOfWorldAttacherUpdater ) volumetypes.GeneratedOperations
101
102
102
103
// Generates the DetachVolume function needed to perform the detach of a volume plugin
103
104
GenerateDetachVolumeFunc (volumeToDetach AttachedVolume , verifySafeToDetach bool , actualStateOfWorld ActualStateOfWorldAttacherUpdater ) (volumetypes.GeneratedOperations , error )
@@ -293,57 +294,41 @@ func (og *operationGenerator) GenerateBulkVolumeVerifyFunc(
293
294
294
295
func (og * operationGenerator ) GenerateAttachVolumeFunc (
295
296
volumeToAttach VolumeToAttach ,
296
- actualStateOfWorld ActualStateOfWorldAttacherUpdater ) (volumetypes.GeneratedOperations , error ) {
297
- var err error
298
- var attachableVolumePlugin volume.AttachableVolumePlugin
299
-
300
- // Get attacher plugin
301
- eventRecorderFunc := func (err * error ) {
302
- if * err != nil {
303
- for _ , pod := range volumeToAttach .ScheduledPods {
304
- og .recorder .Eventf (pod , v1 .EventTypeWarning , kevents .FailedAttachVolume , (* err ).Error ())
305
- }
297
+ actualStateOfWorld ActualStateOfWorldAttacherUpdater ) volumetypes.GeneratedOperations {
298
+ attachVolumeFunc := func () (error , error ) {
299
+ var attachableVolumePlugin volume.AttachableVolumePlugin
300
+ originalSpec := volumeToAttach .VolumeSpec
301
+ nu , err := nodeUsingCSIPlugin (og , volumeToAttach .VolumeSpec , volumeToAttach .NodeName )
302
+ if err != nil {
303
+ return volumeToAttach .GenerateError ("AttachVolume.NodeUsingCSIPlugin failed" , err )
306
304
}
307
- }
308
305
309
- originalSpec := volumeToAttach .VolumeSpec
310
- nu , err := nodeUsingCSIPlugin (og , volumeToAttach .VolumeSpec , volumeToAttach .NodeName )
311
- if err != nil {
312
- eventRecorderFunc (& err )
313
- return volumetypes.GeneratedOperations {}, volumeToAttach .GenerateErrorDetailed ("AttachVolume.NodeUsingCSIPlugin failed" , err )
314
- }
315
-
316
- // useCSIPlugin will check both CSIMigration and the plugin specific feature gate
317
- if useCSIPlugin (og .volumePluginMgr , volumeToAttach .VolumeSpec ) && nu {
318
- // The volume represented by this spec is CSI and thus should be migrated
319
- attachableVolumePlugin , err = og .volumePluginMgr .FindAttachablePluginByName (csi .CSIPluginName )
320
- if err != nil || attachableVolumePlugin == nil {
321
- eventRecorderFunc (& err )
322
- return volumetypes.GeneratedOperations {}, volumeToAttach .GenerateErrorDetailed ("AttachVolume.FindAttachablePluginByName failed" , err )
323
- }
306
+ // useCSIPlugin will check both CSIMigration and the plugin specific feature gate
307
+ if useCSIPlugin (og .volumePluginMgr , volumeToAttach .VolumeSpec ) && nu {
308
+ // The volume represented by this spec is CSI and thus should be migrated
309
+ attachableVolumePlugin , err = og .volumePluginMgr .FindAttachablePluginByName (csi .CSIPluginName )
310
+ if err != nil || attachableVolumePlugin == nil {
311
+ return volumeToAttach .GenerateError ("AttachVolume.FindAttachablePluginByName failed" , err )
312
+ }
324
313
325
- csiSpec , err := translateSpec (volumeToAttach .VolumeSpec )
326
- if err != nil {
327
- return volumetypes.GeneratedOperations {}, volumeToAttach .GenerateErrorDetailed ("AttachVolume.TranslateSpec failed" , err )
314
+ csiSpec , err := translateSpec (volumeToAttach .VolumeSpec )
315
+ if err != nil {
316
+ return volumeToAttach .GenerateError ("AttachVolume.TranslateSpec failed" , err )
317
+ }
318
+ volumeToAttach .VolumeSpec = csiSpec
319
+ } else {
320
+ attachableVolumePlugin , err =
321
+ og .volumePluginMgr .FindAttachablePluginBySpec (volumeToAttach .VolumeSpec )
322
+ if err != nil || attachableVolumePlugin == nil {
323
+ return volumeToAttach .GenerateError ("AttachVolume.FindAttachablePluginBySpec failed" , err )
324
+ }
328
325
}
329
326
330
- volumeToAttach .VolumeSpec = csiSpec
331
- } else {
332
- attachableVolumePlugin , err =
333
- og .volumePluginMgr .FindAttachablePluginBySpec (volumeToAttach .VolumeSpec )
334
- if err != nil || attachableVolumePlugin == nil {
335
- eventRecorderFunc (& err )
336
- return volumetypes.GeneratedOperations {}, volumeToAttach .GenerateErrorDetailed ("AttachVolume.FindAttachablePluginBySpec failed" , err )
327
+ volumeAttacher , newAttacherErr := attachableVolumePlugin .NewAttacher ()
328
+ if newAttacherErr != nil {
329
+ return volumeToAttach .GenerateError ("AttachVolume.NewAttacher failed" , newAttacherErr )
337
330
}
338
- }
339
-
340
- volumeAttacher , newAttacherErr := attachableVolumePlugin .NewAttacher ()
341
- if newAttacherErr != nil {
342
- eventRecorderFunc (& err )
343
- return volumetypes.GeneratedOperations {}, volumeToAttach .GenerateErrorDetailed ("AttachVolume.NewAttacher failed" , newAttacherErr )
344
- }
345
331
346
- attachVolumeFunc := func () (error , error ) {
347
332
// Execute attach
348
333
devicePath , attachErr := volumeAttacher .Attach (
349
334
volumeToAttach .VolumeSpec , volumeToAttach .NodeName )
@@ -389,12 +374,32 @@ func (og *operationGenerator) GenerateAttachVolumeFunc(
389
374
return nil , nil
390
375
}
391
376
377
+ eventRecorderFunc := func (err * error ) {
378
+ if * err != nil {
379
+ for _ , pod := range volumeToAttach .ScheduledPods {
380
+ og .recorder .Eventf (pod , v1 .EventTypeWarning , kevents .FailedAttachVolume , (* err ).Error ())
381
+ }
382
+ }
383
+ }
384
+
385
+ // Get attacher plugin
386
+ attachableVolumePluginName := unknownAttachableVolumePlugin
387
+ attachableVolumePlugin , err :=
388
+ og .volumePluginMgr .FindAttachablePluginBySpec (volumeToAttach .VolumeSpec )
389
+ // It's ok to ignore the error, returning error is not expected from this function.
390
+ // If an error case occurred during the function generation, this error case(skipped one) will also trigger an error
391
+ // while the generated function is executed. And those errors will be handled during the execution of the generated
392
+ // function with a back off policy.
393
+ if err == nil && attachableVolumePlugin != nil {
394
+ attachableVolumePluginName = attachableVolumePlugin .GetPluginName ()
395
+ }
396
+
392
397
return volumetypes.GeneratedOperations {
393
398
OperationName : "volume_attach" ,
394
399
OperationFunc : attachVolumeFunc ,
395
400
EventRecorderFunc : eventRecorderFunc ,
396
- CompleteFunc : util .OperationCompleteHook (util .GetFullQualifiedPluginNameForVolume (attachableVolumePlugin . GetPluginName () , volumeToAttach .VolumeSpec ), "volume_attach" ),
397
- }, nil
401
+ CompleteFunc : util .OperationCompleteHook (util .GetFullQualifiedPluginNameForVolume (attachableVolumePluginName , volumeToAttach .VolumeSpec ), "volume_attach" ),
402
+ }
398
403
}
399
404
400
405
func (og * operationGenerator ) GetVolumePluginMgr () * volume.VolumePluginMgr {
0 commit comments