Skip to content

Automated cherry pick of #999: Add implicit ListVolumesResponse#Entry pagination limit #1011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/gce-pd-csi-driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ const (

replicationTypeNone = "none"
replicationTypeRegionalPD = "regional-pd"

// The maximum number of entries that we can include in the
// ListVolumesResposne
// In reality, the limit here is 4MB (based on gRPC client response limits),
// but 500 is a good proxy (gives ~8KB of data per ListVolumesResponse#Entry)
// See https://github.com/grpc/grpc/blob/master/include/grpc/impl/codegen/grpc_types.h#L503)
maxListVolumesResponseEntries = 500
)

func isDiskReady(disk *gce.CloudDisk) (bool, error) {
Expand Down Expand Up @@ -708,7 +715,7 @@ func (gceCS *GCEControllerServer) ListVolumes(ctx context.Context, req *csi.List

var maxEntries int = int(req.MaxEntries)
if maxEntries == 0 {
maxEntries = len(gceCS.disks)
maxEntries = maxListVolumesResponseEntries
}

entries := []*csi.ListVolumesResponse_Entry{}
Expand Down
76 changes: 73 additions & 3 deletions pkg/gce-pd-csi-driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
package gceGCEDriver

import (
"context"
"fmt"
"math/rand"
"reflect"
Expand All @@ -25,8 +26,6 @@ import (
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
"github.com/golang/protobuf/ptypes"

"context"

compute "google.golang.org/api/compute/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -840,8 +839,79 @@ func TestCreateVolumeArguments(t *testing.T) {
}
}

func TestListVolumePagination(t *testing.T) {
testCases := []struct {
name string
diskCount int
maxEntries int32
expectedEntries []int
}{
{
name: "no pagination (implicit)",
diskCount: 325,
expectedEntries: []int{325},
},
{
name: "no pagination (explicit)",
diskCount: 2500,
maxEntries: 2500,
expectedEntries: []int{2500},
},
{
name: "pagination (implicit)",
diskCount: 1327,
expectedEntries: []int{500, 500, 327},
},
{
name: "pagination (explicit)",
diskCount: 723,
maxEntries: 200,
expectedEntries: []int{200, 200, 200, 123},
},
{
name: "pagination (explicit)",
diskCount: 3253,
maxEntries: 1000,
expectedEntries: []int{1000, 1000, 1000, 253},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Setup new driver each time so no interference
var d []*gce.CloudDisk
for i := 0; i < tc.diskCount; i++ {
// Create diskCount dummy disks
d = append(d, gce.CloudDiskFromV1(&compute.Disk{Name: fmt.Sprintf("%v", i)}))
}
gceDriver := initGCEDriver(t, d)
tok := ""
for i, expectedEntry := range tc.expectedEntries {
lvr := &csi.ListVolumesRequest{
MaxEntries: tc.maxEntries,
StartingToken: tok,
}
resp, err := gceDriver.cs.ListVolumes(context.TODO(), lvr)
if err != nil {
t.Fatalf("Got error %v", err)
return
}

if len(resp.Entries) != expectedEntry {
t.Fatalf("Got %v entries, expected %v on call # %d", len(resp.Entries), expectedEntry, i+1)
}

tok = resp.NextToken
}
if len(tok) != 0 {
t.Fatalf("Expected no more entries, but got NextToken in response: %s", tok)
}
})
}
}

func TestListVolumeArgs(t *testing.T) {
diskCount := 600
diskCount := 500
testCases := []struct {
name string
maxEntries int32
Expand Down