Skip to content

Commit c691c11

Browse files
committed
check vol status before trying to attach; abort early if status is not happy
Signed-off-by: Niclas Schad <niclas.schad@stackit.cloud>
1 parent 8c7bae7 commit c691c11

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

pkg/csi/blockstorage/controllerserver.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func (cs *controllerServer) ControllerPublishVolume(ctx context.Context, req *cs
356356
return nil, status.Error(codes.InvalidArgument, "[ControllerPublishVolume] Volume capability must be provided")
357357
}
358358

359-
_, err := cloud.GetVolume(ctx, volumeID)
359+
vol, err := cloud.GetVolume(ctx, volumeID)
360360
if err != nil {
361361
if stackiterrors.IsNotFound(err) {
362362
return nil, status.Errorf(codes.NotFound, "[ControllerPublishVolume] Volume %s not found", volumeID)
@@ -372,6 +372,15 @@ func (cs *controllerServer) ControllerPublishVolume(ctx context.Context, req *cs
372372
return nil, status.Errorf(codes.Internal, "[ControllerPublishVolume] GetInstanceByID failed with error %v", err)
373373
}
374374

375+
// If Volume is already mounted to target instanceID, return OK
376+
if vol.ServerId != nil && *vol.ServerId == instanceID {
377+
return &csi.ControllerPublishVolumeResponse{}, nil
378+
}
379+
380+
if vol.GetStatus() != stackitclient.VolumeAvailableStatus {
381+
return nil, status.Errorf(codes.Internal, "[ControllerPublishVolume] Volume %s is not in an READY state. Got:%s Want:%s", volumeID, vol.GetStatus(), stackitclient.VolumeAvailableStatus)
382+
}
383+
375384
payload := iaas.AddVolumeToServerPayload{
376385
DeleteOnTermination: new(false),
377386
}

pkg/csi/blockstorage/controllerserver_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ var _ = Describe("ControllerServer test", Ordered, func() {
660660
NodeId: "fake",
661661
VolumeCapability: stdVolCap,
662662
}
663-
iaasClient.EXPECT().GetVolume(gomock.Any(), req.VolumeId).Return(&iaas.Volume{}, nil)
663+
iaasClient.EXPECT().GetVolume(gomock.Any(), req.VolumeId).Return(&iaas.Volume{Status: new("AVAILABLE")}, nil)
664664
iaasClient.EXPECT().GetServer(gomock.Any(), "fake").Return(&iaas.Server{}, nil)
665665
iaasClient.EXPECT().AttachVolume(gomock.Any(), req.NodeId, req.VolumeId, gomock.Any()).Return(req.VolumeId, nil)
666666
iaasClient.EXPECT().WaitDiskAttached(gomock.Any(), req.NodeId, req.VolumeId).Return(nil)
@@ -674,7 +674,7 @@ var _ = Describe("ControllerServer test", Ordered, func() {
674674
NodeId: "fake",
675675
VolumeCapability: stdVolCap,
676676
}
677-
iaasClient.EXPECT().GetVolume(gomock.Any(), req.VolumeId).Return(&iaas.Volume{}, nil)
677+
iaasClient.EXPECT().GetVolume(gomock.Any(), req.VolumeId).Return(&iaas.Volume{Status: new("AVAILABLE")}, nil)
678678
iaasClient.EXPECT().GetServer(gomock.Any(), req.NodeId).Return(&iaas.Server{}, nil)
679679
iaasClient.EXPECT().AttachVolume(gomock.Any(), req.NodeId, req.VolumeId, gomock.Any()).Return("", &oapierror.GenericOpenAPIError{
680680
StatusCode: http.StatusForbidden,

pkg/csi/blockstorage/sanity_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ var _ = Describe("CSI sanity test", Ordered, func() {
263263
newBackup := &iaas.Backup{
264264
Id: new(uuid.New().String()),
265265
Name: new(name),
266-
Status: new("available"),
266+
Status: new("AVAILABLE"),
267267
VolumeId: new(volID),
268268
SnapshotId: new(snapshotID),
269269
CreatedAt: new(time.Now()),
@@ -329,7 +329,7 @@ var _ = Describe("CSI sanity test", Ordered, func() {
329329
return "", &oapierror.GenericOpenAPIError{StatusCode: http.StatusNotFound}
330330
}
331331
vol.ServerId = new(instanceID)
332-
vol.Status = new("attached")
332+
vol.Status = new("ATTACHED")
333333
return *vol.Id, nil
334334
}).AnyTimes()
335335

@@ -343,7 +343,15 @@ var _ = Describe("CSI sanity test", Ordered, func() {
343343
gomock.Any(), // context
344344
gomock.Any(), // instanceID
345345
gomock.Any(), // volumeID
346-
).Return(nil).AnyTimes()
346+
).DoAndReturn(func(_ context.Context, _, volumeID string) error {
347+
vol, ok := createdVolumes[volumeID]
348+
if !ok {
349+
return &oapierror.GenericOpenAPIError{StatusCode: http.StatusNotFound}
350+
}
351+
vol.ServerId = nil
352+
vol.Status = new("AVAILABLE")
353+
return nil
354+
}).AnyTimes()
347355

348356
iaasClient.EXPECT().WaitDiskDetached(
349357
gomock.Any(), // context

0 commit comments

Comments
 (0)