From 35bfee289ea32efc0679e70a020e9afbc2ca0448 Mon Sep 17 00:00:00 2001 From: Jeremy THERIN Date: Tue, 3 Oct 2023 15:46:12 +0200 Subject: [PATCH] fix: filter snapshot on api side --- driver/controller.go | 67 ++++++++++++++++++++++++-------------------- go.mod | 4 +-- go.sum | 4 +-- 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/driver/controller.go b/driver/controller.go index 8663b2e..bef1a63 100644 --- a/driver/controller.go +++ b/driver/controller.go @@ -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" ) @@ -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()) } @@ -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{ @@ -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{ @@ -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 := "" @@ -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{ diff --git a/go.mod b/go.mod index e719ac5..ebc1198 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 ) diff --git a/go.sum b/go.sum index d9263f4..900de1b 100644 --- a/go.sum +++ b/go.sum @@ -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=