-
Notifications
You must be signed in to change notification settings - Fork 30
/
controllerserver.go
768 lines (658 loc) · 24.5 KB
/
controllerserver.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
package glusterfs
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/gluster/gluster-csi-driver/pkg/glusterfs/utils"
csi "github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/gluster/glusterd2/pkg/api"
gd2Error "github.com/gluster/glusterd2/pkg/errors"
"github.com/golang/glog"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
volumeOwnerAnn = "VolumeOwner"
defaultVolumeSize int64 = 1000 * utils.MB // default volume size ie 1 GB
defaultReplicaCount = 3
minReplicaCount = 1
maxReplicaCount = 10
)
var errVolumeNotFound = errors.New("volume not found")
// ControllerServer struct of GlusterFS CSI driver with supported methods of CSI controller server spec.
type ControllerServer struct {
*GfDriver
}
// CsiDrvParam stores csi driver specific request parameters.
// This struct will be used to gather specific fields of CSI driver:
// For eg. csiDrvName, csiDrvVersion..etc
// and also gather parameters passed from SC which not part of gluster volcreate api.
// GlusterCluster - The resturl of gluster cluster
// GlusterUser - The gluster username who got access to the APIs.
// GlusterUserToken - The password/token of glusterUser to connect to glusterCluster
// GlusterVersion - Says the version of the glustercluster running in glusterCluster endpoint.
// All of these fields are optional and can be used if needed.
type CsiDrvParam struct {
GlusterCluster string
GlusterUser string
GlusterUserToken string
GlusterVersion string
CsiDrvName string
CsiDrvVersion string
}
// ProvisionerConfig is the combined configuration of gluster cli vol create request and CSI driver specific input
type ProvisionerConfig struct {
gdVolReq *api.VolCreateReq
//csiConf *CsiDrvParam
}
// ParseCreateVolRequest parse incoming volume create request and fill ProvisionerConfig.
func (cs *ControllerServer) ParseCreateVolRequest(req *csi.CreateVolumeRequest) (*ProvisionerConfig, error) {
var reqConf ProvisionerConfig
var gdReq api.VolCreateReq
var err error
reqConf.gdVolReq = &gdReq
replicaCount := defaultReplicaCount
// Get Volume name
if req != nil {
reqConf.gdVolReq.Name = req.Name
}
for k, v := range req.GetParameters() {
switch strings.ToLower(k) {
case "replicas":
replicas := v
replicaCount, err = parseVolumeParamInt(replicas, minReplicaCount, maxReplicaCount)
if err != nil {
return nil, fmt.Errorf("invalid value for parameter '%s', %v", k, err)
}
default:
return nil, fmt.Errorf("invalid option %s given for %s CSI driver", k, glusterfsCSIDriverName)
}
}
gdReq.ReplicaCount = replicaCount
return &reqConf, nil
}
func parseVolumeParamInt(valueString string, min int, max int) (int, error) {
count, err := strconv.Atoi(valueString)
if err != nil {
return 0, fmt.Errorf("value '%s' must be an integer between %d and %d", valueString, min, max)
}
if count < min {
return 0, fmt.Errorf("value '%s' must be >= %v", valueString, min)
}
if count > max {
return 0, fmt.Errorf("value '%s' must be <= %v", valueString, max)
}
return count, nil
}
// CreateVolume creates and starts the volume
func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
glog.V(2).Infof("request received %+v", req)
if err := cs.validateCreateVolumeReq(req); err != nil {
return nil, err
}
glog.V(1).Infof("creating volume with name %s", req.Name)
volSizeBytes := cs.getVolumeSize(req)
// parse the request.
parseResp, err := cs.ParseCreateVolRequest(req)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "failed to parse request")
}
volumeName := parseResp.gdVolReq.Name
err = cs.checkExistingVolume(volumeName, volSizeBytes)
if err != nil {
if err != errVolumeNotFound {
glog.Errorf("error checking for pre-existing volume: %v", err)
return nil, err
}
if req.VolumeContentSource.GetSnapshot().GetId() != "" {
snapName := req.VolumeContentSource.GetSnapshot().GetId()
glog.V(2).Infof("creating volume from snapshot %s", snapName)
err = cs.checkExistingSnapshot(snapName, req.GetName())
if err != nil {
return nil, err
}
} else {
// If volume does not exist, provision volume
err = cs.doVolumeCreate(volumeName, volSizeBytes)
if err != nil {
return nil, err
}
}
}
err = cs.client.VolumeStart(volumeName, true)
if err != nil {
//we dont need to delete the volume if volume start fails
//as we are listing the volumes and starting it again
//before sending back the response
glog.Errorf("failed to start volume: %v", err)
return nil, status.Errorf(codes.Internal, "failed to start volume: %v", err)
}
glusterServer, bkpServers, err := cs.getClusterNodes()
if err != nil {
glog.Errorf("failed to get cluster nodes: %v", err)
return nil, status.Errorf(codes.Internal, "failed to get cluster nodes: %v", err)
}
resp := &csi.CreateVolumeResponse{
Volume: &csi.Volume{
Id: volumeName,
CapacityBytes: volSizeBytes,
Attributes: map[string]string{
"glustervol": volumeName,
"glusterserver": glusterServer,
"glusterbkpservers": strings.Join(bkpServers, ":"),
},
},
}
glog.V(4).Infof("CSI volume response: %+v", resp)
return resp, nil
}
func (cs *ControllerServer) getVolumeSize(req *csi.CreateVolumeRequest) int64 {
// If capacity mentioned, pick that or use default size 1 GB
volSizeBytes := defaultVolumeSize
if capRange := req.GetCapacityRange(); capRange != nil {
volSizeBytes = capRange.GetRequiredBytes()
}
return volSizeBytes
}
func (cs *ControllerServer) checkExistingSnapshot(snapName, volName string) error {
snapInfo, err := cs.client.SnapshotInfo(snapName)
if err != nil {
errResp := cs.client.LastErrorResponse()
//errResp will be nil in case of No route to host error
if errResp != nil && errResp.StatusCode == http.StatusNotFound {
return status.Errorf(codes.NotFound, "failed to get snapshot info %s", err.Error())
}
return status.Error(codes.Internal, err.Error())
}
if snapInfo.VolInfo.State != api.VolStarted {
actReq := api.SnapActivateReq{
Force: true,
}
err = cs.client.SnapshotActivate(actReq, snapName)
if err != nil {
glog.Errorf("failed to activate snapshot: %v", err)
return status.Errorf(codes.Internal, "failed to activate snapshot %s", err.Error())
}
}
//create snapshot clone
err = cs.createSnapshotClone(snapName, volName)
return err
}
func (cs *ControllerServer) createSnapshotClone(snapName, volName string) error {
var snapreq api.SnapCloneReq
snapreq.CloneName = volName
snapResp, err := cs.client.SnapshotClone(snapName, snapreq)
if err != nil {
glog.Errorf("failed to create volume clone: %v", err)
return status.Errorf(codes.Internal, "failed to create volume clone: %s", err.Error())
}
glog.V(1).Infof("snapshot clone response : %+v", snapResp)
return nil
}
func (cs *ControllerServer) validateCreateVolumeReq(req *csi.CreateVolumeRequest) error {
if req == nil {
return status.Errorf(codes.InvalidArgument, "request cannot be empty")
}
if req.GetName() == "" {
return status.Error(codes.InvalidArgument, "name is a required field")
}
reqCaps := req.GetVolumeCapabilities()
if reqCaps == nil {
return status.Error(codes.InvalidArgument, "volume capabilities is a required field")
}
for _, cap := range reqCaps {
if cap.GetBlock() != nil {
return status.Error(codes.Unimplemented, "block volume not supported")
}
}
return nil
}
func (cs *ControllerServer) doVolumeCreate(volumeName string, volSizeBytes int64) error {
glog.V(4).Infof("received request to create volume %s with size %d", volumeName, volSizeBytes)
volMetaMap := make(map[string]string)
volMetaMap[volumeOwnerAnn] = glusterfsCSIDriverName
volumeReq := api.VolCreateReq{
Name: volumeName,
Metadata: volMetaMap,
ReplicaCount: defaultReplicaCount,
Size: uint64(volSizeBytes),
}
glog.V(2).Infof("volume create request: %+v", volumeReq)
volumeCreateResp, err := cs.client.VolumeCreate(volumeReq)
if err != nil {
glog.Errorf("failed to create volume: %v", err)
errResp := cs.client.LastErrorResponse()
//errResp will be nil in case of `No route to host` error
if errResp != nil && errResp.StatusCode == http.StatusConflict {
return status.Errorf(codes.Aborted, "volume create already in process: %v", err)
}
return status.Errorf(codes.Internal, "failed to create volume: %v", err)
}
glog.V(3).Infof("volume create response : %+v", volumeCreateResp)
return nil
}
func (cs *ControllerServer) checkExistingVolume(volumeName string, volSizeBytes int64) error {
vol, err := cs.client.Volumes(volumeName)
if err != nil {
errResp := cs.client.LastErrorResponse()
//errResp will be nil in case of `No route to host` error
if errResp != nil && errResp.StatusCode == http.StatusNotFound {
return errVolumeNotFound
}
glog.Errorf("failed to fetch volume : %v", err)
return status.Errorf(codes.Internal, "error in fetching volume details %v", err)
}
volInfo := vol[0]
// Do the owner validation
if glusterAnnVal, found := volInfo.Metadata[volumeOwnerAnn]; !found || (found && glusterAnnVal != glusterfsCSIDriverName) {
return status.Errorf(codes.Internal, "volume %s (%s) is not owned by GlusterFS CSI driver",
volInfo.Name, volInfo.Metadata)
}
if int64(volInfo.Capacity) != volSizeBytes {
return status.Errorf(codes.AlreadyExists, "volume %s already exits with different size: %d", volInfo.Name, volInfo.Capacity)
}
//volume has not started, start the volume
if volInfo.State != api.VolStarted {
err = cs.client.VolumeStart(volInfo.Name, true)
if err != nil {
return status.Errorf(codes.Internal, "failed to start volume %s: %v", volInfo.Name, err)
}
}
glog.Infof("requested volume %s already exists in the gluster cluster", volumeName)
return nil
}
func (cs *ControllerServer) getClusterNodes() (string, []string, error) {
peers, err := cs.client.Peers()
if err != nil {
return "", nil, err
}
glusterServer := ""
bkpservers := []string{}
for i, p := range peers {
if i == 0 {
for _, a := range p.PeerAddresses {
ip := strings.Split(a, ":")
glusterServer = ip[0]
}
continue
}
for _, a := range p.PeerAddresses {
ip := strings.Split(a, ":")
bkpservers = append(bkpservers, ip[0])
}
}
glog.V(2).Infof("primary and backup gluster servers [%+v,%+v]", glusterServer, bkpservers)
return glusterServer, bkpservers, err
}
// DeleteVolume deletes the given volume.
func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "volume delete request is nil")
}
volumeID := req.GetVolumeId()
if volumeID == "" {
return nil, status.Error(codes.InvalidArgument, "volume ID is nil")
}
glog.V(2).Infof("deleting volume with ID: %s", volumeID)
// Stop volume
err := cs.client.VolumeStop(req.VolumeId)
if err != nil {
errResp := cs.client.LastErrorResponse()
//errResp will be nil in case of `No route to host` error
if errResp != nil && errResp.StatusCode == http.StatusNotFound {
return &csi.DeleteVolumeResponse{}, nil
}
if err.Error() != gd2Error.ErrVolAlreadyStopped.Error() {
glog.Errorf("failed to stop volume %s: %v", volumeID, err)
return nil, status.Errorf(codes.Internal, "failed to stop volume %s: %v", volumeID, err)
}
}
// Delete volume
err = cs.client.VolumeDelete(req.VolumeId)
if err != nil {
errResp := cs.client.LastErrorResponse()
//errResp will be nil in case of No route to host error
if errResp != nil && errResp.StatusCode == http.StatusNotFound {
return &csi.DeleteVolumeResponse{}, nil
}
glog.Errorf("deleting volume %s failed: %v", req.VolumeId, err)
return nil, status.Errorf(codes.Internal, "deleting volume %s failed: %v", req.VolumeId, err)
}
glog.Infof("successfully deleted volume %s", volumeID)
return &csi.DeleteVolumeResponse{}, nil
}
// ControllerPublishVolume return Unimplemented error
func (cs *ControllerServer) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
// ControllerUnpublishVolume return Unimplemented error
func (cs *ControllerServer) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
// ValidateVolumeCapabilities checks whether the volume capabilities requested
// are supported.
func (cs *ControllerServer) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "ValidateVolumeCapabilities() - request is nil")
}
volumeID := req.GetVolumeId()
if volumeID == "" {
return nil, status.Error(codes.InvalidArgument, "ValidateVolumeCapabilities() - VolumeId is nil")
}
reqCaps := req.GetVolumeCapabilities()
if reqCaps == nil {
return nil, status.Error(codes.InvalidArgument, "ValidateVolumeCapabilities() - VolumeCapabilities is nil")
}
_, err := cs.client.Volumes(volumeID)
if err != nil {
return nil, status.Errorf(codes.NotFound, "ValidateVolumeCapabilities() - %v", err)
}
var vcaps []*csi.VolumeCapability_AccessMode
for _, mode := range []csi.VolumeCapability_AccessMode_Mode{
csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY,
csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY,
csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER,
} {
vcaps = append(vcaps, &csi.VolumeCapability_AccessMode{Mode: mode})
}
capSupport := true
IsSupport := func(mode csi.VolumeCapability_AccessMode_Mode) bool {
for _, m := range vcaps {
if mode == m.Mode {
return true
}
}
return false
}
for _, cap := range reqCaps {
if !IsSupport(cap.AccessMode.Mode) {
capSupport = false
}
}
resp := &csi.ValidateVolumeCapabilitiesResponse{
Supported: capSupport,
}
glog.V(1).Infof("GlusterFS CSI driver volume capabilities: %+v", resp)
return resp, nil
}
// ListVolumes returns a list of volumes
func (cs *ControllerServer) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) {
//Fetch all the volumes in the TSP
volumes, err := cs.client.Volumes("")
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
var entries []*csi.ListVolumesResponse_Entry
for _, vol := range volumes {
entries = append(entries, &csi.ListVolumesResponse_Entry{
Volume: &csi.Volume{
Id: vol.Name,
CapacityBytes: int64(vol.Capacity),
},
})
}
resp := &csi.ListVolumesResponse{
Entries: entries,
}
return resp, nil
}
// GetCapacity returns the capacity of the storage pool
func (cs *ControllerServer) GetCapacity(ctx context.Context, req *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
// ControllerGetCapabilities returns the capabilities of the controller service.
func (cs *ControllerServer) ControllerGetCapabilities(ctx context.Context, req *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) {
newCap := func(cap csi.ControllerServiceCapability_RPC_Type) *csi.ControllerServiceCapability {
return &csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: cap,
},
},
}
}
var caps []*csi.ControllerServiceCapability
for _, cap := range []csi.ControllerServiceCapability_RPC_Type{
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
csi.ControllerServiceCapability_RPC_LIST_VOLUMES,
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT,
csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS,
} {
caps = append(caps, newCap(cap))
}
resp := &csi.ControllerGetCapabilitiesResponse{
Capabilities: caps,
}
return resp, nil
}
// CreateSnapshot create snapshot of an existing PV
func (cs *ControllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequest) (*csi.CreateSnapshotResponse, error) {
if err := cs.validateCreateSnapshotReq(req); err != nil {
return nil, err
}
glog.V(2).Infof("received request to create snapshot %v from volume %v", req.GetName(), req.GetSourceVolumeId())
snapInfo, err := cs.client.SnapshotInfo(req.Name)
if err != nil {
glog.Errorf("failed to get snapshot info for %v with Error %v", req.GetName(), err.Error())
errResp := cs.client.LastErrorResponse()
//errResp will be nil in case of No route to host error
if errResp != nil && errResp.StatusCode != http.StatusNotFound {
return nil, status.Errorf(codes.Internal, "CreateSnapshot - failed to get snapshot info %s", err.Error())
}
if errResp == nil {
return nil, status.Error(codes.Internal, err.Error())
}
} else {
if snapInfo.ParentVolName != req.GetSourceVolumeId() {
glog.Errorf("snapshot %v belongs to different volume %v", req.GetName(), snapInfo.ParentVolName)
return nil, status.Errorf(codes.AlreadyExists, "CreateSnapshot - snapshot %s belongs to different volume %s", snapInfo.ParentVolName, req.GetSourceVolumeId())
}
return &csi.CreateSnapshotResponse{
Snapshot: &csi.Snapshot{
Id: snapInfo.VolInfo.Name,
SourceVolumeId: snapInfo.ParentVolName,
CreatedAt: snapInfo.CreatedAt.Unix(),
SizeBytes: int64(snapInfo.VolInfo.Capacity),
Status: &csi.SnapshotStatus{
Type: csi.SnapshotStatus_READY,
},
},
}, nil
}
snapResp, err := cs.doSnapshot(req.GetName(), req.GetSourceVolumeId())
if err != nil {
return nil, err
}
return &csi.CreateSnapshotResponse{
Snapshot: &csi.Snapshot{
Id: snapResp.VolInfo.Name,
SourceVolumeId: snapResp.ParentVolName,
CreatedAt: snapResp.CreatedAt.Unix(),
SizeBytes: int64(snapResp.VolInfo.Capacity),
Status: &csi.SnapshotStatus{
Type: csi.SnapshotStatus_READY,
},
},
}, nil
}
func (cs *ControllerServer) doSnapshot(name, sourceVolID string) (*api.SnapCreateResp, error) {
snapReq := api.SnapCreateReq{
VolName: sourceVolID,
SnapName: name,
Force: true,
}
glog.V(2).Infof("snapshot request: %+v", snapReq)
snapResp, err := cs.client.SnapshotCreate(snapReq)
if err != nil {
glog.Errorf("failed to create snapshot %v", err)
errResp := cs.client.LastErrorResponse()
//errResp will be nil in case of `No route to host` error
if errResp != nil && errResp.StatusCode == http.StatusConflict {
return nil, status.Errorf(codes.Aborted, "snapshot create already in process: %v", err)
}
return nil, status.Errorf(codes.Internal, "CreateSnapshot - snapshot create failed %s", err.Error())
}
actReq := api.SnapActivateReq{
Force: true,
}
err = cs.client.SnapshotActivate(actReq, name)
if err != nil {
glog.Errorf("failed to activate snapshot %v", err)
return nil, status.Errorf(codes.Internal, "failed to activate snapshot %s", err.Error())
}
return &snapResp, nil
}
func (cs *ControllerServer) validateCreateSnapshotReq(req *csi.CreateSnapshotRequest) error {
if req == nil {
return status.Errorf(codes.InvalidArgument, "CreateSnapshot request is nil")
}
if req.GetName() == "" {
return status.Error(codes.InvalidArgument, "CreateSnapshot - name cannot be nil")
}
if req.GetSourceVolumeId() == "" {
return status.Error(codes.InvalidArgument, "CreateSnapshot - sourceVolumeId is nil")
}
if req.GetName() == req.GetSourceVolumeId() {
//In glusterd2 we cannot create a snapshot as same name as volume name
return status.Error(codes.InvalidArgument, "CreateSnapshot - sourceVolumeId and snapshot name cannot be same")
}
return nil
}
// DeleteSnapshot delete provided snapshot of a PV
func (cs *ControllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteSnapshotRequest) (*csi.DeleteSnapshotResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "DeleteSnapshot request is nil")
}
if req.GetSnapshotId() == "" {
return nil, status.Error(codes.InvalidArgument, "DeleteSnapshot - snapshotId is empty")
}
glog.V(4).Infof("deleting snapshot %s", req.GetSnapshotId())
err := cs.client.SnapshotDeactivate(req.GetSnapshotId())
if err != nil {
errResp := cs.client.LastErrorResponse()
if errResp != nil && errResp.StatusCode == http.StatusNotFound {
return &csi.DeleteSnapshotResponse{}, nil
}
if err.Error() != gd2Error.ErrSnapDeactivated.Error() {
glog.Errorf("failed to deactivate snapshot %v", err)
return nil, status.Errorf(codes.Internal, "DeleteSnapshot - failed to deactivate snapshot %s", err.Error())
}
}
err = cs.client.SnapshotDelete(req.SnapshotId)
if err != nil {
errResp := cs.client.LastErrorResponse()
if errResp != nil && errResp.StatusCode == http.StatusNotFound {
return &csi.DeleteSnapshotResponse{}, nil
}
glog.Errorf("failed to delete snapshot %v", err)
return nil, status.Errorf(codes.Internal, "DeleteSnapshot - failed to delete snapshot %s", err.Error())
}
return &csi.DeleteSnapshotResponse{}, nil
}
// ListSnapshots list the snapshots of a PV
func (cs *ControllerServer) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
var (
snaplist api.SnapListResp
err error
startToken int32
)
if req.GetStartingToken() != "" {
i, parseErr := strconv.ParseUint(req.StartingToken, 10, 32)
if parseErr != nil {
return nil, status.Errorf(codes.Aborted, "invalid starting token %s", parseErr.Error())
}
startToken = int32(i)
}
if len(req.GetSnapshotId()) != 0 {
return cs.listSnapshotFromID(req.GetSnapshotId())
}
//If volume id is sent
if len(req.GetSourceVolumeId()) != 0 {
snaplist, err = cs.client.SnapshotList(req.SourceVolumeId)
if err != nil {
errResp := cs.client.LastErrorResponse()
if errResp != nil && errResp.StatusCode == http.StatusNotFound {
resp := csi.ListSnapshotsResponse{}
return &resp, nil
}
glog.Errorf("failed to list snapshots %v", err)
return nil, status.Errorf(codes.Internal, "ListSnapshot - failed to get snapshots %s", err.Error())
}
} else {
//Get all snapshots
snaplist, err = cs.client.SnapshotList("")
if err != nil {
glog.Errorf("failed to list snapshots %v", err)
return nil, status.Errorf(codes.Internal, "failed to get snapshots %s", err.Error())
}
}
return cs.doPagination(req, snaplist, startToken)
}
func (cs *ControllerServer) listSnapshotFromID(snapID string) (*csi.ListSnapshotsResponse, error) {
var entries []*csi.ListSnapshotsResponse_Entry
snap, err := cs.client.SnapshotInfo(snapID)
if err != nil {
errResp := cs.client.LastErrorResponse()
if errResp != nil && errResp.StatusCode == http.StatusNotFound {
resp := csi.ListSnapshotsResponse{}
return &resp, nil
}
glog.Errorf("failed to get snapshot info %v", err)
return nil, status.Errorf(codes.NotFound, "ListSnapshot - failed to get snapshot info %s", err.Error())
}
entries = append(entries, &csi.ListSnapshotsResponse_Entry{
Snapshot: &csi.Snapshot{
Id: snap.VolInfo.Name,
SourceVolumeId: snap.ParentVolName,
CreatedAt: snap.CreatedAt.Unix(),
SizeBytes: int64(snap.VolInfo.Capacity),
Status: &csi.SnapshotStatus{
Type: csi.SnapshotStatus_READY,
},
},
})
resp := csi.ListSnapshotsResponse{}
resp.Entries = entries
return &resp, nil
}
func (cs *ControllerServer) doPagination(req *csi.ListSnapshotsRequest, snapList api.SnapListResp, startToken int32) (*csi.ListSnapshotsResponse, error) {
if req.GetStartingToken() != "" && int(startToken) > len(snapList) {
return nil, status.Error(codes.Aborted, "invalid starting token")
}
var entries []*csi.ListSnapshotsResponse_Entry
for _, snap := range snapList {
for _, s := range snap.SnapList {
entries = append(entries, &csi.ListSnapshotsResponse_Entry{
Snapshot: &csi.Snapshot{
Id: s.VolInfo.Name,
SourceVolumeId: snap.ParentName,
CreatedAt: s.CreatedAt.Unix(),
SizeBytes: int64(s.VolInfo.Capacity),
Status: &csi.SnapshotStatus{
Type: csi.SnapshotStatus_READY,
},
},
})
}
}
//TODO need to remove paginating code once glusterd2 issue
//https://github.com/gluster/glusterd2/issues/372 is merged
var (
maximumEntries = req.MaxEntries
nextToken int32
remainingEntries = int32(len(snapList)) - startToken
resp csi.ListSnapshotsResponse
)
if maximumEntries == 0 || maximumEntries > remainingEntries {
maximumEntries = remainingEntries
}
resp.Entries = entries[startToken : startToken+maximumEntries]
if nextToken = startToken + maximumEntries; nextToken < int32(len(snapList)) {
resp.NextToken = fmt.Sprintf("%d", nextToken)
}
return &resp, nil
}