Skip to content

Commit

Permalink
fix: filter snapshot on api side
Browse files Browse the repository at this point in the history
  • Loading branch information
jtherin committed Oct 3, 2023
1 parent bfcef14 commit 35bfee2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
67 changes: 36 additions & 31 deletions driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"sync"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/protobuf/ptypes"
"github.com/scaleway/scaleway-csi/scaleway"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
"k8s.io/klog/v2"
)

Expand Down Expand Up @@ -506,7 +506,7 @@ func (d *controllerService) ListVolumes(ctx context.Context, req *csi.ListVolume
}
}

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

if snapshot.CreationDate != nil {
creationTime, err := ptypes.TimestampProto(*snapshot.CreationDate)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
snapshotResp.CreationTime = creationTime

snapshotResp.CreationTime = timestamppb.New(*snapshot.CreationDate)
}

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

if snapshotResp.Snapshot.CreationDate != nil {
creationTime, err := ptypes.TimestampProto(*snapshotResp.Snapshot.CreationDate)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
snapshotProtoResp.CreationTime = creationTime
snapshotProtoResp.CreationTime = timestamppb.New(*snapshotResp.Snapshot.CreationDate)
}

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

// TODO fix zones
snapshotID, _, _ := getSnapshotIDAndZone(req.GetSnapshotId())
sourceVolumeID, _, _ := getSourceVolumeIDAndZone(req.GetSourceVolumeId())
snapshotID, snapshotZone, _ := getSnapshotIDAndZone(req.GetSnapshotId())
sourceVolumeID, sourceVolumeZone, _ := getSourceVolumeIDAndZone(req.GetSourceVolumeId())

// TODO all zones
snapshotsResp, err := d.scaleway.ListSnapshots(&instance.ListSnapshotsRequest{}, scw.WithAllPages())
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
snapshots := []*instance.Snapshot{}
for _, snap := range snapshotsResp.Snapshots {
if snapshotID != "" && snap.ID == snapshotID {
snapshots = []*instance.Snapshot{snap}
break
switch {
case req.SnapshotId != "":
snapshotResp, err := d.scaleway.GetSnapshot(&instance.GetSnapshotRequest{
SnapshotID: snapshotID,
Zone: snapshotZone,
}, scw.WithContext(ctx))
if err != nil {
// not found should return empty list
if _, ok := err.(*scw.ResourceNotFoundError); ok {
return &csi.ListSnapshotsResponse{
Entries: []*csi.ListSnapshotsResponse_Entry{},
}, nil
}
return nil, status.Error(codes.Internal, err.Error())
}
snapshots = []*instance.Snapshot{snapshotResp.Snapshot}
case sourceVolumeID != "":
snapshotsResp, err := d.scaleway.ListSnapshots(&instance.ListSnapshotsRequest{
BaseVolumeID: &sourceVolumeID,
Zone: sourceVolumeZone,
}, scw.WithContext(ctx), scw.WithAllPages())
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if sourceVolumeID != "" && snap.BaseVolume != nil && snap.BaseVolume.ID == sourceVolumeID || snapshotID == "" && sourceVolumeID == "" {
snapshots = append(snapshots, snap)
snapshots = snapshotsResp.Snapshots
default:
snapshotsResp, err := d.scaleway.ListSnapshots(&instance.ListSnapshotsRequest{}, scw.WithContext(ctx), scw.WithAllPages())
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
snapshots = snapshotsResp.Snapshots
}

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

if snap.CreationDate != nil {
creationTime, err := ptypes.TimestampProto(*snap.CreationDate)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
snapshotProtoResp.CreationTime = creationTime
snapshotProtoResp.CreationTime = timestamppb.New(*snap.CreationDate)
}

snapshotsEntries = append(snapshotsEntries, &csi.ListSnapshotsResponse_Entry{
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ require (
github.com/golang/protobuf v1.5.3
github.com/google/uuid v1.3.0
github.com/kubernetes-csi/csi-test/v5 v5.0.0
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.17
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.21.0.20230918151823-4f048611ed7c
golang.org/x/sys v0.9.0
google.golang.org/grpc v1.56.1
google.golang.org/protobuf v1.30.0
k8s.io/klog/v2 v2.100.1
k8s.io/mount-utils v0.27.3
k8s.io/utils v0.0.0-20230505201702-9f6742963106
Expand All @@ -26,7 +27,6 @@ require (
golang.org/x/net v0.9.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.17 h1:1WuWJu7/e8SqK+uQl7lfk/N/oMZTL2NE/TJsNKRNMc4=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.17/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.21.0.20230918151823-4f048611ed7c h1:HM3dPr4NWDAAJDt3mmJGLZ+1SqvQNbRM0zBvBB4UHmU=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.21.0.20230918151823-4f048611ed7c/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down

0 comments on commit 35bfee2

Please sign in to comment.