Skip to content

Commit

Permalink
Merge branch 'master' into verify/prestop-hook
Browse files Browse the repository at this point in the history
  • Loading branch information
hlts2 authored Jan 15, 2025
2 parents 9d03961 + 1d20f22 commit 5d2498d
Show file tree
Hide file tree
Showing 3 changed files with 416 additions and 260 deletions.
2 changes: 1 addition & 1 deletion pkg/driver/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

// Hook defines the lifecycle methods for a hook, such as PreStop, PostStart, and PostStop.
// Hook defines the lifecycle methods for a hook, such as PreStop, PostStart, etc.
// Implementations of this interface can define actions to be performed at different lifecycle stages.
type Hook interface {
PreStop(ctx context.Context) error
Expand Down
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 5d2498d

Please # to comment.