sort volume ID's for rbd volume list

looping over a map is not guaranteet
to be ordered.
we need to sort the volume ID's for
ListVolume rpc  for rbd plugin.

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
Madhu Rajanna 2019-02-26 18:49:00 +05:30 committed by mergify[bot]
parent e861f12a60
commit 1cedbf0eee

View File

@ -19,6 +19,7 @@ package rbd
import ( import (
"fmt" "fmt"
"os/exec" "os/exec"
"sort"
"strconv" "strconv"
"syscall" "syscall"
@ -278,7 +279,7 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
// ListVolumes returns a list of volumes stored in memory // ListVolumes returns a list of volumes stored in memory
func (cs *ControllerServer) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) { func (cs *ControllerServer) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) {
var startToken int
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_LIST_VOLUMES); err != nil { if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_LIST_VOLUMES); err != nil {
klog.Warningf("invalid list volume req: %v", req) klog.Warningf("invalid list volume req: %v", req)
return nil, err return nil, err
@ -294,16 +295,26 @@ func (cs *ControllerServer) ListVolumes(ctx context.Context, req *csi.ListVolume
if len(rbdVolumes) < int(i) { if len(rbdVolumes) < int(i) {
return nil, status.Errorf(codes.Aborted, "invalid starting token %s", parseErr.Error()) return nil, status.Errorf(codes.Aborted, "invalid starting token %s", parseErr.Error())
} }
startToken = int(i)
} }
var entries []*csi.ListVolumesResponse_Entry var entries []*csi.ListVolumesResponse_Entry
for _, vol := range rbdVolumes { keys := make([]string, 0)
for k := range rbdVolumes {
keys = append(keys, k)
}
sort.Strings(keys)
for index, k := range keys {
if index < startToken {
continue
}
entries = append(entries, &csi.ListVolumesResponse_Entry{ entries = append(entries, &csi.ListVolumesResponse_Entry{
Volume: &csi.Volume{ Volume: &csi.Volume{
VolumeId: vol.VolID, VolumeId: rbdVolumes[k].VolID,
CapacityBytes: vol.VolSize, CapacityBytes: rbdVolumes[k].VolSize,
VolumeContext: extractStoredVolOpt(vol), VolumeContext: extractStoredVolOpt(rbdVolumes[k]),
}, },
}) })
} }