Skip to content

Commit 3fb7de0

Browse files
committed
chore: pass todo context to internal functions
1 parent 7d8b81c commit 3fb7de0

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

plugin.go

Lines changed: 27 additions & 15 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(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(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(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())
@@ -293,6 +299,8 @@ func (d plugin) Remove(r *volume.RemoveRequest) error {
293299
logger.Infof("Removing volume '%s' ...", r.Name)
294300
logger.Debugf("Remove: %+v", r)
295301

302+
ctx := context.TODO()
303+
296304
vol, err := d.getByName(r.Name)
297305

298306
if err != nil {
@@ -304,15 +312,15 @@ func (d plugin) Remove(r *volume.RemoveRequest) error {
304312

305313
if len(vol.Attachments) > 0 {
306314
logger.Debug("Volume still attached, detaching first")
307-
if vol, err = d.detachVolume(vol); err != nil {
315+
if vol, err = d.detachVolume(ctx, vol); err != nil {
308316
logger.WithError(err).Error("Error detaching volume")
309317
return err
310318
}
311319
}
312320

313321
logger.Debug("Deleting block volume...")
314322

315-
err = volumes.Delete(context.TODO(), d.blockClient, vol.ID, volumes.DeleteOpts{}).ExtractErr()
323+
err = volumes.Delete(ctx, d.blockClient, vol.ID, volumes.DeleteOpts{}).ExtractErr()
316324
if err != nil {
317325
logger.WithError(err).Errorf("Error deleting volume: %s", err.Error())
318326
return err
@@ -328,6 +336,8 @@ func (d plugin) Unmount(r *volume.UnmountRequest) error {
328336
logger.Infof("Unmounting volume '%s' ...", r.Name)
329337
logger.Debugf("Unmount: %+v", r)
330338

339+
ctx := context.TODO()
340+
331341
d.mutex.Lock()
332342
defer d.mutex.Unlock()
333343

@@ -348,7 +358,7 @@ func (d plugin) Unmount(r *volume.UnmountRequest) error {
348358
if err != nil {
349359
logger.WithError(err).Error("Error retriving volume")
350360
} else {
351-
_, err = d.detachVolume(vol)
361+
_, err = d.detachVolume(ctx, vol)
352362
if err != nil {
353363
logger.WithError(err).Error("Error detaching volume")
354364
}
@@ -360,8 +370,10 @@ func (d plugin) Unmount(r *volume.UnmountRequest) error {
360370
func (d plugin) getByName(name string) (*volumes.Volume, error) {
361371
var volume *volumes.Volume
362372

373+
ctx := context.TODO()
374+
363375
pager := volumes.List(d.blockClient, volumes.ListOpts{Name: name})
364-
err := pager.EachPage(context.TODO(), func(ctx context.Context, page pagination.Page) (bool, error) {
376+
err := pager.EachPage(ctx, func(ctx context.Context, page pagination.Page) (bool, error) {
365377
vList, err := volumes.ExtractVolumes(page)
366378

367379
if err != nil {
@@ -385,9 +397,9 @@ func (d plugin) getByName(name string) (*volumes.Volume, error) {
385397
return volume, err
386398
}
387399

388-
func (d plugin) detachVolume(vol *volumes.Volume) (*volumes.Volume, error) {
400+
func (d plugin) detachVolume(ctx context.Context, vol *volumes.Volume) (*volumes.Volume, error) {
389401
for _, att := range vol.Attachments {
390-
err := volumeattach.Delete(context.Background(), d.computeClient, att.ServerID, att.ID).ExtractErr()
402+
err := volumeattach.Delete(ctx, d.computeClient, att.ServerID, att.ID).ExtractErr()
391403
if err != nil {
392404
return nil, err
393405
}
@@ -396,15 +408,15 @@ func (d plugin) detachVolume(vol *volumes.Volume) (*volumes.Volume, error) {
396408
return vol, nil
397409
}
398410

399-
func (d plugin) waitOnVolumeState(vol *volumes.Volume, status string) (*volumes.Volume, error) {
411+
func (d plugin) waitOnVolumeState(ctx context.Context, vol *volumes.Volume, status string) (*volumes.Volume, error) {
400412
if vol.Status == status {
401413
return vol, nil
402414
}
403415

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

407-
vol, err := volumes.Get(context.Background(), d.blockClient, vol.ID).Extract()
419+
vol, err := volumes.Get(ctx, d.blockClient, vol.ID).Extract()
408420
if err != nil {
409421
return nil, err
410422
}

0 commit comments

Comments
 (0)