Skip to content

Commit 745ae14

Browse files
authored
Merge pull request #60 from jtherin/snapshotfilter
fix: filter snapshot on api side
2 parents bfcef14 + 7c12df6 commit 745ae14

File tree

4 files changed

+51
-40
lines changed

4 files changed

+51
-40
lines changed

driver/controller.go

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
"sync"
1010

1111
"github.com/container-storage-interface/spec/lib/go/csi"
12-
"github.com/golang/protobuf/ptypes"
1312
"github.com/scaleway/scaleway-csi/scaleway"
1413
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
1514
"github.com/scaleway/scaleway-sdk-go/scw"
1615
"google.golang.org/grpc/codes"
1716
"google.golang.org/grpc/status"
17+
"google.golang.org/protobuf/types/known/timestamppb"
1818
"k8s.io/klog/v2"
1919
)
2020

@@ -506,7 +506,7 @@ func (d *controllerService) ListVolumes(ctx context.Context, req *csi.ListVolume
506506
}
507507
}
508508

509-
volumesResp, err := d.scaleway.ListVolumes(&instance.ListVolumesRequest{}, scw.WithAllPages())
509+
volumesResp, err := d.scaleway.ListVolumes(&instance.ListVolumesRequest{}, scw.WithContext(ctx), scw.WithAllPages())
510510
if err != nil {
511511
return nil, status.Error(codes.Internal, err.Error())
512512
}
@@ -605,12 +605,7 @@ func (d *controllerService) CreateSnapshot(ctx context.Context, req *csi.CreateS
605605
}
606606

607607
if snapshot.CreationDate != nil {
608-
creationTime, err := ptypes.TimestampProto(*snapshot.CreationDate)
609-
if err != nil {
610-
return nil, status.Error(codes.Internal, err.Error())
611-
}
612-
snapshotResp.CreationTime = creationTime
613-
608+
snapshotResp.CreationTime = timestamppb.New(*snapshot.CreationDate)
614609
}
615610

