Skip to content

Commit 95dc81c

Browse files
authored
[cinder-csi-plugin]: fix pagination, avoid unnecessary memory allocation, add more logs (#2296)
* [cinder]: avoid unnecessary memory allocation with more logs * Fix pagination parsing
1 parent a2c86f9 commit 95dc81c

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

pkg/csi/cinder/controllerserver.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ func (cs *controllerServer) ControllerUnpublishVolume(ctx context.Context, req *
280280
}
281281

282282
func (cs *controllerServer) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) {
283+
klog.V(4).Infof("ListVolumes: called with %+#v request", req)
283284

284285
if req.MaxEntries < 0 {
285286
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf(
@@ -296,7 +297,7 @@ func (cs *controllerServer) ListVolumes(ctx context.Context, req *csi.ListVolume
296297
return nil, status.Error(codes.Internal, fmt.Sprintf("ListVolumes failed with error %v", err))
297298
}
298299

299-
var ventries []*csi.ListVolumesResponse_Entry
300+
ventries := make([]*csi.ListVolumesResponse_Entry, 0, len(vlist))
300301
for _, v := range vlist {
301302
ventry := csi.ListVolumesResponse_Entry{
302303
Volume: &csi.Volume{
@@ -306,13 +307,16 @@ func (cs *controllerServer) ListVolumes(ctx context.Context, req *csi.ListVolume
306307
}
307308

308309
status := &csi.ListVolumesResponse_VolumeStatus{}
310+
status.PublishedNodeIds = make([]string, 0, len(v.Attachments))
309311
for _, attachment := range v.Attachments {
310312
status.PublishedNodeIds = append(status.PublishedNodeIds, attachment.ServerID)
311313
}
312314
ventry.Status = status
313315

314316
ventries = append(ventries, &ventry)
315317
}
318+
319+
klog.V(4).Infof("ListVolumes: completed with %d entries and %q next token", len(ventries), nextPageToken)
316320
return &csi.ListVolumesResponse{
317321
Entries: ventries,
318322
NextToken: nextPageToken,
@@ -478,7 +482,7 @@ func (cs *controllerServer) ListSnapshots(ctx context.Context, req *csi.ListSnap
478482
return nil, status.Errorf(codes.Internal, "ListSnapshots failed with error %v", err)
479483
}
480484

481-
var sentries []*csi.ListSnapshotsResponse_Entry
485+
sentries := make([]*csi.ListSnapshotsResponse_Entry, 0, len(slist))
482486
for _, v := range slist {
483487
ctime := timestamppb.New(v.CreatedAt)
484488
if err := ctime.CheckValid(); err != nil {
@@ -582,6 +586,7 @@ func (cs *controllerServer) ControllerGetVolume(ctx context.Context, req *csi.Co
582586
}
583587

584588
status := &csi.ControllerGetVolumeResponse_VolumeStatus{}
589+
status.PublishedNodeIds = make([]string, 0, len(volume.Attachments))
585590
for _, attachment := range volume.Attachments {
586591
status.PublishedNodeIds = append(status.PublishedNodeIds, attachment.ServerID)
587592
}

pkg/csi/cinder/controllerserver_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,9 @@ func TestListVolumes(t *testing.T) {
441441
VolumeId: FakeVol3.ID,
442442
CapacityBytes: int64(FakeVol3.Size * 1024 * 1024 * 1024),
443443
},
444-
Status: &csi.ListVolumesResponse_VolumeStatus{},
444+
Status: &csi.ListVolumesResponse_VolumeStatus{
445+
PublishedNodeIds: []string{},
446+
},
445447
},
446448
},
447449
NextToken: "",

pkg/csi/cinder/openstack/openstack_snapshots.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ func (os *OpenStack) ListSnapshots(filters map[string]string) ([]snapshots.Snaps
119119
}
120120

121121
if nextPageURL != "" {
122-
queryParams, err := url.ParseQuery(nextPageURL)
122+
pageURL, err := url.Parse(nextPageURL)
123123
if err != nil {
124124
return false, err
125125
}
126-
nextPageToken = queryParams.Get("marker")
126+
nextPageToken = pageURL.Query().Get("marker")
127127
}
128128

129129
return false, nil

pkg/csi/cinder/openstack/openstack_volumes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ func (os *OpenStack) ListVolumes(limit int, startingToken string) ([]volumes.Vol
9696
}
9797

9898
if nextPageURL != "" {
99-
queryParams, err := url.ParseQuery(nextPageURL)
99+
pageURL, err := url.Parse(nextPageURL)
100100
if err != nil {
101101
return false, err
102102
}
103-
nextPageToken = queryParams.Get("marker")
103+
nextPageToken = pageURL.Query().Get("marker")
104104
}
105105

106106
return false, nil

0 commit comments

Comments
 (0)