Skip to content

Commit

Permalink
fix: Correctly handle a volumes slice of zero length
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Skazinski <[email protected]>
  • Loading branch information
jskazinski committed May 27, 2022
1 parent 80883af commit 242b3fa
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,24 +330,30 @@ func (client *Client) chooseLUN(initiatorName string) (int, error) {

sort.Sort(Volumes(volumes))

klog.V(5).Infof("checking if next LUN is not above maximum LUNs limit")
klog.V(5).Infof("use LUN 1 when volumes slice is empty")
if len(volumes) == 0 {
return 1, nil
}

klog.V(5).Infof("use the next highest LUN number, until the end is reached")
if volumes[len(volumes)-1].LUN+1 < MaximumLUN {
return volumes[len(volumes)-1].LUN + 1, nil
}

klog.V(5).Infof("checking if LUN 1 is not already in use")
if len(volumes) == 0 || volumes[0].LUN > 1 {
klog.V(5).Infof("use LUN 1 when not in use")
if volumes[0].LUN > 1 {
return 1, nil
}

klog.V(5).Infof("searching for an available LUN between LUNs in use")
klog.V(5).Infof("use the next available LUN, searching from LUN 1 towards the maximum")
for index := 1; index < len(volumes); index++ {
// Find a gap between used LUNs
if volumes[index].LUN-volumes[index-1].LUN > 1 {
return volumes[index-1].LUN + 1, nil
}
}

klog.Errorf("no more available LUNs: [%d] luns=%v", len(volumes), volumes)
klog.Errorf("no available LUN: [%d] luns=%v", len(volumes), volumes)

return -1, status.Error(codes.ResourceExhausted, "no more available LUNs")
}
Expand Down

0 comments on commit 242b3fa

Please sign in to comment.