From efb2ce0a678bc0cd3c53e7d125476fd04ee92fbe Mon Sep 17 00:00:00 2001 From: NeilSun Date: Fri, 26 May 2023 17:22:47 +0800 Subject: [PATCH] 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 --- pkg/modeling/modeling.go | 2 +- pkg/modeling/modeling_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pkg/modeling/modeling.go b/pkg/modeling/modeling.go index 8c9ddb070..71eabeb2a 100644 --- a/pkg/modeling/modeling.go +++ b/pkg/modeling/modeling.go @@ -85,7 +85,7 @@ func InitSummary(resourceModels []clusterapis.ResourceModel) (ResourceSummary, e } rs = make(ResourceSummary, len(rsList)) // 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 i, name := range rsName { modelSortings[i] = append(modelSortings[i], rsList[index][name]) diff --git a/pkg/modeling/modeling_test.go b/pkg/modeling/modeling_test.go index d97a32f96..47afcb74a 100644 --- a/pkg/modeling/modeling_test.go +++ b/pkg/modeling/modeling_test.go @@ -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) { nums, target := []resource.Quantity{ *resource.NewMilliQuantity(1999, resource.DecimalSI),