Skip to content

Commit

Permalink
✨ feat: honor maxEntries in ListSnapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
jfbus committed Jan 14, 2025
1 parent f0589e7 commit ebb1bf1
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 37 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ test-e2e-single-az-buildx:
docker buildx build --load -t $(E2E_ENV) -f ./tests/e2e/docker/Dockerfile_e2eTest .
$(MAKE) test-e2e-single-az-run

bin/mockgen: | bin
go get github.com/golang/mock/mockgen@latest
bin/mockgen:
go install github.com/golang/mock/mockgen@latest

mockgen: bin/mockgen
mockgen:
./hack/update-gomock

.PHONY: trivy-scan
Expand Down
11 changes: 6 additions & 5 deletions hack/update-gomock
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ set -euo pipefail

IMPORT_PATH=github.com/outscale/osc-bsu-csi-driver

./bin/mockgen -package=mocks -destination=./pkg/cloud/mocks/mock_ec2.go ${IMPORT_PATH}/pkg/cloud EC2
./bin/mockgen -package=mocks -destination=./pkg/cloud/mocks/mock_ec2metadata.go ${IMPORT_PATH}/pkg/cloud EC2Metadata
./bin/mockgen -package=mocks -destination=./pkg/driver/mocks/mock_cloud.go ${IMPORT_PATH}/pkg/cloud Cloud
./bin/mockgen -package=mocks -destination=./pkg/driver/mocks/mock_metadata_service.go ${IMPORT_PATH}/pkg/cloud MetadataService
./bin/mockgen -package=mocks -destination=./pkg/driver/mocks/mock_mounter.go ${IMPORT_PATH}/pkg/driver Mounter
# mockgen -package=mocks -destination=./pkg/cloud/mocks/mock_ec2.go ${IMPORT_PATH}/pkg/cloud EC2
mockgen -package=mocks -destination=./pkg/cloud/mocks/mock_ec2metadata.go ${IMPORT_PATH}/pkg/cloud EC2Metadata

mockgen -package=mocks -destination=./pkg/driver/mocks/mock_cloud.go ${IMPORT_PATH}/pkg/cloud Cloud
mockgen -package=mocks -destination=./pkg/driver/mocks/mock_metadata_service.go ${IMPORT_PATH}/pkg/cloud MetadataService
mockgen -package=mocks -destination=./pkg/driver/mocks/mock_mounter.go ${IMPORT_PATH}/pkg/driver Mounter

17 changes: 12 additions & 5 deletions pkg/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
osc "github.com/outscale/osc-sdk-go/v2"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
)

// Outscale volume types
Expand Down Expand Up @@ -174,7 +175,7 @@ type Cloud interface {
DeleteSnapshot(ctx context.Context, snapshotID string) (success bool, err error)
GetSnapshotByName(ctx context.Context, name string) (snapshot Snapshot, err error)
GetSnapshotByID(ctx context.Context, snapshotID string) (snapshot Snapshot, err error)
ListSnapshots(ctx context.Context, volumeID string, maxResults int64, nextToken string) (listSnapshotsResponse ListSnapshotsResponse, err error)
ListSnapshots(ctx context.Context, volumeID string, maxResults int32, nextToken string) (listSnapshotsResponse ListSnapshotsResponse, err error)
}

