Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nil context on volume detach and state change #50

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (d plugin) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {

if vol.Status == "creating" || vol.Status == "detaching" {
logger.Infof("Volume is in '%s' state, wait for 'available'...", vol.Status)
if vol, err = d.waitOnVolumeState(logger.Context, vol, "available"); err != nil {
if vol, err = d.waitOnVolumeState(vol, "available"); err != nil {
logger.Error(err.Error())
return nil, err
}
Expand All @@ -195,12 +195,12 @@ func (d plugin) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {

if len(vol.Attachments) > 0 {
logger.Debug("Volume already attached, detaching first")
if vol, err = d.detachVolume(logger.Context, vol); err != nil {
if vol, err = d.detachVolume(vol); err != nil {
logger.WithError(err).Error("Error detaching volume")
return nil, err
}

if vol, err = d.waitOnVolumeState(logger.Context, vol, "available"); err != nil {
if vol, err = d.waitOnVolumeState(vol, "available"); err != nil {
logger.WithError(err).Error("Error detaching volume")
return nil, err
}
Expand Down Expand Up @@ -304,7 +304,7 @@ func (d plugin) Remove(r *volume.RemoveRequest) error {

if len(vol.Attachments) > 0 {
logger.Debug("Volume still attached, detaching first")
if vol, err = d.detachVolume(logger.Context, vol); err != nil {
if vol, err = d.detachVolume(vol); err != nil {
logger.WithError(err).Error("Error detaching volume")
return err
}
Expand Down Expand Up @@ -348,7 +348,7 @@ func (d plugin) Unmount(r *volume.UnmountRequest) error {
if err != nil {
logger.WithError(err).Error("Error retriving volume")
} else {
_, err = d.detachVolume(logger.Context, vol)
_, err = d.detachVolume(vol)
if err != nil {
logger.WithError(err).Error("Error detaching volume")
}
Expand Down Expand Up @@ -385,9 +385,9 @@ func (d plugin) getByName(name string) (*volumes.Volume, error) {
return volume, err
}

func (d plugin) detachVolume(ctx context.Context, vol *volumes.Volume) (*volumes.Volume, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we keep context.Context as an argument to all internal functions, and pass a single context.TODO() from the main functions?

func (d plugin) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
    ctx := context.TODO()
    ...
        if vol, err = d.waitOnVolumeState(ctx, vol, "available"); err != nil {
    ...
        if vol, err = d.detachVolume(ctx, vol); err != nil {
    ...

This way, we would be able to e.g. pass a cancelable context later if needed (https://pkg.go.dev/context).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea. Should the same instance of context.TODO() be passed to the other functions using their own instance of it at the moment?

Like in this line, for example:
https://github.com/SLH335/docker-plugin-cinder/blob/93cab39892abd699b64a6b3f67d38d5be2bed382/plugin.go#L192

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that sounds about right! 👍

func (d plugin) detachVolume(vol *volumes.Volume) (*volumes.Volume, error) {
for _, att := range vol.Attachments {
err := volumeattach.Delete(ctx, d.computeClient, att.ServerID, att.ID).ExtractErr()
err := volumeattach.Delete(context.Background(), d.computeClient, att.ServerID, att.ID).ExtractErr()
if err != nil {
return nil, err
}
Expand All @@ -396,15 +396,15 @@ func (d plugin) detachVolume(ctx context.Context, vol *volumes.Volume) (*volumes
return vol, nil
}

func (d plugin) waitOnVolumeState(ctx context.Context, vol *volumes.Volume, status string) (*volumes.Volume, error) {
func (d plugin) waitOnVolumeState(vol *volumes.Volume, status string) (*volumes.Volume, error) {
if vol.Status == status {
return vol, nil
}

for i := 1; i <= 10; i++ {
time.Sleep(500 * time.Millisecond)

vol, err := volumes.Get(ctx, d.blockClient, vol.ID).Extract()
vol, err := volumes.Get(context.Background(), d.blockClient, vol.ID).Extract()
if err != nil {
return nil, err
}
Expand All @@ -414,7 +414,7 @@ func (d plugin) waitOnVolumeState(ctx context.Context, vol *volumes.Volume, stat
}
}

log.WithContext(ctx).Debugf("Volume did not become %s: %+v", status, vol)
log.Debugf("Volume status did not change to %s: %+v", status, vol)

return nil, fmt.Errorf("Volume status did became %s", status)
return nil, fmt.Errorf("Volume status changed to %s", status)
}
Loading