Skip to content

Commit 88992ba

Browse files
authored
Merge pull request kubernetes#75986 from mucahitkurt/improvement/reduce-event-spam-for-GenerateAttachVolumeFunc
Reduce event spam for function GenerateAttachVolumeFunc
2 parents e8f28b5 + 1c1da75 commit 88992ba

File tree

3 files changed

+56
-54
lines changed

3 files changed

+56
-54
lines changed

pkg/volume/util/operationexecutor/operation_executor.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,8 @@ func (oe *operationExecutor) IsOperationPending(volumeName v1.UniqueVolumeName,
598598
func (oe *operationExecutor) AttachVolume(
599599
volumeToAttach VolumeToAttach,
600600
actualStateOfWorld ActualStateOfWorldAttacherUpdater) error {
601-
generatedOperations, err :=
601+
generatedOperations :=
602602
oe.operationGenerator.GenerateAttachVolumeFunc(volumeToAttach, actualStateOfWorld)
603-
if err != nil {
604-
return err
605-
}
606603

607604
return oe.pendingOperations.Run(
608605
volumeToAttach.VolumeName, "" /* podName */, generatedOperations)

pkg/volume/util/operationexecutor/operation_executor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,14 @@ func (fopg *fakeOperationGenerator) GenerateUnmountVolumeFunc(volumeToUnmount Mo
407407
OperationFunc: opFunc,
408408
}, nil
409409
}
410-
func (fopg *fakeOperationGenerator) GenerateAttachVolumeFunc(volumeToAttach VolumeToAttach, actualStateOfWorld ActualStateOfWorldAttacherUpdater) (volumetypes.GeneratedOperations, error) {
410+
func (fopg *fakeOperationGenerator) GenerateAttachVolumeFunc(volumeToAttach VolumeToAttach, actualStateOfWorld ActualStateOfWorldAttacherUpdater) volumetypes.GeneratedOperations {
411411
opFunc := func() (error, error) {
412412
startOperationAndBlock(fopg.ch, fopg.quit)
413413
return nil, nil
414414
}
415415
return volumetypes.GeneratedOperations{
416416
OperationFunc: opFunc,
417-
}, nil
417+
}
418418
}
419419
func (fopg *fakeOperationGenerator) GenerateDetachVolumeFunc(volumeToDetach AttachedVolume, verifySafeToDetach bool, actualStateOfWorld ActualStateOfWorldAttacherUpdater) (volumetypes.GeneratedOperations, error) {
420420
opFunc := func() (error, error) {

pkg/volume/util/operationexecutor/operation_generator.go

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ import (
4646
)
4747

4848
const (
49-
unknownVolumePlugin string = "UnknownVolumePlugin"
49+
unknownVolumePlugin string = "UnknownVolumePlugin"
50+
unknownAttachableVolumePlugin string = "UnknownAttachableVolumePlugin"
5051
)
5152

5253
var _ OperationGenerator = &operationGenerator{}
@@ -97,7 +98,7 @@ type OperationGenerator interface {
9798
GenerateUnmountVolumeFunc(volumeToUnmount MountedVolume, actualStateOfWorld ActualStateOfWorldMounterUpdater, podsDir string) (volumetypes.GeneratedOperations, error)
9899

99100
// 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
101102

102103
// Generates the DetachVolume function needed to perform the detach of a volume plugin
103104
GenerateDetachVolumeFunc(volumeToDetach AttachedVolume, verifySafeToDetach bool, actualStateOfWorld ActualStateOfWorldAttacherUpdater) (volumetypes.GeneratedOperations, error)
@@ -293,57 +294,41 @@ func (og *operationGenerator) GenerateBulkVolumeVerifyFunc(
293294

294295
func (og *operationGenerator) GenerateAttachVolumeFunc(
295296
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)
306304
}
307-
}
308305

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+
}
324313

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+
}
328325
}
329326

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)
337330
}
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-
}
345331

346-
attachVolumeFunc := func() (error, error) {
347332
// Execute attach
348333
devicePath, attachErr := volumeAttacher.Attach(
349334
volumeToAttach.VolumeSpec, volumeToAttach.NodeName)
@@ -389,12 +374,32 @@ func (og *operationGenerator) GenerateAttachVolumeFunc(
389374
return nil, nil
390375
}
391376

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+
392397
return volumetypes.GeneratedOperations{
393398
OperationName: "volume_attach",
394399
OperationFunc: attachVolumeFunc,
395400
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+
}
398403
}
399404

400405
func (og *operationGenerator) GetVolumePluginMgr() *volume.VolumePluginMgr {

0 commit comments

Comments
 (0)