type OscInterface interface {
Expand Down Expand Up @@ -846,25 +847,31 @@ func (c *cloud) GetSnapshotByID(ctx context.Context, snapshotID string) (snapsho
return c.oscSnapshotResponseToStruct(oscsnapshot), nil
}

const maxResultsLimit = 1000

// ListSnapshots retrieves Outscale BSU snapshots for an optionally specified volume ID. If maxResults is set, it will return up to maxResults snapshots. If there are more snapshots than maxResults,
// a next token value will be returned to the client as well. They can use this token with subsequent calls to retrieve the next page of results.
// Pagination not supported
func (c *cloud) ListSnapshots(ctx context.Context, volumeID string, maxResults int64, nextToken string) (listSnapshotsResponse ListSnapshotsResponse, err error) {
request := osc.ReadSnapshotsRequest{
func (c *cloud) ListSnapshots(ctx context.Context, volumeID string, maxResults int32, nextToken string) (listSnapshotsResponse ListSnapshotsResponse, err error) {
if maxResults > maxResultsLimit {
maxResults = maxResultsLimit
}
req := osc.ReadSnapshotsRequest{
Filters: &osc.FiltersSnapshot{
VolumeIds: &[]string{},
},
ResultsPerPage: ptr.To(maxResults),
}

if len(volumeID) != 0 {
request = osc.ReadSnapshotsRequest{
req = osc.ReadSnapshotsRequest{
Filters: &osc.FiltersSnapshot{
VolumeIds: &[]string{volumeID},
},
}
}

resp, err := c.listSnapshots(ctx, request)
resp, err := c.listSnapshots(ctx, req)
if err != nil {
return ListSnapshotsResponse{}, err
}
Expand Down
35 changes: 19 additions & 16 deletions pkg/cloud/mocks/mock_ec2metadata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ func (d *controllerService) ListSnapshots(ctx context.Context, req *csi.ListSnap
nextToken := req.GetStartingToken()
maxEntries := req.GetMaxEntries()

cloudSnapshots, err := d.cloud.ListSnapshots(ctx, volumeID, int64(maxEntries), nextToken)
cloudSnapshots, err := d.cloud.ListSnapshots(ctx, volumeID, maxEntries, nextToken)
if err != nil {
if errors.Is(err, cloud.ErrNotFound) {
klog.FromContext(ctx).V(4).Info("Snapshot does not exist")
Expand Down
6 changes: 3 additions & 3 deletions pkg/driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,7 @@ func TestListSnapshots(t *testing.T) {
defer mockCtl.Finish()

mockCloud := mocks.NewMockCloud(mockCtl)
mockCloud.EXPECT().ListSnapshots(gomock.Eq(ctx), gomock.Eq(""), gomock.Eq(int64(0)), gomock.Eq("")).Return(mockCloudSnapshotsResponse, nil)
mockCloud.EXPECT().ListSnapshots(gomock.Eq(ctx), gomock.Eq(""), gomock.Eq(int32(0)), gomock.Eq("")).Return(mockCloudSnapshotsResponse, nil)

oscDriver := controllerService{
cloud: mockCloud,
Expand All @@ -1630,7 +1630,7 @@ func TestListSnapshots(t *testing.T) {
defer mockCtl.Finish()

mockCloud := mocks.NewMockCloud(mockCtl)
mockCloud.EXPECT().ListSnapshots(gomock.Eq(ctx), gomock.Eq(""), gomock.Eq(int64(0)), gomock.Eq("")).Return(cloud.ListSnapshotsResponse{}, cloud.ErrNotFound)
mockCloud.EXPECT().ListSnapshots(gomock.Eq(ctx), gomock.Eq(""), gomock.Eq(int32(0)), gomock.Eq("")).Return(cloud.ListSnapshotsResponse{}, cloud.ErrNotFound)

oscDriver := controllerService{
cloud: mockCloud,
Expand Down Expand Up @@ -1754,7 +1754,7 @@ func TestListSnapshots(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
mockCloud := mocks.NewMockCloud(mockCtl)
mockCloud.EXPECT().ListSnapshots(gomock.Eq(ctx), gomock.Eq(""), gomock.Eq(int64(4)), gomock.Eq("")).Return(cloud.ListSnapshotsResponse{}, cloud.ErrInvalidMaxResults)
mockCloud.EXPECT().ListSnapshots(gomock.Eq(ctx), gomock.Eq(""), gomock.Eq(int32(4)), gomock.Eq("")).Return(cloud.ListSnapshotsResponse{}, cloud.ErrInvalidMaxResults)

oscDriver := controllerService{
cloud: mockCloud,
Expand Down
2 changes: 1 addition & 1 deletion pkg/driver/mocks/mock_cloud.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/driver/sanity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ type fakeCloudProvider struct {
// snapshots contains mapping from snapshot ID to snapshot
snapshots map[string]*fakeSnapshot
pub map[string]string
tokens map[string]int64
tokens map[string]int32
m *cloud.Metadata
}

Expand All @@ -123,7 +123,7 @@ func newFakeCloudProvider() *fakeCloudProvider {
disks: make(map[string]*fakeDisk),
snapshots: make(map[string]*fakeSnapshot),
pub: make(map[string]string),
tokens: make(map[string]int64),
tokens: make(map[string]int32),
m: &cloud.Metadata{
InstanceID: "instanceID",
Region: "region",
Expand Down Expand Up @@ -264,7 +264,7 @@ func (c *fakeCloudProvider) GetSnapshotByID(ctx context.Context, snapshotID stri
return ret.Snapshot, nil
}

func (c *fakeCloudProvider) ListSnapshots(ctx context.Context, volumeID string, maxResults int64, nextToken string) (listSnapshotsResponse cloud.ListSnapshotsResponse, err error) {
func (c *fakeCloudProvider) ListSnapshots(ctx context.Context, volumeID string, maxResults int32, nextToken string) (listSnapshotsResponse cloud.ListSnapshotsResponse, err error) {
var snapshots []cloud.Snapshot
var retToken string
for _, fakeSnapshot := range c.snapshots {
Expand Down

0 comments on commit ebb1bf1

Please sign in to comment.