Skip to content

Commit d925267

Browse files
committed
Allow nodeID to be empty as defined in CSI spec
1 parent 3113112 commit d925267

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

pkg/cloud/cloud.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Interface interface {
2121
CreateVolume(ctx context.Context, diskOfferingID, zoneID, name string, sizeInGB int64) (string, error)
2222
DeleteVolume(ctx context.Context, id string) error
2323
AttachVolume(ctx context.Context, volumeID, vmID string) (string, error)
24-
DetachVolume(ctx context.Context, volumeID string) error
24+
DetachVolume(ctx context.Context, volumeID string, vmID string) error
2525
}
2626

2727
// Volume represents a CloudStack volume.

pkg/cloud/fake/fake.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,6 @@ func (f *fakeConnector) AttachVolume(ctx context.Context, volumeID, vmID string)
100100
return "1", nil
101101
}
102102

103-
func (f *fakeConnector) DetachVolume(ctx context.Context, volumeID string) error { return nil }
103+
func (f *fakeConnector) DetachVolume(ctx context.Context, volumeID string, vmID string) error {
104+
return nil
105+
}

pkg/cloud/volumes.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,15 @@ func (c *client) AttachVolume(ctx context.Context, volumeID, vmID string) (strin
111111
return strconv.FormatInt(r.Deviceid, 10), nil
112112
}
113113

114-
func (c *client) DetachVolume(ctx context.Context, volumeID string) error {
114+
func (c *client) DetachVolume(ctx context.Context, volumeID string, vmID string) error {
115115
p := c.Volume.NewDetachVolumeParams()
116116
p.SetId(volumeID)
117+
if vmID != "" {
118+
p.SetVirtualmachineid(vmID)
119+
}
117120
ctxzap.Extract(ctx).Sugar().Infow("CloudStack API call", "command", "DetachVolume", "params", map[string]string{
118-
"id": volumeID,
121+
"id": volumeID,
122+
"virtualmachineid": vmID,
119123
})
120124
_, err := c.Volume.DetachVolume(p)
121125
return err

pkg/driver/controller.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func (cs *controllerServer) ControllerPublishVolume(ctx context.Context, req *cs
252252
}
253253

254254
if _, err := cs.connector.GetVMByID(ctx, nodeID); err == cloud.ErrNotFound {
255-
return nil, status.Errorf(codes.NotFound, "VM %v not found", volumeID)
255+
return nil, status.Errorf(codes.NotFound, "VM %v not found", nodeID)
256256
} else if err != nil {
257257
// Error with CloudStack
258258
return nil, status.Errorf(codes.Internal, "Error %v", err)
@@ -285,15 +285,6 @@ func (cs *controllerServer) ControllerUnpublishVolume(ctx context.Context, req *
285285
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
286286
}
287287
volumeID := req.GetVolumeId()
288-
289-
// TODO: according to the spec, node_id is allowed to be empty:
290-
//
291-
// If the value is set, the SP MUST unpublish the volume from
292-
// the specified node. If the value is unset, the SP MUST unpublish
293-
// the volume from all nodes it is published to.
294-
if req.GetNodeId() == "" {
295-
return nil, status.Error(codes.InvalidArgument, "Node ID missing in request")
296-
}
297288
nodeID := req.GetNodeId()
298289

299290
// Check volume
@@ -304,8 +295,8 @@ func (cs *controllerServer) ControllerUnpublishVolume(ctx context.Context, req *
304295
} else if err != nil {
305296
// Error with CloudStack
306297
return nil, status.Errorf(codes.Internal, "Error %v", err)
307-
} else if vol.VirtualMachineID != nodeID {
308-
// Nothing to do
298+
} else if nodeID != "" && vol.VirtualMachineID != nodeID {
299+
// Volume is present but not attached to this particular nodeID
309300
return &csi.ControllerUnpublishVolumeResponse{}, nil
310301
}
311302

@@ -317,7 +308,7 @@ func (cs *controllerServer) ControllerUnpublishVolume(ctx context.Context, req *
317308
return nil, status.Errorf(codes.Internal, "Error %v", err)
318309
}
319310

320-
err := cs.connector.DetachVolume(ctx, volumeID)
311+
err := cs.connector.DetachVolume(ctx, volumeID, nodeID)
321312
if err != nil {
322313
return nil, status.Errorf(codes.Internal, "Cannot detach volume %s: %s", volumeID, err.Error())
323314
}

0 commit comments

Comments
 (0)