Skip to content

Commit

Permalink
[cinder-csi-plugin]: fix pagination, avoid unnecessary memory allocat…
Browse files Browse the repository at this point in the history
…ion, add more logs (#2296)

* [cinder]: avoid unnecessary memory allocation with more logs

* Fix pagination parsing
  • Loading branch information
kayrus authored Jul 18, 2023
1 parent a2c86f9 commit 95dc81c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
9 changes: 7 additions & 2 deletions pkg/csi/cinder/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func (cs *controllerServer) ControllerUnpublishVolume(ctx context.Context, req *
}

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

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

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

status := &csi.ListVolumesResponse_VolumeStatus{}
status.PublishedNodeIds = make([]string, 0, len(v.Attachments))
for _, attachment := range v.Attachments {
status.PublishedNodeIds = append(status.PublishedNodeIds, attachment.ServerID)
}
ventry.Status = status

ventries = append(ventries, &ventry)
}

klog.V(4).Infof("ListVolumes: completed with %d entries and %q next token", len(ventries), nextPageToken)
return &csi.ListVolumesResponse{
Entries: ventries,
NextToken: nextPageToken,
Expand Down Expand Up @@ -478,7 +482,7 @@ func (cs *controllerServer) ListSnapshots(ctx context.Context, req *csi.ListSnap
return nil, status.Errorf(codes.Internal, "ListSnapshots failed with error %v", err)
}

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

status := &csi.ControllerGetVolumeResponse_VolumeStatus{}
status.PublishedNodeIds = make([]string, 0, len(volume.Attachments))
for _, attachment := range volume.Attachments {
status.PublishedNodeIds = append(status.PublishedNodeIds, attachment.ServerID)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/csi/cinder/controllerserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ func TestListVolumes(t *testing.T) {
VolumeId: FakeVol3.ID,
CapacityBytes: int64(FakeVol3.Size * 1024 * 1024 * 1024),
},
Status: &csi.ListVolumesResponse_VolumeStatus{},
Status: &csi.ListVolumesResponse_VolumeStatus{
PublishedNodeIds: []string{},
},
},
},
NextToken: "",
Expand Down
4 changes: 2 additions & 2 deletions pkg/csi/cinder/openstack/openstack_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ func (os *OpenStack) ListSnapshots(filters map[string]string) ([]snapshots.Snaps
}

if nextPageURL != "" {
queryParams, err := url.ParseQuery(nextPageURL)
pageURL, err := url.Parse(nextPageURL)
if err != nil {
return false, err
}
nextPageToken = queryParams.Get("marker")
nextPageToken = pageURL.Query().Get("marker")
}

return false, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/csi/cinder/openstack/openstack_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ func (os *OpenStack) ListVolumes(limit int, startingToken string) ([]volumes.Vol
}

if nextPageURL != "" {
queryParams, err := url.ParseQuery(nextPageURL)
pageURL, err := url.Parse(nextPageURL)
if err != nil {
return false, err
}
nextPageToken = queryParams.Get("marker")
nextPageToken = pageURL.Query().Get("marker")
}

return false, nil
Expand Down

0 comments on commit 95dc81c

Please sign in to comment.