-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcontroller.go
637 lines (521 loc) · 21 KB
/
controller.go
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
Copyright 2020 Vultr Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package driver
import (
"context"
"fmt"
"strings"
"time"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/sirupsen/logrus"
"github.com/vultr/vultr-csi/internal/vultrstorage"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
gibiByte int64 = 1073741824
volumeStatusCheckRetries int = 15
volumeStatusCheckInterval int = 1
)
var _ csi.ControllerServer = &VultrControllerServer{}
// VultrControllerServer is the struct type for the VultrDriver
type VultrControllerServer struct {
csi.UnimplementedControllerServer
Driver *VultrDriver
}
// NewVultrControllerServer returns a VultrControllerServer
func NewVultrControllerServer(driver *VultrDriver) *VultrControllerServer {
return &VultrControllerServer{Driver: driver}
}
// CreateVolume provisions a new volume on behalf of the user
func (c *VultrControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) { //nolint:gocyclo,lll,funlen
if req.Name == "" {
return nil, status.Error(codes.InvalidArgument, "CreateVolume: name is missing")
}
if len(req.VolumeCapabilities) == 0 {
return nil, status.Error(codes.InvalidArgument, "CreateVolume: capabilities is missing")
}
diskType := strings.ToLower(req.Parameters["disk_type"])
storageType := strings.ToLower(req.Parameters["storage_type"])
blockType := strings.ToLower(req.Parameters["block_type"])
// handle legacy param
if blockType != "" {
storageType = "block"
if blockType == "high_perf" {
diskType = "nvme"
} else if blockType == "storage_opt" {
diskType = "hdd"
}
}
if diskType == "" {
return nil, status.Error(codes.InvalidArgument, "CreateVolume: parameter `disk_type` is missing")
}
if storageType == "" {
return nil, status.Error(codes.InvalidArgument, "CreateVolume: parameter `storage_type` is missing")
}
sh, err := vultrstorage.NewVultrStorageHandler(c.Driver.client, storageType, diskType, false)
if err != nil {
return nil, status.Errorf(codes.Internal, "CreateVolume: cannot initialize vultr storage handler: %v", err.Error())
}
if err := validateCapabilities(req.VolumeCapabilities, sh.Capabilities); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "CreateVolume: requested capability is not compatible: %v", err)
}
c.Driver.log.WithFields(logrus.Fields{
"volume-name": req.Name,
"capabilities": req.VolumeCapabilities,
}).Info("CreateVolume: called")
var curVolume *vultrstorage.VultrStorage
storages, err := vultrstorage.ListAllStorages(ctx, c.Driver.client)
if err != nil {
return nil, status.Errorf(codes.Internal, "CreateVolume: could not retrieve list of storages. %v", err.Error())
}
// check if volume already exists
for i := range storages {
if storages[i].Label == req.Name {
curVolume = &storages[i]
break
}
}
if curVolume != nil {
return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
VolumeId: curVolume.ID,
CapacityBytes: int64(curVolume.SizeGB) * gibiByte,
},
}, nil
}
// volume doesn't exist, create
size, err := getStorageBytes(req.CapacityRange, sh)
if err != nil {
return nil, status.Errorf(codes.Internal, "CreateVolume: could not request new volume: %v", err.Error())
}
storageReq := &vultrstorage.VultrStorageReq{
Region: c.Driver.region,
SizeGB: int(size / gibiByte),
Label: req.Name,
DiskType: diskType,
}
volume, err := sh.Operations.Create(ctx, *storageReq)
if err != nil {
return nil, status.Errorf(codes.Internal, "CreateVolume: could not create a new volume: %v", err.Error())
}
// Check to see if volume is in active state
volReady := false
for i := 0; i < volumeStatusCheckRetries; i++ {
time.Sleep(time.Duration(volumeStatusCheckInterval) * time.Second)
storage, err := sh.Operations.Get(ctx, volume.ID)
if err != nil {
return nil, status.Errorf(codes.Internal, "CreateVolume: could not retrieve the new volume: %v", err.Error())
}
if storage.Status == "active" {
volReady = true
break
}
}
if !volReady {
return nil, status.Errorf(codes.Internal, "CreateVolume: volume is not active after %v seconds", volumeStatusCheckRetries)
}
res := &csi.CreateVolumeResponse{
Volume: &csi.Volume{
VolumeId: volume.ID,
CapacityBytes: size,
AccessibleTopology: []*csi.Topology{
{
Segments: map[string]string{
"region": c.Driver.region,
},
},
},
},
}
c.Driver.log.WithFields(logrus.Fields{
"size": size,
"volume-id": volume.ID,
"volume-name": volume.Label,
"volume-size": volume.SizeGB,
}).Info("CreateVolume: created volume")
return res, nil
}
// DeleteVolume performs the volume deletion
func (c *VultrControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
if req.VolumeId == "" {
return nil, status.Error(codes.InvalidArgument, "DeleteVolume: volume ID is missing")
}
c.Driver.log.WithFields(logrus.Fields{
"volume-id": req.VolumeId,
}).Info("DeleteVolume: called")
exists := false
var deleteStorage vultrstorage.VultrStorage
storages, err := vultrstorage.ListAllStorages(ctx, c.Driver.client)
if err != nil {
return nil, status.Errorf(codes.Internal, "DeleteVolume: could not retrieve list of storages. %v", err.Error())
}
for i := range storages {
if storages[i].ID == req.VolumeId {
exists = true
deleteStorage = storages[i]
break
}
}
if !exists {
return &csi.DeleteVolumeResponse{}, nil
}
sh, err := vultrstorage.NewVultrStorageHandler(c.Driver.client, deleteStorage.StorageType, "", true)
if err != nil {
return nil, fmt.Errorf("DeleteVolume: cannot initialize vultr storage handler. %v", err)
}
// detach all instances
for i := range deleteStorage.AttachedInstances {
if err := sh.Operations.Detach(ctx, deleteStorage.ID, deleteStorage.AttachedInstances[i].NodeID); err != nil {
if !strings.Contains(err.Error(), "volume is not currently attached") ||
!strings.Contains(err.Error(), "Attachment Not Found") {
return nil, status.Errorf(codes.Internal, "DeleteVolume: cannot detach volume in delete, %v", err.Error())
}
}
}
// otherwise, internal brokenness
if err := sh.Operations.Delete(ctx, deleteStorage.ID); err != nil {
return nil, status.Errorf(codes.Internal, "DeleteVolume: cannot delete volume, %v", err.Error())
}
c.Driver.log.WithFields(logrus.Fields{
"volume-id": req.VolumeId,
}).Info("DeleteVolume: deleted")
return &csi.DeleteVolumeResponse{}, nil
}
// ControllerPublishVolume performs the volume publish for the controller
func (c *VultrControllerServer) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) { //nolint:lll,gocyclo
if req.VolumeId == "" {
return nil, status.Error(codes.InvalidArgument, "ControllerPublishVolume: volume ID is missing")
}
if req.NodeId == "" {
return nil, status.Error(codes.InvalidArgument, "ControllerPublishVolume: node ID is missing")
}
if req.VolumeCapability == nil {
return nil, status.Error(codes.InvalidArgument, "ControllerPublishVolume: volume capability is missing")
}
if req.Readonly {
return nil, status.Error(codes.InvalidArgument, "ControllerPublishVolume: read only is not currently supported")
}
sh, err := vultrstorage.FindVultrStorageHandlerByID(ctx, c.Driver.client, req.VolumeId)
if err != nil {
return nil, status.Errorf(codes.Internal, "ControllerPublishVolume: could not find storage handler for storage. %v", err.Error())
}
storageExisting, err := sh.Operations.Get(ctx, req.VolumeId)
if err != nil {
return nil, status.Errorf(codes.NotFound, "ControllerPublishVolume: could not retrieve existing storage volume: %v", err.Error())
}
if _, _, err = c.Driver.client.Instance.Get(ctx, req.NodeId); err != nil { //nolint:bodyclose
return nil, status.Errorf(codes.NotFound, "ControllerPublishVolume: could not retrieve node: %v", err.Error())
}
for i := range storageExisting.AttachedInstances {
if storageExisting.AttachedInstances[i].NodeID == req.NodeId {
return &csi.ControllerPublishVolumeResponse{
PublishContext: map[string]string{
"mount_vol_name": storageExisting.AttachedInstances[i].MountName,
"storage_type": storageExisting.StorageType,
},
}, nil
}
}
// block storage cannot be mounted to more than one instance
if storageExisting.StorageType == "block" && len(storageExisting.AttachedInstances) > 0 {
return nil, status.Errorf(codes.FailedPrecondition,
"ControllerPublishVolume: cannot attach volume to node because it is already attached to a different node ID: %v",
storageExisting.AttachedInstances[0].NodeID)
}
c.Driver.log.WithFields(logrus.Fields{
"volume-id": req.VolumeId,
"node-id": req.NodeId,
}).Info("ControllerPublishVolume: called")
err = sh.Operations.Attach(ctx, req.VolumeId, req.NodeId)
if err != nil {
if strings.Contains(err.Error(), "Server is currently locked") {
return nil, status.Errorf(codes.Aborted, "cannot attach volume to node: %v", err.Error())
}
return nil, status.Errorf(codes.Internal, "ControllPublishVolume: cannot attach volume to node: %v", err.Error())
}
attachReady := false
var storageAttached *vultrstorage.VultrStorage
publishedVolName := ""
retries:
for i := 0; i < volumeStatusCheckRetries; i++ {
time.Sleep(time.Duration(volumeStatusCheckInterval) * time.Second)
storageAttached, err = sh.Operations.Get(ctx, storageExisting.ID)
if err != nil {
return nil, status.Errorf(
codes.Internal,
"ControllerPublishVolume: unable to retrieve storage for retry check: %v",
err.Error(),
)
}
for i := range storageAttached.AttachedInstances {
if storageAttached.AttachedInstances[i].NodeID == req.NodeId {
attachReady = true
publishedVolName = storageAttached.AttachedInstances[i].MountName
break retries
}
}
}
if !attachReady {
return nil, status.Errorf(
codes.Internal,
"ControllerPublishVolume: volume is not attached to node after %v seconds",
volumeStatusCheckRetries,
)
}
c.Driver.log.WithFields(logrus.Fields{
"volume-id": req.VolumeId,
"node-id": req.NodeId,
}).Info("ControllerPublishVolume: published")
return &csi.ControllerPublishVolumeResponse{
PublishContext: map[string]string{
"mount_vol_name": publishedVolName,
"storage_type": storageAttached.StorageType,
},
}, nil
}
// ControllerUnpublishVolume performs the volume un-publish
func (c *VultrControllerServer) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) { //nolint:lll
if req.VolumeId == "" {
return nil, status.Error(codes.InvalidArgument, "ControllerUnpublishVolume: volume ID is missing")
}
if req.NodeId == "" {
return nil, status.Error(codes.InvalidArgument, "ControllerUnpublishVolume: node ID is missing")
}
c.Driver.log.WithFields(logrus.Fields{
"volume-id": req.VolumeId,
"node-id": req.NodeId,
}).Info("ControllerPublishUnpublish: called")
sh, err := vultrstorage.FindVultrStorageHandlerByID(ctx, c.Driver.client, req.VolumeId)
if err != nil {
return nil, status.Errorf(codes.Internal, "ControllerUnpublishVolume: could not find storage handler for storage. %v", err.Error())
}
storage, err := sh.Operations.Get(ctx, req.VolumeId)
if err != nil {
// Not found, return empty response
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
// node is already unattached, do nothing
if len(storage.AttachedInstances) == 0 {
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
for i := range storage.AttachedInstances {
if storage.AttachedInstances[i].NodeID != req.NodeId {
continue
}
if err := sh.Operations.Detach(ctx, req.VolumeId, storage.AttachedInstances[i].NodeID); err != nil {
if strings.Contains(err.Error(), "Block storage volume is not currently attached to a server") ||
strings.Contains(err.Error(), "Attachment Not Found") {
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
return nil, status.Errorf(codes.Internal, "ControllerUnpublishVolume: could not detach volume: %v", err.Error())
}
}
c.Driver.log.WithFields(logrus.Fields{
"volume-id": req.VolumeId,
"node-id": req.NodeId,
}).Info("ControllerUnublishVolume: unpublished")
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
// ControllerModifyVolume is unimplemented
func (c *VultrControllerServer) ControllerModifyVolume(ctx context.Context, req *csi.ControllerModifyVolumeRequest) (*csi.ControllerModifyVolumeResponse, error) { //nolint:lll
return nil, status.Errorf(codes.Unimplemented, "method ControllerModifyVolume not implemented")
}
// ValidateVolumeCapabilities checks if requested capabilities are supported
func (c *VultrControllerServer) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) { //nolint:lll
if req.VolumeId == "" {
return nil, status.Error(codes.InvalidArgument, "ValidateVolumeCapabilities: volume ID is missing")
}
if req.VolumeCapabilities == nil {
return nil, status.Error(codes.InvalidArgument, "ValidateVolumeCapabilities: volume Capabilities is missing")
}
diskType := strings.ToLower(req.Parameters["disk_type"])
storageType := strings.ToLower(req.Parameters["storage_type"])
blockType := strings.ToLower(req.Parameters["block_type"])
// handle legacy param
if blockType != "" {
storageType = "block"
if blockType == "high_perf" {
diskType = "nvme"
} else if blockType == "storage_opt" {
diskType = "hdd"
}
}
c.Driver.log.WithFields(logrus.Fields{
"volume-id": req.VolumeId,
"capabilities": req.VolumeCapabilities,
"parameters": req.Parameters,
}).Info("ValidateVolumeCapabilites: called")
sh, err := vultrstorage.NewVultrStorageHandler(c.Driver.client, storageType, diskType, false)
if err != nil {
return nil, status.Errorf(codes.Internal, "ValidateVolumeCapabilities: cannot initialize vultr storage handler. %v", err.Error())
}
if _, err := sh.Operations.Get(ctx, req.VolumeId); err != nil {
return nil, status.Errorf(codes.NotFound, "cannot get volume: %v", err.Error())
}
return &csi.ValidateVolumeCapabilitiesResponse{
Confirmed: &csi.ValidateVolumeCapabilitiesResponse_Confirmed{VolumeCapabilities: sh.Capabilities},
}, nil
}
// ListVolumes performs the list volume function
func (c *VultrControllerServer) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) {
var entries []*csi.ListVolumesResponse_Entry
storages, err := vultrstorage.ListAllStorages(ctx, c.Driver.client)
if err != nil {
return nil, status.Errorf(codes.Internal, "ListVolumes: cannot retrieve all volumes: %v", err.Error())
}
for i := range storages {
entries = append(entries, &csi.ListVolumesResponse_Entry{
Volume: &csi.Volume{
VolumeId: storages[i].ID,
CapacityBytes: int64(storages[i].SizeGB) * gibiByte,
},
})
}
res := &csi.ListVolumesResponse{
Entries: entries,
}
c.Driver.log.WithFields(logrus.Fields{
"volumes": entries,
}).Info("ListVolumes: called")
return res, nil
}
func (c *VultrControllerServer) GetCapacity(context.Context, *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
// ControllerGetCapabilities get capabilities of the controller
func (c *VultrControllerServer) ControllerGetCapabilities(context.Context, *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) { //nolint:lll
capability := func(capability csi.ControllerServiceCapability_RPC_Type) *csi.ControllerServiceCapability {
return &csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: capability,
},
},
}
}
var capabilities []*csi.ControllerServiceCapability
for _, caps := range []csi.ControllerServiceCapability_RPC_Type{
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME,
csi.ControllerServiceCapability_RPC_LIST_VOLUMES,
csi.ControllerServiceCapability_RPC_EXPAND_VOLUME,
} {
capabilities = append(capabilities, capability(caps))
}
resp := &csi.ControllerGetCapabilitiesResponse{
Capabilities: capabilities,
}
c.Driver.log.WithFields(logrus.Fields{
"response": resp,
"method": "controller-get-capabilities",
})
return resp, nil
}
// CreateSnapshot provides snapshot creation
func (c *VultrControllerServer) CreateSnapshot(context.Context, *csi.CreateSnapshotRequest) (*csi.CreateSnapshotResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
// DeleteSnapshot provides snapshot deletion
func (c *VultrControllerServer) DeleteSnapshot(context.Context, *csi.DeleteSnapshotRequest) (*csi.DeleteSnapshotResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
// ListSnapshots provides the list snapshot
func (c *VultrControllerServer) ListSnapshots(context.Context, *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
// ControllerExpandVolume provides the expand volume
func (c *VultrControllerServer) ControllerExpandVolume(ctx context.Context, req *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) { //nolint:lll
volumeID := req.GetVolumeId()
if volumeID == "" {
return nil, status.Error(codes.InvalidArgument, "ControllerExpandVolume: volume ID must be provided")
}
sh, err := vultrstorage.FindVultrStorageHandlerByID(ctx, c.Driver.client, req.VolumeId)
if err != nil {
return nil, status.Errorf(codes.Internal, "ControllerExpandVolume: could not find storage handler for volume: %v", err.Error())
}
curVolume, err := sh.Operations.Get(ctx, req.VolumeId)
if err != nil {
return nil, status.Errorf(codes.Internal, "ControllerExpandVolume: could not retrieve volume: %v", err.Error())
}
newSizeBytes, err := getStorageBytes(req.CapacityRange, sh)
if err != nil {
return nil, status.Errorf(codes.Internal, "ControllerExpandVolume: unable to determine requested size: %v", err.Error())
}
if int64(curVolume.SizeGB)*gibiByte > newSizeBytes {
return nil, status.Error(codes.InvalidArgument, "ControllerExpandVolume: requested size must be larger than current size.")
}
c.Driver.log.WithFields(logrus.Fields{
"volume-id": req.VolumeId,
"size": newSizeBytes,
}).Info("ControllerExpandVolume: called")
updateReq := vultrstorage.VultrStorageUpdateReq{
SizeGB: int(newSizeBytes / gibiByte),
}
if _, err := sh.Operations.Update(ctx, req.VolumeId, updateReq); err != nil {
return nil, status.Errorf(codes.Internal, "ControllerExpandVolume: unable to update storage: %v", err.Error())
}
nodeExpansion := false
if sh.StorageType == "block" {
nodeExpansion = true
}
return &csi.ControllerExpandVolumeResponse{CapacityBytes: newSizeBytes, NodeExpansionRequired: nodeExpansion}, nil
}
// ControllerGetVolume This relates to being able to get health checks on a PV. We do not have this
func (c *VultrControllerServer) ControllerGetVolume(ctx context.Context, request *csi.ControllerGetVolumeRequest) (*csi.ControllerGetVolumeResponse, error) { //nolint:lll
return nil, status.Error(codes.Unimplemented, "")
}
// validateCapabilities compares the requested capabilities with the supported
// capabilities and returning false if not supported. Currently, only the
// AccessMode capability is relevant and checked.
func validateCapabilities(reqCaps, storageCaps []*csi.VolumeCapability) error {
for i := range reqCaps {
if reqCaps[i] == nil {
return fmt.Errorf("requested capability missing")
}
accessMode := reqCaps[i].GetAccessMode()
if accessMode == nil {
return fmt.Errorf("requested capability access mode is missing")
}
var modeMatch bool
for j := range storageCaps {
if accessMode.GetMode() == storageCaps[j].AccessMode.GetMode() {
modeMatch = true
}
}
if !modeMatch {
return fmt.Errorf("requested capability access mode is not supported")
}
accessType := reqCaps[i].GetAccessType()
if accessType != nil {
switch accessType.(type) {
case *csi.VolumeCapability_Block:
case *csi.VolumeCapability_Mount:
default:
return fmt.Errorf("requested capability is not supported: %v", accessType)
}
}
}
return nil
}
func getStorageBytes(capRange *csi.CapacityRange, sh *vultrstorage.VultrStorageHandler) (int64, error) {
// return the csi capacity in bytes if present
if capRange != nil {
return capRange.GetRequiredBytes(), nil
}
// otherwise return the defaults
if sh.DefaultSize != 0 {
return sh.DefaultSize, nil
}
return 0, fmt.Errorf("default size unavailable for type %v storage %v disk", sh.StorageType, sh.DiskType)
}