Skip to content

Commit

Permalink
fix: add test case for informer event
Browse files Browse the repository at this point in the history
Signed-off-by: hlts2 <hiroto.funakoshi.hiroto@gmail.com>
  • Loading branch information
hlts2 committed Jan 15, 2025
1 parent fbbcc13 commit 48c41b4
Show file tree
Hide file tree
Showing 2 changed files with 415 additions and 259 deletions.
13 changes: 7 additions & 6 deletions pkg/driver/hook/prestop.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,23 @@ func (h *hook) waitForVolumeAttachmentsCleanup(ctx context.Context) error {
_, err := informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
DeleteFunc: func(obj interface{}) {
log.Warn().Msg("Received an object in DeleteFunc")
if err := h.volumeAttachmentEventHandler(ctx, obj); err != nil {
if err := h.volumeAttachmentEventHandler(ctx, obj, stopInformerFn); err != nil {
log.Error().
Str("node_name", h.nodeName).
Err(err).
Msg("An error occurred while handling the VolumeAttachment event in DeleteFunc")
return
}
stopInformerFn()
},
UpdateFunc: func(_, obj interface{}) {
log.Warn().Msg("Received an object in UpdateFunc")
if err := h.volumeAttachmentEventHandler(ctx, obj); err != nil {
if err := h.volumeAttachmentEventHandler(ctx, obj, stopInformerFn); err != nil {
log.Error().
Str("node_name", h.nodeName).
Err(err).
Msg("An error occurred while handling the VolumeAttachment event in UpdateFunc")
return
}
stopInformerFn()
},
})
if err != nil {
Expand Down Expand Up @@ -134,20 +132,22 @@ func (h *hook) waitForVolumeAttachmentsCleanup(ctx context.Context) error {
for {
select {
case <-informerCh:
log.Info().
Msg("Stopped informer to check event of VolumeAttachment resource")
return nil
case <-tick.C:
log.Info().
Msg("Waiting for VolumeAttachments to be deleted")
case <-ctx.Done():
log.Error().
Err(ctx.Err()).
Msg("Stopped waiting for VolumeAttachments, therefore some resources might still remain")
Msg("Stopped waiting for VolumeAttachments cleanup, therefore some resources might still remain")
return nil
}
}
}

func (h *hook) volumeAttachmentEventHandler(ctx context.Context, obj interface{}) error {
func (h *hook) volumeAttachmentEventHandler(ctx context.Context, obj interface{}, stopEventFn func()) error {
va, ok := obj.(*storagev1.VolumeAttachment)
if !ok {
return errors.New("received an object that is not a VolumeAttachment")
Expand All @@ -156,6 +156,7 @@ func (h *hook) volumeAttachmentEventHandler(ctx context.Context, obj interface{}
if _, err := h.checkVolumeAttachmentsExist(ctx); err != nil {
return err
}
stopEventFn()
}
return nil
}
Expand Down
Loading

0 comments on commit 48c41b4

Please # to comment.