@@ -261,32 +261,37 @@ func (d *Driver) ControllerPublishVolume(ctx context.Context, req *csi.Controlle
261
261
return & csi.ControllerPublishVolumeResponse {}, nil
262
262
}
263
263
264
- // Check if the volume is attaching to this node
265
- if volume .InstanceID == req .NodeId && volume .Status == "attaching" {
266
- log .Info ().Str ("volume_id" , volume .ID ).Str ("instance_id" , req .NodeId ).Msg ("Volume is already attaching to the requested instance" )
267
- return nil , status .Errorf (codes .Unavailable , "Volume is already attaching to the requested instance" )
268
- }
269
-
270
264
// if the volume is not available, we can't attach it, so error out
271
- if volume .Status != "available" {
272
- log .Error ().Str ("volume_id" , volume .ID ).Str ("status" , volume .Status ).Msg ("Volume is not available to be attached" )
265
+ if volume .Status != "available" && volume .InstanceID != req .NodeId {
266
+ log .Error ().
267
+ Str ("volume_id" , volume .ID ).
268
+ Str ("status" , volume .Status ).
269
+ Str ("requested_instance_id" , req .NodeId ).
270
+ Str ("current_instance_id" , volume .InstanceID ).
271
+ Msg ("Volume is not available to be attached" )
273
272
return nil , status .Errorf (codes .Unavailable , "Volume is not available to be attached" )
274
273
}
275
274
276
- // Call the CivoAPI to attach it to a node/instance
277
- log .Debug ().
278
- Str ("volume_id" , volume .ID ).
279
- Str ("volume_status" , volume .Status ).
280
- Str ("reqested_instance_id" , req .NodeId ).
281
- Msg ("Requesting volume to be attached in Civo API" )
282
- _ , err = d .CivoClient .AttachVolume (req .VolumeId , req .NodeId )
283
- if err != nil {
284
- log .Error ().Err (err ).Msg ("Unable to attach volume in Civo API" )
285
- return nil , err
275
+ // Check if the volume is attaching to this node
276
+ if volume .InstanceID == req .NodeId && volume .Status != "attaching" {
277
+ // Do nothing, the volume is already attaching
278
+ log .Debug ().Str ("volume_id" , volume .ID ).Str ("status" , volume .Status ).Msg ("Volume is already attaching" )
279
+ } else {
280
+ // Call the CivoAPI to attach it to a node/instance
281
+ log .Debug ().
282
+ Str ("volume_id" , volume .ID ).
283
+ Str ("volume_status" , volume .Status ).
284
+ Str ("reqested_instance_id" , req .NodeId ).
285
+ Msg ("Requesting volume to be attached in Civo API" )
286
+ _ , err = d .CivoClient .AttachVolume (req .VolumeId , req .NodeId )
287
+ if err != nil {
288
+ log .Error ().Err (err ).Msg ("Unable to attach volume in Civo API" )
289
+ return nil , err
290
+ }
291
+ log .Info ().Str ("volume_id" , volume .ID ).Str ("instance_id" , req .NodeId ).Msg ("Volume successfully requested to be attached in Civo API" )
286
292
}
287
- log .Info ().Str ("volume_id" , volume .ID ).Str ("instance_id" , req .NodeId ).Msg ("Volume successfully requested to be attached in Civo API" )
288
293
289
- time .Sleep (10 * time .Second )
294
+ time .Sleep (5 * time .Second )
290
295
// refetch the volume
291
296
log .Info ().Str ("volume_id" , volume .ID ).Msg ("Fetching volume again to check status after attaching" )
292
297
volume , err = d .CivoClient .GetVolume (req .VolumeId )
@@ -343,22 +348,24 @@ func (d *Driver) ControllerUnpublishVolume(ctx context.Context, req *csi.Control
343
348
return & csi.ControllerUnpublishVolumeResponse {}, nil
344
349
}
345
350
346
- // The volume is either attached to the requested node or the requested node is empty
347
- // and the volume is attached, so we need to detach the volume
348
- log .Info ().
349
- Str ("volume_id" , volume .ID ).
350
- Str ("current_instance_id" , volume .InstanceID ).
351
- Str ("requested_instance_id" , req .NodeId ).
352
- Str ("status" , volume .Status ).
353
- Msg ("Requesting volume to be detached" )
351
+ if volume .Status != "detaching" {
352
+ // The volume is either attached to the requested node or the requested node is empty
353
+ // and the volume is attached, so we need to detach the volume
354
+ log .Info ().
355
+ Str ("volume_id" , volume .ID ).
356
+ Str ("current_instance_id" , volume .InstanceID ).
357
+ Str ("requested_instance_id" , req .NodeId ).
358
+ Str ("status" , volume .Status ).
359
+ Msg ("Requesting volume to be detached" )
354
360
355
- _ , err = d .CivoClient .DetachVolume (req .VolumeId )
356
- if err != nil {
357
- log .Error ().Err (err ).Msg ("Unable to detach volume in Civo API" )
358
- return nil , err
359
- }
361
+ _ , err = d .CivoClient .DetachVolume (req .VolumeId )
362
+ if err != nil {
363
+ log .Error ().Err (err ).Msg ("Unable to detach volume in Civo API" )
364
+ return nil , err
365
+ }
360
366
361
- log .Info ().Str ("volume_id" , volume .ID ).Msg ("Volume sucessfully requested to be detached in Civo API" )
367
+ log .Info ().Str ("volume_id" , volume .ID ).Msg ("Volume sucessfully requested to be detached in Civo API" )
368
+ }
362
369
363
370
// Fetch the new state after 5 seconds
364
371
time .Sleep (5 * time .Second )
0 commit comments