forked from shapeblue/cloudstack-csi-driver
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontroller.go
More file actions
564 lines (466 loc) · 18.1 KB
/
controller.go
File metadata and controls
564 lines (466 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package driver
import (
"context"
"errors"
"fmt"
"math/rand"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog/v2"
"github.com/cloudstack/cloudstack-csi-driver/pkg/cloud"
"github.com/cloudstack/cloudstack-csi-driver/pkg/util"
)
// onlyVolumeCapAccessMode is the only volume capability access
// mode possible for CloudStack: SINGLE_NODE_WRITER, since a
// CloudStack volume can only be attached to a single node at
// any given time.
var onlyVolumeCapAccessMode = csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
}
type controllerServer struct {
csi.UnimplementedControllerServer
// connector is the CloudStack client interface
connector cloud.Interface
// A map storing all volumes with ongoing operations so that additional operations
// for that same volume (as defined by VolumeID/volume name) return an Aborted error
volumeLocks *util.VolumeLocks
// A map storing all volumes/snapshots with ongoing operations.
operationLocks *util.OperationLock
}
// NewControllerServer creates a new Controller gRPC server.
func NewControllerServer(connector cloud.Interface) csi.ControllerServer {
return &controllerServer{
connector: connector,
volumeLocks: util.NewVolumeLocks(),
operationLocks: util.NewOperationLock(),
}
}
func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
logger := klog.FromContext(ctx)
logger.V(6).Info("CreateVolume: called", "args", *req)
// Check arguments.
if req.GetName() == "" {
return nil, status.Error(codes.InvalidArgument, "Volume name missing in request")
}
name := req.GetName()
volCaps := req.GetVolumeCapabilities()
if len(volCaps) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume capabilities missing in request")
}
if !isValidVolumeCapabilities(volCaps) {
return nil, status.Error(codes.InvalidArgument, "Volume capabilities not supported. Only SINGLE_NODE_WRITER supported.")
}
if req.GetParameters() == nil {
return nil, status.Error(codes.InvalidArgument, "Volume parameters missing in request")
}
diskOfferingID := req.GetParameters()[DiskOfferingKey]
if diskOfferingID == "" {
return nil, status.Errorf(codes.InvalidArgument, "Missing parameter %v", DiskOfferingKey)
}
if acquired := cs.volumeLocks.TryAcquire(name); !acquired {
logger.Error(errors.New(util.ErrVolumeOperationAlreadyExistsVolumeName), "failed to acquire volume lock", "volumeName", name)
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, name)
}
defer cs.volumeLocks.Release(name)
// Check if a volume with that name already exists.
vol, err := cs.connector.GetVolumeByName(ctx, name)
if err != nil {
if !errors.Is(err, cloud.ErrNotFound) {
// Error with CloudStack
return nil, status.Errorf(codes.Internal, "CloudStack error: %v", err)
}
} else {
// The volume exists. Check if it suits the request.
if ok, message := checkVolumeSuitable(vol, diskOfferingID, req.GetCapacityRange(), req.GetAccessibilityRequirements()); !ok {
return nil, status.Errorf(codes.AlreadyExists, "Volume %v already exists but does not satisfy request: %s", name, message)
}
// Existing volume is ok.
resp := &csi.CreateVolumeResponse{
Volume: &csi.Volume{
VolumeId: vol.ID,
CapacityBytes: vol.Size,
VolumeContext: req.GetParameters(),
// ContentSource: req.GetVolumeContentSource(), TODO: snapshot support.
AccessibleTopology: []*csi.Topology{
Topology{ZoneID: vol.ZoneID}.ToCSI(),
},
},
}
return resp, nil
}
// We have to create the volume.
// Determine volume size using requested capacity range.
sizeInGB, err := determineSize(req)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
// Determine zone using topology constraints.
var zoneID string
topologyRequirement := req.GetAccessibilityRequirements()
if topologyRequirement == nil || topologyRequirement.GetRequisite() == nil { //nolint:nestif
// No topology requirement. Use random zone.
zones, err := cs.connector.ListZonesID(ctx)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
n := len(zones)
if n == 0 {
return nil, status.Error(codes.Internal, "No zone available")
}
zoneID = zones[rand.Intn(n)] //nolint:gosec
} else {
reqTopology := topologyRequirement.GetRequisite()
if len(reqTopology) > 1 {
return nil, status.Error(codes.InvalidArgument, "Too many topology requirements")
}
t, err := NewTopology(reqTopology[0])
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Cannot parse topology requirements")
}
zoneID = t.ZoneID
}
logger.Info("Creating new volume",
"name", name,
"size", sizeInGB,
"offering", diskOfferingID,
"zone", zoneID,
)
volID, err := cs.connector.CreateVolume(ctx, diskOfferingID, zoneID, name, sizeInGB)
if err != nil {
return nil, status.Errorf(codes.Internal, "Cannot create volume %s: %v", name, err.Error())
}
resp := &csi.CreateVolumeResponse{
Volume: &csi.Volume{
VolumeId: volID,
CapacityBytes: util.GigaBytesToBytes(sizeInGB),
VolumeContext: req.GetParameters(),
// ContentSource: req.GetVolumeContentSource(), TODO: snapshot support.
AccessibleTopology: []*csi.Topology{
Topology{ZoneID: zoneID}.ToCSI(),
},
},
}
return resp, nil
}
func checkVolumeSuitable(vol *cloud.Volume,
diskOfferingID string, capRange *csi.CapacityRange, topologyRequirement *csi.TopologyRequirement,
) (bool, string) {
if vol.DiskOfferingID != diskOfferingID {
return false, fmt.Sprintf("Disk offering %s; requested disk offering %s", vol.DiskOfferingID, diskOfferingID)
}
if capRange != nil {
if capRange.GetLimitBytes() > 0 && vol.Size > capRange.GetLimitBytes() {
return false, fmt.Sprintf("Disk size %v bytes > requested limit size %v bytes", vol.Size, capRange.GetLimitBytes())
}
if capRange.GetRequiredBytes() > 0 && vol.Size < capRange.GetRequiredBytes() {
return false, fmt.Sprintf("Disk size %v bytes < requested required size %v bytes", vol.Size, capRange.GetRequiredBytes())
}
}
if topologyRequirement != nil && topologyRequirement.GetRequisite() != nil {
reqTopology := topologyRequirement.GetRequisite()
if len(reqTopology) > 1 {
return false, "Too many topology requirements"
}
t, err := NewTopology(reqTopology[0])
if err != nil {
return false, "Cannot parse topology requirements"
}
if t.ZoneID != vol.ZoneID {
return false, fmt.Sprintf("Volume in zone %s, requested zone is %s", vol.ZoneID, t.ZoneID)
}
}
return true, ""
}
func determineSize(req *csi.CreateVolumeRequest) (int64, error) {
var sizeInGB int64
if req.GetCapacityRange() != nil {
capRange := req.GetCapacityRange()
required := capRange.GetRequiredBytes()
sizeInGB = util.RoundUpBytesToGB(required)
if sizeInGB == 0 {
sizeInGB = 1
}
if limit := capRange.GetLimitBytes(); limit > 0 {
if util.GigaBytesToBytes(sizeInGB) > limit {
return 0, fmt.Errorf("after round-up, volume size %v GB exceeds the limit specified of %v bytes", sizeInGB, limit)
}
}
}
if sizeInGB == 0 {
sizeInGB = 1
}
return sizeInGB, nil
}
func (cs *controllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
logger := klog.FromContext(ctx)
logger.V(6).Info("DeleteVolume: called", "args", *req)
if req.GetVolumeId() == "" {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
volumeID := req.GetVolumeId()
if acquired := cs.volumeLocks.TryAcquire(volumeID); !acquired {
logger.Error(errors.New(util.ErrVolumeOperationAlreadyExistsVolumeID), "failed to acquire volume lock", "volumeID", volumeID)
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, volumeID)
}
defer cs.volumeLocks.Release(volumeID)
// lock out volumeID for clone and expand operation
if err := cs.operationLocks.GetDeleteLock(volumeID); err != nil {
logger.Error(err, "Failed to acquire delete operation lock")
return nil, status.Error(codes.Aborted, err.Error())
}
defer cs.operationLocks.ReleaseDeleteLock(volumeID)
logger.Info("Deleting volume",
"volumeID", volumeID,
)
err := cs.connector.DeleteVolume(ctx, volumeID)
if err != nil && !errors.Is(err, cloud.ErrNotFound) {
return nil, status.Errorf(codes.Internal, "Cannot delete volume %s: %s", volumeID, err.Error())
}
return &csi.DeleteVolumeResponse{}, nil
}
func (cs *controllerServer) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
logger := klog.FromContext(ctx)
logger.V(6).Info("ControllerPublishVolume: called", "args", *req)
// Check arguments.
if req.GetVolumeId() == "" {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
volumeID := req.GetVolumeId()
if req.GetNodeId() == "" {
return nil, status.Error(codes.InvalidArgument, "Node ID missing in request")
}
nodeID := req.GetNodeId()
if req.GetReadonly() {
return nil, status.Error(codes.InvalidArgument, "Readonly not possible")
}
if req.GetVolumeCapability() == nil {
return nil, status.Error(codes.InvalidArgument, "Volume capability missing in request")
}
if req.GetVolumeCapability().GetAccessMode().GetMode() != onlyVolumeCapAccessMode.GetMode() {
return nil, status.Error(codes.InvalidArgument, "Access mode not accepted")
}
logger.Info("Initiating attaching volume",
"volumeID", volumeID,
"nodeID", nodeID,
)
// Check volume.
vol, err := cs.connector.GetVolumeByID(ctx, volumeID)
if errors.Is(err, cloud.ErrNotFound) {
return nil, status.Errorf(codes.NotFound, "Volume %v not found", volumeID)
} else if err != nil {
// Error with CloudStack
return nil, status.Errorf(codes.Internal, "Error %v", err)
}
if vol.VirtualMachineID != "" && vol.VirtualMachineID != nodeID {
logger.Error(nil, "Volume already attached to another node",
"volumeID", volumeID,
"nodeID", nodeID,
"attachedNodeID", vol.VirtualMachineID,
)
return nil, status.Error(codes.AlreadyExists, "Volume already assigned to another node")
}
if _, err := cs.connector.GetVMByID(ctx, nodeID); errors.Is(err, cloud.ErrNotFound) {
return nil, status.Errorf(codes.NotFound, "VM %v not found", nodeID)
} else if err != nil {
// Error with CloudStack
return nil, status.Errorf(codes.Internal, "Error %v", err)
}
if vol.VirtualMachineID == nodeID {
// volume already attached.
logger.Info("Volume already attached to node",
"volumeID", volumeID,
"nodeID", nodeID,
"deviceID", vol.DeviceID,
)
publishContext := map[string]string{
deviceIDContextKey: vol.DeviceID,
}
return &csi.ControllerPublishVolumeResponse{PublishContext: publishContext}, nil
}
logger.Info("Attaching volume to node",
"volumeID", volumeID,
"nodeID", nodeID,
)
deviceID, err := cs.connector.AttachVolume(ctx, volumeID, nodeID)
if err != nil {
return nil, status.Errorf(codes.Internal, "Cannot attach volume %s: %s", volumeID, err.Error())
}
logger.Info("Attached volume to node successfully",
"volumeID", volumeID,
"nodeID", nodeID,
)
publishContext := map[string]string{
deviceIDContextKey: deviceID,
}
return &csi.ControllerPublishVolumeResponse{PublishContext: publishContext}, nil
}
func (cs *controllerServer) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
logger := klog.FromContext(ctx)
logger.V(6).Info("ControllerUnpublishVolume: called", "args", *req)
// Check arguments.
if req.GetVolumeId() == "" {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
volumeID := req.GetVolumeId()
nodeID := req.GetNodeId()
// Check volume.
if vol, err := cs.connector.GetVolumeByID(ctx, volumeID); errors.Is(err, cloud.ErrNotFound) {
// Volume does not exist in CloudStack. We can safely assume this volume is no longer attached
// The spec requires us to return OK here.
return &csi.ControllerUnpublishVolumeResponse{}, nil
} else if err != nil {
// Error with CloudStack
return nil, status.Errorf(codes.Internal, "Error %v", err)
} else if nodeID != "" && vol.VirtualMachineID != nodeID {
// Volume is present but not attached to this particular nodeID
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
// Check VM existence.
if _, err := cs.connector.GetVMByID(ctx, nodeID); errors.Is(err, cloud.ErrNotFound) {
// volumes cannot be attached to deleted VMs.
logger.Error(nil, "VM not found, marking ControllerUnpublishVolume successful",
"volumeID", volumeID,
"nodeID", nodeID,
)
return &csi.ControllerUnpublishVolumeResponse{}, nil
} else if err != nil {
// Error with CloudStack
return nil, status.Errorf(codes.Internal, "Error %v", err)
}
logger.Info("Detaching volume from node",
"volumeID", volumeID,
"nodeID", nodeID,
)
err := cs.connector.DetachVolume(ctx, volumeID)
if err != nil {
return nil, status.Errorf(codes.Internal, "Cannot detach volume %s: %s", volumeID, err.Error())
}
logger.Info("Detached volume from node successfully",
"volumeID", volumeID,
"nodeID", nodeID,
)
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
func (cs *controllerServer) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
logger := klog.FromContext(ctx)
logger.V(6).Info("ValidateVolumeCapabilities: called", "args", *req)
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
}
volCaps := req.GetVolumeCapabilities()
if len(volCaps) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume capabilities not provided")
}
if _, err := cs.connector.GetVolumeByID(ctx, volumeID); errors.Is(err, cloud.ErrNotFound) {
return nil, status.Errorf(codes.NotFound, "Volume %v not found", volumeID)
} else if err != nil {
// Error with CloudStack
return nil, status.Errorf(codes.Internal, "Error %v", err)
}
if !isValidVolumeCapabilities(volCaps) {
return &csi.ValidateVolumeCapabilitiesResponse{Message: "Requested VolumeCapabilities are invalid"}, nil
}
return &csi.ValidateVolumeCapabilitiesResponse{
Confirmed: &csi.ValidateVolumeCapabilitiesResponse_Confirmed{
VolumeContext: req.GetVolumeContext(),
VolumeCapabilities: volCaps,
Parameters: req.GetParameters(),
},
}, nil
}
func isValidVolumeCapabilities(volCaps []*csi.VolumeCapability) bool {
for _, c := range volCaps {
if c.GetAccessMode() != nil && c.GetAccessMode().GetMode() != onlyVolumeCapAccessMode.GetMode() {
return false
}
}
return true
}
func (cs *controllerServer) ControllerExpandVolume(ctx context.Context, req *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) {
logger := klog.FromContext(ctx)
logger.V(6).Info("ControllerExpandVolume: called", "args", protosanitizer.StripSecrets(*req))
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
}
capRange := req.GetCapacityRange()
if capRange == nil {
return nil, status.Error(codes.InvalidArgument, "Capacity range not provided")
}
// lock out parallel requests against the same volume ID
if acquired := cs.volumeLocks.TryAcquire(volumeID); !acquired {
logger.Error(errors.New(util.ErrVolumeOperationAlreadyExistsVolumeID), "failed to acquire volume lock", "volumeID", volumeID)
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, volumeID)
}
defer cs.volumeLocks.Release(volumeID)
volSizeBytes := capRange.GetRequiredBytes()
volSizeGB := util.RoundUpBytesToGB(volSizeBytes)
maxVolSize := capRange.GetLimitBytes()
if maxVolSize > 0 && maxVolSize < util.GigaBytesToBytes(volSizeGB) {
return nil, status.Error(codes.OutOfRange, "Volume size exceeds the limit specified")
}
_, err := cs.connector.GetVolumeByID(ctx, volumeID)
if err != nil {
if errors.Is(err, cloud.ErrNotFound) {
return nil, status.Errorf(codes.NotFound, "Volume %v not found", volumeID)
}
return nil, status.Error(codes.Internal, fmt.Sprintf("GetVolume failed with error %v", err))
}
// lock out volumeID for clone and delete operation
if err := cs.operationLocks.GetExpandLock(volumeID); err != nil {
logger.Error(err, "failed acquiring expand lock", "volumeID", volumeID)
return nil, status.Error(codes.Aborted, err.Error())
}
defer cs.operationLocks.ReleaseExpandLock(volumeID)
err = cs.connector.ExpandVolume(ctx, volumeID, volSizeGB)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not resize volume %q to size %v: %v", volumeID, volSizeGB, err)
}
logger.Info("Volume successfully expanded",
"volumeID", volumeID,
"volumeSize", volSizeGB,
)
nodeExpansionRequired := true
// Node expansion is not required for raw block volumes.
volCap := req.GetVolumeCapability()
if volCap != nil && volCap.GetBlock() != nil {
nodeExpansionRequired = false
}
return &csi.ControllerExpandVolumeResponse{
CapacityBytes: util.GigaBytesToBytes(volSizeGB),
NodeExpansionRequired: nodeExpansionRequired,
}, nil
}
func (cs *controllerServer) ControllerGetCapabilities(ctx context.Context, req *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) {
logger := klog.FromContext(ctx)
logger.V(6).Info("ControllerGetCapabilities: called", "args", protosanitizer.StripSecrets(*req))
resp := &csi.ControllerGetCapabilitiesResponse{
Capabilities: []*csi.ControllerServiceCapability{
{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
},
},
},
{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME,
},
},
},
{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: csi.ControllerServiceCapability_RPC_EXPAND_VOLUME,
},
},
},
},
}
return resp, nil
}