From 242b3fa540319d8ec1d21200d0a390a5fed275ca Mon Sep 17 00:00:00 2001 From: Joe Skazinski Date: Fri, 27 May 2022 08:39:15 -0600 Subject: [PATCH] fix: Correctly handle a volumes slice of zero length Signed-off-by: Joe Skazinski --- api.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/api.go b/api.go index 35d80fd..8b63b3b 100644 --- a/api.go +++ b/api.go @@ -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") }