616611
return &csi.CreateSnapshotResponse{
@@ -636,11 +631,7 @@ func (d *controllerService) CreateSnapshot(ctx context.Context, req *csi.CreateS
636631
}
637632

638633
if snapshotResp.Snapshot.CreationDate != nil {
639-
creationTime, err := ptypes.TimestampProto(*snapshotResp.Snapshot.CreationDate)
640-
if err != nil {
641-
return nil, status.Error(codes.Internal, err.Error())
642-
}
643-
snapshotProtoResp.CreationTime = creationTime
634+
snapshotProtoResp.CreationTime = timestamppb.New(*snapshotResp.Snapshot.CreationDate)
644635
}
645636

646637
return &csi.CreateSnapshotResponse{
@@ -689,23 +680,41 @@ func (d *controllerService) ListSnapshots(ctx context.Context, req *csi.ListSnap
689680
}
690681

691682
// TODO fix zones
692-
snapshotID, _, _ := getSnapshotIDAndZone(req.GetSnapshotId())
693-
sourceVolumeID, _, _ := getSourceVolumeIDAndZone(req.GetSourceVolumeId())
683+
snapshotID, snapshotZone, _ := getSnapshotIDAndZone(req.GetSnapshotId())
684+
sourceVolumeID, sourceVolumeZone, _ := getSourceVolumeIDAndZone(req.GetSourceVolumeId())
694685

695-
// TODO all zones
696-
snapshotsResp, err := d.scaleway.ListSnapshots(&instance.ListSnapshotsRequest{}, scw.WithAllPages())
697-
if err != nil {
698-
return nil, status.Error(codes.Internal, err.Error())
699-
}
700686
snapshots := []*instance.Snapshot{}
701-
for _, snap := range snapshotsResp.Snapshots {
702-
if snapshotID != "" && snap.ID == snapshotID {
703-
snapshots = []*instance.Snapshot{snap}
704-
break
687+
switch {
688+
case req.SnapshotId != "":
689+
snapshotResp, err := d.scaleway.GetSnapshot(&instance.GetSnapshotRequest{
690+
SnapshotID: snapshotID,
691+
Zone: snapshotZone,
692+
}, scw.WithContext(ctx))
693+
if err != nil {
694+
// not found should return empty list
695+
if _, ok := err.(*scw.ResourceNotFoundError); ok {
696+
return &csi.ListSnapshotsResponse{
697+
Entries: []*csi.ListSnapshotsResponse_Entry{},
698+
}, nil
699+
}
700+
return nil, status.Error(codes.Internal, err.Error())
701+
}
702+
snapshots = []*instance.Snapshot{snapshotResp.Snapshot}
703+
case sourceVolumeID != "":
704+
snapshotsResp, err := d.scaleway.ListSnapshots(&instance.ListSnapshotsRequest{
705+
BaseVolumeID: &sourceVolumeID,
706+
Zone: sourceVolumeZone,
707+
}, scw.WithContext(ctx), scw.WithAllPages())
708+
if err != nil {
709+
return nil, status.Error(codes.Internal, err.Error())
705710
}
706-
if sourceVolumeID != "" && snap.BaseVolume != nil && snap.BaseVolume.ID == sourceVolumeID || snapshotID == "" && sourceVolumeID == "" {
707-
snapshots = append(snapshots, snap)
711+
snapshots = snapshotsResp.Snapshots
712+
default:
713+
snapshotsResp, err := d.scaleway.ListSnapshots(&instance.ListSnapshotsRequest{}, scw.WithContext(ctx), scw.WithAllPages())
714+
if err != nil {
715+
return nil, status.Error(codes.Internal, err.Error())
708716
}
717+
snapshots = snapshotsResp.Snapshots
709718
}
710719

711720
nextPage := ""
@@ -738,11 +747,7 @@ func (d *controllerService) ListSnapshots(ctx context.Context, req *csi.ListSnap
738747
}
739748

740749
if snap.CreationDate != nil {
741-
creationTime, err := ptypes.TimestampProto(*snap.CreationDate)
742-
if err != nil {
743-
return nil, status.Error(codes.Internal, err.Error())
744-
}
745-
snapshotProtoResp.CreationTime = creationTime
750+
snapshotProtoResp.CreationTime = timestamppb.New(*snap.CreationDate)
746751
}
747752

748753
snapshotsEntries = append(snapshotsEntries, &csi.ListSnapshotsResponse_Entry{

driver/sanity_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,18 @@ func (s *fakeHelper) GetSnapshot(req *instance.GetSnapshotRequest, opts ...scw.R
298298
func (s *fakeHelper) ListSnapshots(req *instance.ListSnapshotsRequest, opts ...scw.RequestOption) (*instance.ListSnapshotsResponse, error) {
299299
snapshots := make([]*instance.Snapshot, 0)
300300
for _, snap := range s.snapshotsMap {
301-
if req.Name == nil || strings.Contains(snap.Name, *req.Name) {
302-
if snap.State == instance.SnapshotStateSnapshotting {
303-
snap.State = instance.SnapshotStateAvailable
304-
}
305-
snapshots = append(snapshots, snap)
301+
if req.BaseVolumeID != nil && (snap.BaseVolume == nil || *req.BaseVolumeID != snap.BaseVolume.ID) {
302+
continue
303+
}
304+
305+
if req.Name != nil && !strings.Contains(snap.Name, *req.Name) {
306+
continue
307+
}
308+
309+
if snap.State == instance.SnapshotStateSnapshotting {
310+
snap.State = instance.SnapshotStateAvailable
306311
}
312+
snapshots = append(snapshots, snap)
307313
}
308314
return &instance.ListSnapshotsResponse{Snapshots: snapshots, TotalCount: uint32(len(snapshots))}, nil
309315
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ require (
77
github.com/golang/protobuf v1.5.3
88
github.com/google/uuid v1.3.0
99
github.com/kubernetes-csi/csi-test/v5 v5.0.0
10-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.17
10+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.21.0.20230918151823-4f048611ed7c
1111
golang.org/x/sys v0.9.0
1212
google.golang.org/grpc v1.56.1
13+
google.golang.org/protobuf v1.30.0
1314
k8s.io/klog/v2 v2.100.1
1415
k8s.io/mount-utils v0.27.3
1516
k8s.io/utils v0.0.0-20230505201702-9f6742963106
@@ -26,7 +27,6 @@ require (
2627
golang.org/x/net v0.9.0 // indirect
2728
golang.org/x/text v0.9.0 // indirect
2829
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
29-
google.golang.org/protobuf v1.30.0 // indirect
3030
gopkg.in/yaml.v2 v2.4.0 // indirect
3131
gopkg.in/yaml.v3 v3.0.1 // indirect
3232
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
9696
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
9797
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
9898
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
99-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.17 h1:1WuWJu7/e8SqK+uQl7lfk/N/oMZTL2NE/TJsNKRNMc4=
100-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.17/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
99+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.21.0.20230918151823-4f048611ed7c h1:HM3dPr4NWDAAJDt3mmJGLZ+1SqvQNbRM0zBvBB4UHmU=
100+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.21.0.20230918151823-4f048611ed7c/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
101101
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
102102
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
103103
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

0 commit comments

Comments
 (0)