-
Notifications
You must be signed in to change notification settings - Fork 210
/
controllerserver.go
834 lines (716 loc) · 26.7 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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
/*
Copyright 2017 The Kubernetes 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 hostpath
import (
"fmt"
"math"
"os"
"sort"
"strconv"
"github.com/golang/protobuf/ptypes"
"github.com/golang/glog"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/pborman/uuid"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/container-storage-interface/spec/lib/go/csi"
"k8s.io/klog/v2"
utilexec "k8s.io/utils/exec"
"github.com/kubernetes-csi/csi-driver-host-path/pkg/state"
)
const (
deviceID = "deviceID"
)
func (hp *hostPath) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (resp *csi.CreateVolumeResponse, finalErr error) {
if err := hp.validateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
glog.V(3).Infof("invalid create volume req: %v", req)
return nil, err
}
// Check arguments
if len(req.GetName()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Name missing in request")
}
caps := req.GetVolumeCapabilities()
if caps == nil {
return nil, status.Error(codes.InvalidArgument, "Volume Capabilities missing in request")
}
// Keep a record of the requested access types.
var accessTypeMount, accessTypeBlock bool
for _, cap := range caps {
if cap.GetBlock() != nil {
accessTypeBlock = true
}
if cap.GetMount() != nil {
accessTypeMount = true
}
}
// A real driver would also need to check that the other
// fields in VolumeCapabilities are sane. The check above is
// just enough to pass the "[Testpattern: Dynamic PV (block
// volmode)] volumeMode should fail in binding dynamic
// provisioned PV to PVC" storage E2E test.
if accessTypeBlock && accessTypeMount {
return nil, status.Error(codes.InvalidArgument, "cannot have both block and mount access type")
}
var requestedAccessType state.AccessType
if accessTypeBlock {
requestedAccessType = state.BlockAccess
} else {
// Default to mount.
requestedAccessType = state.MountAccess
}
// Lock before acting on global state. A production-quality
// driver might use more fine-grained locking.
hp.mutex.Lock()
defer hp.mutex.Unlock()
capacity := int64(req.GetCapacityRange().GetRequiredBytes())
topologies := []*csi.Topology{}
if hp.config.EnableTopology {
topologies = append(topologies, &csi.Topology{Segments: map[string]string{TopologyKeyNode: hp.config.NodeID}})
}
// Need to check for already existing volume name, and if found
// check for the requested capacity and already allocated capacity
if exVol, err := hp.state.GetVolumeByName(req.GetName()); err == nil {
// Since err is nil, it means the volume with the same name already exists
// need to check if the size of existing volume is the same as in new
// request
if exVol.VolSize < capacity {
return nil, status.Errorf(codes.AlreadyExists, "Volume with the same name: %s but with different size already exist", req.GetName())
}
if req.GetVolumeContentSource() != nil {
volumeSource := req.VolumeContentSource
switch volumeSource.Type.(type) {
case *csi.VolumeContentSource_Snapshot:
if volumeSource.GetSnapshot() != nil && exVol.ParentSnapID != "" && exVol.ParentSnapID != volumeSource.GetSnapshot().GetSnapshotId() {
return nil, status.Error(codes.AlreadyExists, "existing volume source snapshot id not matching")
}
case *csi.VolumeContentSource_Volume:
if volumeSource.GetVolume() != nil && exVol.ParentVolID != volumeSource.GetVolume().GetVolumeId() {
return nil, status.Error(codes.AlreadyExists, "existing volume source volume id not matching")
}
default:
return nil, status.Errorf(codes.InvalidArgument, "%v not a proper volume source", volumeSource)
}
}
// TODO (sbezverk) Do I need to make sure that volume still exists?
return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
VolumeId: exVol.VolID,
CapacityBytes: int64(exVol.VolSize),
VolumeContext: req.GetParameters(),
ContentSource: req.GetVolumeContentSource(),
AccessibleTopology: topologies,
},
}, nil
}
volumeID := uuid.NewUUID().String()
kind := req.GetParameters()[storageKind]
vol, err := hp.createVolume(volumeID, req.GetName(), capacity, requestedAccessType, false /* ephemeral */, kind)
if err != nil {
return nil, err
}
glog.V(4).Infof("created volume %s at path %s", vol.VolID, vol.VolPath)
if req.GetVolumeContentSource() != nil {
path := hp.getVolumePath(volumeID)
volumeSource := req.VolumeContentSource
switch volumeSource.Type.(type) {
case *csi.VolumeContentSource_Snapshot:
if snapshot := volumeSource.GetSnapshot(); snapshot != nil {
err = hp.loadFromSnapshot(capacity, snapshot.GetSnapshotId(), path, requestedAccessType)
vol.ParentSnapID = snapshot.GetSnapshotId()
}
case *csi.VolumeContentSource_Volume:
if srcVolume := volumeSource.GetVolume(); srcVolume != nil {
err = hp.loadFromVolume(capacity, srcVolume.GetVolumeId(), path, requestedAccessType)
vol.ParentVolID = srcVolume.GetVolumeId()
}
default:
err = status.Errorf(codes.InvalidArgument, "%v not a proper volume source", volumeSource)
}
if err != nil {
glog.V(4).Infof("VolumeSource error: %v", err)
if delErr := hp.deleteVolume(volumeID); delErr != nil {
glog.V(2).Infof("deleting hostpath volume %v failed: %v", volumeID, delErr)
}
return nil, err
}
glog.V(4).Infof("successfully populated volume %s", vol.VolID)
}
return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
VolumeId: volumeID,
CapacityBytes: req.GetCapacityRange().GetRequiredBytes(),
VolumeContext: req.GetParameters(),
ContentSource: req.GetVolumeContentSource(),
AccessibleTopology: topologies,
},
}, nil
}
func (hp *hostPath) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
// Check arguments
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
if err := hp.validateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
glog.V(3).Infof("invalid delete volume req: %v", req)
return nil, err
}
// Lock before acting on global state. A production-quality
// driver might use more fine-grained locking.
hp.mutex.Lock()
defer hp.mutex.Unlock()
volId := req.GetVolumeId()
vol, err := hp.state.GetVolumeByID(volId)
if err != nil {
// Volume not found: might have already deleted
return &csi.DeleteVolumeResponse{}, nil
}
if vol.Attached || !vol.Published.Empty() || !vol.Staged.Empty() {
msg := fmt.Sprintf("Volume '%s' is still used (attached: %v, staged: %v, published: %v) by '%s' node",
vol.VolID, vol.Attached, vol.Staged, vol.Published, vol.NodeID)
if hp.config.CheckVolumeLifecycle {
return nil, status.Error(codes.Internal, msg)
}
klog.Warning(msg)
}
if err := hp.deleteVolume(volId); err != nil {
return nil, fmt.Errorf("failed to delete volume %v: %w", volId, err)
}
glog.V(4).Infof("volume %v successfully deleted", volId)
return &csi.DeleteVolumeResponse{}, nil
}
func (hp *hostPath) ControllerGetCapabilities(ctx context.Context, req *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) {
return &csi.ControllerGetCapabilitiesResponse{
Capabilities: hp.getControllerServiceCapabilities(),
}, nil
}
func (hp *hostPath) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
// Check arguments
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
}
if len(req.VolumeCapabilities) == 0 {
return nil, status.Error(codes.InvalidArgument, req.VolumeId)
}
// Lock before acting on global state. A production-quality
// driver might use more fine-grained locking.
hp.mutex.Lock()
defer hp.mutex.Unlock()
if _, err := hp.state.GetVolumeByID(req.GetVolumeId()); err != nil {
return nil, err
}
for _, cap := range req.GetVolumeCapabilities() {
if cap.GetMount() == nil && cap.GetBlock() == nil {
return nil, status.Error(codes.InvalidArgument, "cannot have both mount and block access type be undefined")
}
// A real driver would check the capabilities of the given volume with
// the set of requested capabilities.
}
return &csi.ValidateVolumeCapabilitiesResponse{
Confirmed: &csi.ValidateVolumeCapabilitiesResponse_Confirmed{
VolumeContext: req.GetVolumeContext(),
VolumeCapabilities: req.GetVolumeCapabilities(),
Parameters: req.GetParameters(),
},
}, nil
}
func (hp *hostPath) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
if !hp.config.EnableAttach {
return nil, status.Error(codes.Unimplemented, "ControllerPublishVolume is not supported")
}
if len(req.VolumeId) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
}
if len(req.NodeId) == 0 {
return nil, status.Error(codes.InvalidArgument, "Node ID cannot be empty")
}
if req.VolumeCapability == nil {
return nil, status.Error(codes.InvalidArgument, "Volume Capabilities cannot be empty")
}
if req.NodeId != hp.config.NodeID {
return nil, status.Errorf(codes.NotFound, "Not matching Node ID %s to hostpath Node ID %s", req.NodeId, hp.config.NodeID)
}
hp.mutex.Lock()
defer hp.mutex.Unlock()
vol, err := hp.state.GetVolumeByID(req.VolumeId)
if err != nil {
return nil, status.Error(codes.NotFound, err.Error())
}
// Check to see if the volume is already published.
if vol.Attached {
// Check if readonly flag is compatible with the publish request.
if req.GetReadonly() != vol.ReadOnlyAttach {
return nil, status.Error(codes.AlreadyExists, "Volume published but has incompatible readonly flag")
}
return &csi.ControllerPublishVolumeResponse{
PublishContext: map[string]string{},
}, nil
}
// Check attach limit before publishing.
if hp.config.AttachLimit > 0 && hp.getAttachCount() >= hp.config.AttachLimit {
return nil, status.Errorf(codes.ResourceExhausted, "Cannot attach any more volumes to this node ('%s')", hp.config.NodeID)
}
vol.Attached = true
vol.ReadOnlyAttach = req.GetReadonly()
if err := hp.state.UpdateVolume(vol); err != nil {
return nil, err
}
return &csi.ControllerPublishVolumeResponse{
PublishContext: map[string]string{},
}, nil
}
func (hp *hostPath) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
if !hp.config.EnableAttach {
return nil, status.Error(codes.Unimplemented, "ControllerUnpublishVolume is not supported")
}
if len(req.VolumeId) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
}
// Empty node id is not a failure as per Spec
if req.NodeId != "" && req.NodeId != hp.config.NodeID {
return nil, status.Errorf(codes.NotFound, "Node ID %s does not match to expected Node ID %s", req.NodeId, hp.config.NodeID)
}
hp.mutex.Lock()
defer hp.mutex.Unlock()
vol, err := hp.state.GetVolumeByID(req.VolumeId)
if err != nil {
// Not an error: a non-existent volume is not published.
// See also https://github.com/kubernetes-csi/external-attacher/pull/165
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
// Check to see if the volume is staged/published on a node
if !vol.Published.Empty() || !vol.Staged.Empty() {
msg := fmt.Sprintf("Volume '%s' is still used (staged: %v, published: %v) by '%s' node",
vol.VolID, vol.Staged, vol.Published, vol.NodeID)
if hp.config.CheckVolumeLifecycle {
return nil, status.Error(codes.Internal, msg)
}
klog.Warning(msg)
}
vol.Attached = false
if err := hp.state.UpdateVolume(vol); err != nil {
return nil, status.Errorf(codes.Internal, "could not update volume %s: %v", vol.VolID, err)
}
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
func (hp *hostPath) GetCapacity(ctx context.Context, req *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) {
// Lock before acting on global state. A production-quality
// driver might use more fine-grained locking.
hp.mutex.Lock()
defer hp.mutex.Unlock()
// Topology and capabilities are irrelevant. We only
// distinguish based on the "kind" parameter, if at all.
// Without configured capacity, we just have the maximum size.
available := hp.config.MaxVolumeSize
if hp.config.Capacity.Enabled() {
// Empty "kind" will return "zero capacity". There is no fallback
// to some arbitrary kind here because in practice it always should
// be set.
kind := req.GetParameters()[storageKind]
quantity := hp.config.Capacity[kind]
allocated := hp.sumVolumeSizes(kind)
available = quantity.Value() - allocated
}
maxVolumeSize := hp.config.MaxVolumeSize
if maxVolumeSize > available {
maxVolumeSize = available
}
return &csi.GetCapacityResponse{
AvailableCapacity: available,
MaximumVolumeSize: &wrappers.Int64Value{Value: maxVolumeSize},
// We don't have a minimum volume size, so we might as well report that.
// Better explicit than implicit...
MinimumVolumeSize: &wrappers.Int64Value{Value: 0},
}, nil
}
func (hp *hostPath) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) {
volumeRes := &csi.ListVolumesResponse{
Entries: []*csi.ListVolumesResponse_Entry{},
}
var (
startIdx, volumesLength, maxLength int64
hpVolume state.Volume
)
// Lock before acting on global state. A production-quality
// driver might use more fine-grained locking.
hp.mutex.Lock()
defer hp.mutex.Unlock()
// Sort by volume ID.
volumes := hp.state.GetVolumes()
sort.Slice(volumes, func(i, j int) bool {
return volumes[i].VolID < volumes[j].VolID
})
if req.StartingToken == "" {
req.StartingToken = "1"
}
startIdx, err := strconv.ParseInt(req.StartingToken, 10, 32)
if err != nil {
return nil, status.Error(codes.Aborted, "The type of startingToken should be integer")
}
volumesLength = int64(len(volumes))
maxLength = int64(req.MaxEntries)
if maxLength > volumesLength || maxLength <= 0 {
maxLength = volumesLength
}
for index := startIdx - 1; index < volumesLength && index < maxLength; index++ {
hpVolume = volumes[index]
healthy, msg := hp.doHealthCheckInControllerSide(hpVolume.VolID)
glog.V(3).Infof("Healthy state: %s Volume: %t", hpVolume.VolName, healthy)
volumeRes.Entries = append(volumeRes.Entries, &csi.ListVolumesResponse_Entry{
Volume: &csi.Volume{
VolumeId: hpVolume.VolID,
CapacityBytes: hpVolume.VolSize,
},
Status: &csi.ListVolumesResponse_VolumeStatus{
PublishedNodeIds: []string{hpVolume.NodeID},
VolumeCondition: &csi.VolumeCondition{
Abnormal: !healthy,
Message: msg,
},
},
})
}
glog.V(5).Infof("Volumes are: %+v", *volumeRes)
return volumeRes, nil
}
func (hp *hostPath) ControllerGetVolume(ctx context.Context, req *csi.ControllerGetVolumeRequest) (*csi.ControllerGetVolumeResponse, error) {
// Lock before acting on global state. A production-quality
// driver might use more fine-grained locking.
hp.mutex.Lock()
defer hp.mutex.Unlock()
volume, err := hp.state.GetVolumeByID(req.GetVolumeId())
if err != nil {
// ControllerGetVolume should report abnormal volume condition if volume is not found
return &csi.ControllerGetVolumeResponse{
Volume: &csi.Volume{
VolumeId: req.GetVolumeId(),
},
Status: &csi.ControllerGetVolumeResponse_VolumeStatus{
VolumeCondition: &csi.VolumeCondition{
Abnormal: true,
Message: err.Error(),
},
},
}, nil
}
healthy, msg := hp.doHealthCheckInControllerSide(req.GetVolumeId())
glog.V(3).Infof("Healthy state: %s Volume: %t", volume.VolName, healthy)
return &csi.ControllerGetVolumeResponse{
Volume: &csi.Volume{
VolumeId: volume.VolID,
CapacityBytes: volume.VolSize,
},
Status: &csi.ControllerGetVolumeResponse_VolumeStatus{
PublishedNodeIds: []string{volume.NodeID},
VolumeCondition: &csi.VolumeCondition{
Abnormal: !healthy,
Message: msg,
},
},
}, nil
}
// CreateSnapshot uses tar command to create snapshot for hostpath volume. The tar command can quickly create
// archives of entire directories. The host image must have "tar" binaries in /bin, /usr/sbin, or /usr/bin.
func (hp *hostPath) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequest) (*csi.CreateSnapshotResponse, error) {
if err := hp.validateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT); err != nil {
glog.V(3).Infof("invalid create snapshot req: %v", req)
return nil, err
}
if len(req.GetName()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Name missing in request")
}
// Check arguments
if len(req.GetSourceVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "SourceVolumeId missing in request")
}
// Lock before acting on global state. A production-quality
// driver might use more fine-grained locking.
hp.mutex.Lock()
defer hp.mutex.Unlock()
// Need to check for already existing snapshot name, and if found check for the
// requested sourceVolumeId and sourceVolumeId of snapshot that has been created.
if exSnap, err := hp.state.GetSnapshotByName(req.GetName()); err == nil {
// Since err is nil, it means the snapshot with the same name already exists need
// to check if the sourceVolumeId of existing snapshot is the same as in new request.
if exSnap.VolID == req.GetSourceVolumeId() {
// same snapshot has been created.
return &csi.CreateSnapshotResponse{
Snapshot: &csi.Snapshot{
SnapshotId: exSnap.Id,
SourceVolumeId: exSnap.VolID,
CreationTime: exSnap.CreationTime,
SizeBytes: exSnap.SizeBytes,
ReadyToUse: exSnap.ReadyToUse,
},
}, nil
}
return nil, status.Errorf(codes.AlreadyExists, "snapshot with the same name: %s but with different SourceVolumeId already exist", req.GetName())
}
volumeID := req.GetSourceVolumeId()
hostPathVolume, err := hp.state.GetVolumeByID(volumeID)
if err != nil {
return nil, err
}
snapshotID := uuid.NewUUID().String()
creationTime := ptypes.TimestampNow()
volPath := hostPathVolume.VolPath
file := hp.getSnapshotPath(snapshotID)
var cmd []string
if hostPathVolume.VolAccessType == state.BlockAccess {
glog.V(4).Infof("Creating snapshot of Raw Block Mode Volume")
cmd = []string{"cp", volPath, file}
} else {
glog.V(4).Infof("Creating snapshot of Filsystem Mode Volume")
cmd = []string{"tar", "czf", file, "-C", volPath, "."}
}
executor := utilexec.New()
out, err := executor.Command(cmd[0], cmd[1:]...).CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed create snapshot: %w: %s", err, out)
}
glog.V(4).Infof("create volume snapshot %s", file)
snapshot := state.Snapshot{}
snapshot.Name = req.GetName()
snapshot.Id = snapshotID
snapshot.VolID = volumeID
snapshot.Path = file
snapshot.CreationTime = creationTime
snapshot.SizeBytes = hostPathVolume.VolSize
snapshot.ReadyToUse = true
if err := hp.state.UpdateSnapshot(snapshot); err != nil {
return nil, err
}
return &csi.CreateSnapshotResponse{
Snapshot: &csi.Snapshot{
SnapshotId: snapshot.Id,
SourceVolumeId: snapshot.VolID,
CreationTime: snapshot.CreationTime,
SizeBytes: snapshot.SizeBytes,
ReadyToUse: snapshot.ReadyToUse,
},
}, nil
}
func (hp *hostPath) DeleteSnapshot(ctx context.Context, req *csi.DeleteSnapshotRequest) (*csi.DeleteSnapshotResponse, error) {
// Check arguments
if len(req.GetSnapshotId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Snapshot ID missing in request")
}
if err := hp.validateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT); err != nil {
glog.V(3).Infof("invalid delete snapshot req: %v", req)
return nil, err
}
snapshotID := req.GetSnapshotId()
// Lock before acting on global state. A production-quality
// driver might use more fine-grained locking.
hp.mutex.Lock()
defer hp.mutex.Unlock()
glog.V(4).Infof("deleting snapshot %s", snapshotID)
path := hp.getSnapshotPath(snapshotID)
os.RemoveAll(path)
if err := hp.state.DeleteSnapshot(snapshotID); err != nil {
return nil, err
}
return &csi.DeleteSnapshotResponse{}, nil
}
func (hp *hostPath) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
if err := hp.validateControllerServiceRequest(csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS); err != nil {
glog.V(3).Infof("invalid list snapshot req: %v", req)
return nil, err
}
// Lock before acting on global state. A production-quality
// driver might use more fine-grained locking.
hp.mutex.Lock()
defer hp.mutex.Unlock()
// case 1: SnapshotId is not empty, return snapshots that match the snapshot id,
// none if not found.
if len(req.GetSnapshotId()) != 0 {
snapshotID := req.SnapshotId
if snapshot, err := hp.state.GetSnapshotByID(snapshotID); err == nil {
return convertSnapshot(snapshot), nil
}
return &csi.ListSnapshotsResponse{}, nil
}
// case 2: SourceVolumeId is not empty, return snapshots that match the source volume id,
// none if not found.
if len(req.GetSourceVolumeId()) != 0 {
for _, snapshot := range hp.state.GetSnapshots() {
if snapshot.VolID == req.SourceVolumeId {
return convertSnapshot(snapshot), nil
}
}
return &csi.ListSnapshotsResponse{}, nil
}
var snapshots []csi.Snapshot
// case 3: no parameter is set, so we return all the snapshots.
hpSnapshots := hp.state.GetSnapshots()
sort.Slice(hpSnapshots, func(i, j int) bool {
return hpSnapshots[i].Id < hpSnapshots[j].Id
})
for _, snap := range hpSnapshots {
snapshot := csi.Snapshot{
SnapshotId: snap.Id,
SourceVolumeId: snap.VolID,
CreationTime: snap.CreationTime,
SizeBytes: snap.SizeBytes,
ReadyToUse: snap.ReadyToUse,
}
snapshots = append(snapshots, snapshot)
}
var (
ulenSnapshots = int32(len(snapshots))
maxEntries = req.MaxEntries
startingToken int32
maxToken = uint32(math.MaxUint32)
)
if v := req.StartingToken; v != "" {
i, err := strconv.ParseUint(v, 10, 32)
if err != nil {
return nil, status.Errorf(
codes.Aborted,
"startingToken=%d !< int32=%d",
startingToken, maxToken)
}
startingToken = int32(i)
}
if startingToken > ulenSnapshots {
return nil, status.Errorf(
codes.Aborted,
"startingToken=%d > len(snapshots)=%d",
startingToken, ulenSnapshots)
}
// Discern the number of remaining entries.
rem := ulenSnapshots - startingToken
// If maxEntries is 0 or greater than the number of remaining entries then
// set maxEntries to the number of remaining entries.
if maxEntries == 0 || maxEntries > rem {
maxEntries = rem
}
var (
i int
j = startingToken
entries = make(
[]*csi.ListSnapshotsResponse_Entry,
maxEntries)
)
for i = 0; i < len(entries); i++ {
entries[i] = &csi.ListSnapshotsResponse_Entry{
Snapshot: &snapshots[j],
}
j++
}
var nextToken string
if j < ulenSnapshots {
nextToken = fmt.Sprintf("%d", j)
}
return &csi.ListSnapshotsResponse{
Entries: entries,
NextToken: nextToken,
}, nil
}
func (hp *hostPath) ControllerExpandVolume(ctx context.Context, req *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) {
if !hp.config.EnableVolumeExpansion {
return nil, status.Error(codes.Unimplemented, "ControllerExpandVolume is not supported")
}
volID := req.GetVolumeId()
if len(volID) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
capRange := req.GetCapacityRange()
if capRange == nil {
return nil, status.Error(codes.InvalidArgument, "Capacity range not provided")
}
capacity := int64(capRange.GetRequiredBytes())
if capacity > hp.config.MaxVolumeSize {
return nil, status.Errorf(codes.OutOfRange, "Requested capacity %d exceeds maximum allowed %d", capacity, hp.config.MaxVolumeSize)
}
// Lock before acting on global state. A production-quality
// driver might use more fine-grained locking.
hp.mutex.Lock()
defer hp.mutex.Unlock()
exVol, err := hp.state.GetVolumeByID(volID)
if err != nil {
return nil, err
}
if exVol.VolSize < capacity {
exVol.VolSize = capacity
if err := hp.state.UpdateVolume(exVol); err != nil {
return nil, err
}
}
return &csi.ControllerExpandVolumeResponse{
CapacityBytes: exVol.VolSize,
NodeExpansionRequired: true,
}, nil
}
func convertSnapshot(snap state.Snapshot) *csi.ListSnapshotsResponse {
entries := []*csi.ListSnapshotsResponse_Entry{
{
Snapshot: &csi.Snapshot{
SnapshotId: snap.Id,
SourceVolumeId: snap.VolID,
CreationTime: snap.CreationTime,
SizeBytes: snap.SizeBytes,
ReadyToUse: snap.ReadyToUse,
},
},
}
rsp := &csi.ListSnapshotsResponse{
Entries: entries,
}
return rsp
}
func (hp *hostPath) validateControllerServiceRequest(c csi.ControllerServiceCapability_RPC_Type) error {
if c == csi.ControllerServiceCapability_RPC_UNKNOWN {
return nil
}
for _, cap := range hp.getControllerServiceCapabilities() {
if c == cap.GetRpc().GetType() {
return nil
}
}
return status.Errorf(codes.InvalidArgument, "unsupported capability %s", c)
}
func (hp *hostPath) getControllerServiceCapabilities() []*csi.ControllerServiceCapability {
var cl []csi.ControllerServiceCapability_RPC_Type
if !hp.config.Ephemeral {
cl = []csi.ControllerServiceCapability_RPC_Type{
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
csi.ControllerServiceCapability_RPC_GET_VOLUME,
csi.ControllerServiceCapability_RPC_GET_CAPACITY,
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT,
csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS,
csi.ControllerServiceCapability_RPC_LIST_VOLUMES,
csi.ControllerServiceCapability_RPC_CLONE_VOLUME,
csi.ControllerServiceCapability_RPC_VOLUME_CONDITION,
csi.ControllerServiceCapability_RPC_SINGLE_NODE_MULTI_WRITER,
}
if hp.config.EnableVolumeExpansion && !hp.config.DisableControllerExpansion {
cl = append(cl, csi.ControllerServiceCapability_RPC_EXPAND_VOLUME)
}
if hp.config.EnableAttach {
cl = append(cl, csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME)
}
}
var csc []*csi.ControllerServiceCapability
for _, cap := range cl {
csc = append(csc, &csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: cap,
},
},
})
}
return csc
}