Skip to content

Commit 5b74ed2

Browse files
committed
Fix
1 parent 8b7b06b commit 5b74ed2

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

pkg/driver/controller_server.go

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -261,32 +261,37 @@ func (d *Driver) ControllerPublishVolume(ctx context.Context, req *csi.Controlle
261261
return &csi.ControllerPublishVolumeResponse{}, nil
262262
}
263263

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-
270264
// 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")
273272
return nil, status.Errorf(codes.Unavailable, "Volume is not available to be attached")
274273
}
275274

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")
286292
}
287-
log.Info().Str("volume_id", volume.ID).Str("instance_id", req.NodeId).Msg("Volume successfully requested to be attached in Civo API")
288293

289-
time.Sleep(10 * time.Second)
294+
time.Sleep(5 * time.Second)
290295
// refetch the volume
291296
log.Info().Str("volume_id", volume.ID).Msg("Fetching volume again to check status after attaching")
292297
volume, err = d.CivoClient.GetVolume(req.VolumeId)
@@ -343,22 +348,24 @@ func (d *Driver) ControllerUnpublishVolume(ctx context.Context, req *csi.Control
343348
return &csi.ControllerUnpublishVolumeResponse{}, nil
344349
}
345350

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")
354360

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+
}
360366

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+
}
362369

363370
// Fetch the new state after 5 seconds
364371
time.Sleep(5 * time.Second)

0 commit comments

Comments
 (0)