Skip to content

Commit a2ee495

Browse files
authored
Merge pull request #50 from SLH335/fix-context
fix: nil context on volume detach and state change
2 parents 5ab108e + 3fb7de0 commit a2ee495

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

plugin.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func (d plugin) Create(r *volume.CreateRequest) error {
8383
logger.Infof("Creating volume '%s' ...", r.Name)
8484
logger.Debugf("Create: %+v", r)
8585

86+
ctx := context.TODO()
87+
8688
d.mutex.Lock()
8789
defer d.mutex.Unlock()
8890

@@ -98,7 +100,7 @@ func (d plugin) Create(r *volume.CreateRequest) error {
98100
}
99101
}
100102

101-
vol, err := volumes.Create(context.TODO(), d.blockClient, volumes.CreateOpts{
103+
vol, err := volumes.Create(ctx, d.blockClient, volumes.CreateOpts{
102104
Size: size,
103105
Name: r.Name,
104106
}, volumes.SchedulerHintOpts{}).Extract()
@@ -139,10 +141,12 @@ func (d plugin) List() (*volume.ListResponse, error) {
139141
logger := log.WithFields(log.Fields{"action": "list"})
140142
logger.Debugf("List")
141143

144+
ctx := context.TODO()
145+
142146
var vols []*volume.Volume
143147

144148
pager := volumes.List(d.blockClient, volumes.ListOpts{})
145-
err := pager.EachPage(context.TODO(), func(ctx context.Context, page pagination.Page) (bool, error) {
149+
err := pager.EachPage(ctx, func(ctx context.Context, page pagination.Page) (bool, error) {
146150
vList, _ := volumes.ExtractVolumes(page)
147151

148152
for _, v := range vList {
@@ -170,6 +174,8 @@ func (d plugin) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
170174
logger.Infof("Mounting volume '%s' ...", r.Name)
171175
logger.Debugf("Mount: %+v", r)
172176

177+
ctx := context.TODO()
178+
173179
d.mutex.Lock()
174180
defer d.mutex.Unlock()
175181

@@ -183,24 +189,24 @@ func (d plugin) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
183189

184190
if vol.Status == "creating" || vol.Status == "detaching" {
185191
logger.Infof("Volume is in '%s' state, wait for 'available'...", vol.Status)
186-
if vol, err = d.waitOnVolumeState(logger.Context, vol, "available"); err != nil {
192+
if vol, err = d.waitOnVolumeState(ctx, vol, "available"); err != nil {
187193
logger.Error(err.Error())
188194
return nil, err
189195
}
190196
}
191197

192-
if vol, err = volumes.Get(context.TODO(), d.blockClient, vol.ID).Extract(); err != nil {
198+
if vol, err = volumes.Get(ctx, d.blockClient, vol.ID).Extract(); err != nil {
193199
return nil, err
194200
}
195201

196202
if len(vol.Attachments) > 0 {
197203
logger.Debug("Volume already attached, detaching first")
198-
if vol, err = d.detachVolume(logger.Context, vol); err != nil {
204+
if vol, err = d.detachVolume(ctx, vol); err != nil {
199205
logger.WithError(err).Error("Error detaching volume")
200206
return nil, err
201207
}
202208

203-
if vol, err = d.waitOnVolumeState(logger.Context, vol, "available"); err != nil {
209+
if vol, err = d.waitOnVolumeState(ctx, vol, "available"); err != nil {
204210
logger.WithError(err).Error("Error detaching volume")
205211
return nil, err
206212
}
@@ -216,7 +222,7 @@ func (d plugin) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
216222
// Attaching block volume to compute instance
217223

218224
opts := volumeattach.CreateOpts{VolumeID: vol.ID}
219-
_, err = volumeattach.Create(context.TODO(), d.computeClient, d.config.MachineID, opts).Extract()
225+
_, err = volumeattach.Create(ctx, d.computeClient, d.config.MachineID, opts).Extract()
220226

221227
if err != nil {
222228
logger.WithError(err).Errorf("Error attaching volume: %s", err.Error())
@@ -295,6 +301,8 @@ func (d plugin) Remove(r *volume.RemoveRequest) error {
295301
logger.Infof("Removing volume '%s' ...", r.Name)
296302
logger.Debugf("Remove: %+v", r)
297303

304+
ctx := context.TODO()
305+
298306
vol, err := d.getByName(r.Name)
299307

300308
if err != nil {
@@ -306,15 +314,15 @@ func (d plugin) Remove(r *volume.RemoveRequest) error {
306314

307315
if len(vol.Attachments) > 0 {
308316
logger.Debug("Volume still attached, detaching first")
309-
if vol, err = d.detachVolume(logger.Context, vol); err != nil {
317+
if vol, err = d.detachVolume(ctx, vol); err != nil {
310318
logger.WithError(err).Error("Error detaching volume")
311319
return err
312320
}
313321
}
314322

315323
logger.Debug("Deleting block volume...")
316324

317-
err = volumes.Delete(context.TODO(), d.blockClient, vol.ID, volumes.DeleteOpts{}).ExtractErr()
325+
err = volumes.Delete(ctx, d.blockClient, vol.ID, volumes.DeleteOpts{}).ExtractErr()
318326
if err != nil {
319327
logger.WithError(err).Errorf("Error deleting volume: %s", err.Error())
320328
return err
@@ -330,6 +338,8 @@ func (d plugin) Unmount(r *volume.UnmountRequest) error {
330338
logger.Infof("Unmounting volume '%s' ...", r.Name)
331339
logger.Debugf("Unmount: %+v", r)
332340

341+
ctx := context.TODO()
342+
333343
d.mutex.Lock()
334344
defer d.mutex.Unlock()
335345

@@ -350,7 +360,7 @@ func (d plugin) Unmount(r *volume.UnmountRequest) error {
350360
if err != nil {
351361
logger.WithError(err).Error("Error retriving volume")
352362
} else {
353-
_, err = d.detachVolume(logger.Context, vol)
363+
_, err = d.detachVolume(ctx, vol)
354364
if err != nil {
355365
logger.WithError(err).Error("Error detaching volume")
356366
}
@@ -362,8 +372,10 @@ func (d plugin) Unmount(r *volume.UnmountRequest) error {
362372
func (d plugin) getByName(name string) (*volumes.Volume, error) {
363373
var volume *volumes.Volume
364374

375+
ctx := context.TODO()
376+
365377
pager := volumes.List(d.blockClient, volumes.ListOpts{Name: name})
366-
err := pager.EachPage(context.TODO(), func(ctx context.Context, page pagination.Page) (bool, error) {
378+
err := pager.EachPage(ctx, func(ctx context.Context, page pagination.Page) (bool, error) {
367379
vList, err := volumes.ExtractVolumes(page)
368380

369381
if err != nil {
@@ -416,7 +428,7 @@ func (d plugin) waitOnVolumeState(ctx context.Context, vol *volumes.Volume, stat
416428
}
417429
}
418430

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

421-
return nil, fmt.Errorf("Volume status did became %s", status)
433+
return nil, fmt.Errorf("Volume status changed to %s", status)
422434
}

0 commit comments

Comments
 (0)