Change the length of modelSortings array for panic of index error

If grade number of resourceModel less than rsName, the controller
may panic with index error.
The length of resource model grades array should equl the length
of rsName array.

Signed-off-by: NeilSun <sunbinnnnn@hotmail.com>
This commit is contained in:
NeilSun 2023-05-26 17:22:47 +08:00 committed by NeilSun
parent 218a0b2400
commit efb2ce0a67
2 changed files with 30 additions and 1 deletions

View File

@ -85,7 +85,7 @@ func InitSummary(resourceModels []clusterapis.ResourceModel) (ResourceSummary, e
} }
rs = make(ResourceSummary, len(rsList)) rs = make(ResourceSummary, len(rsList))
// generate a sorted array by first priority of ResourceName // generate a sorted array by first priority of ResourceName
modelSortings = make([][]resource.Quantity, len(rsList)) modelSortings = make([][]resource.Quantity, len(rsName))
for index := 0; index < len(rsList); index++ { for index := 0; index < len(rsList); index++ {
for i, name := range rsName { for i, name := range rsName {
modelSortings[i] = append(modelSortings[i], rsList[index][name]) modelSortings[i] = append(modelSortings[i], rsList[index][name])

View File

@ -96,6 +96,35 @@ func TestInitSummaryError(t *testing.T) {
} }
} }
func TestInitSummaryWithOneGrade(t *testing.T) {
rms := []clusterapis.ResourceModel{
{
Grade: 0,
Ranges: []clusterapis.ResourceModelRange{
{
Name: clusterapis.ResourceCPU,
Min: *resource.NewMilliQuantity(0, resource.DecimalSI),
Max: *resource.NewQuantity(1, resource.DecimalSI),
},
{
Name: clusterapis.ResourceMemory,
Min: *resource.NewMilliQuantity(0, resource.DecimalSI),
Max: *resource.NewQuantity(1024, resource.DecimalSI),
},
},
},
}
rs, err := InitSummary(rms)
if actualValue := len(rs); actualValue != 1 {
t.Errorf("Got %v expected %v", actualValue, 1)
}
if err != nil {
t.Errorf("Got %v expected %v", err, nil)
}
}
func TestSearchLastLessElement(t *testing.T) { func TestSearchLastLessElement(t *testing.T) {
nums, target := []resource.Quantity{ nums, target := []resource.Quantity{
*resource.NewMilliQuantity(1999, resource.DecimalSI), *resource.NewMilliQuantity(1999, resource.DecimalSI),