fix validateFunctionShape nil pointer if RequestedToCapacityRatio not set

Signed-off-by: bin <bin.pan@daocloud.io>
This commit is contained in:
bin
2025-06-05 20:47:53 +08:00
parent 7f50c56b1a
commit 786f4e9c20
2 changed files with 35 additions and 11 deletions

View File

@@ -317,9 +317,17 @@ func ValidateNodeResourcesFitArgs(path *field.Path, args *config.NodeResourcesFi
allErrs = append(allErrs, field.NotSupported(strategyPath.Child("type"), args.ScoringStrategy.Type, sets.List(supportedScoringStrategyTypes)))
}
allErrs = append(allErrs, validateResources(args.ScoringStrategy.Resources, strategyPath.Child("resources"))...)
if args.ScoringStrategy.RequestedToCapacityRatio != nil {
allErrs = append(allErrs, validateFunctionShape(args.ScoringStrategy.RequestedToCapacityRatio.Shape, strategyPath.Child("shape"))...)
if args.ScoringStrategy.Type == config.RequestedToCapacityRatio {
if args.ScoringStrategy.RequestedToCapacityRatio == nil {
allErrs = append(allErrs, field.Required(strategyPath.Child("requestedToCapacityRatio"), "must be specified when type is RequestedToCapacityRatio"))
} else {
allErrs = append(allErrs, validateFunctionShape(args.ScoringStrategy.RequestedToCapacityRatio.Shape, strategyPath.Child("requestedToCapacityRatio").Child("shape"))...)
}
} else if args.ScoringStrategy.RequestedToCapacityRatio != nil {
allErrs = append(allErrs, field.Forbidden(strategyPath.Child("requestedToCapacityRatio"), "must be nil when type is not RequestedToCapacityRatio"))
}
} else {
allErrs = append(allErrs, field.Required(strategyPath, "ScoringStrategy field is required"))
}
if len(allErrs) == 0 {

View File

@@ -754,12 +754,28 @@ func TestValidateFitArgs(t *testing.T) {
},
expect: `Unsupported value: "Invalid"`,
},
{
name: "ScoringStrategy: requestedToCapacityRatio field is missing",
args: config.NodeResourcesFitArgs{
ScoringStrategy: &config.ScoringStrategy{
Type: config.RequestedToCapacityRatio,
},
},
expect: "must be specified when type is RequestedToCapacityRatio",
},
}
for _, test := range argsTest {
t.Run(test.name, func(t *testing.T) {
if err := ValidateNodeResourcesFitArgs(nil, &test.args); err != nil && (!strings.Contains(err.Error(), test.expect)) {
t.Errorf("case[%v]: error details do not include %v", test.name, err)
err := ValidateNodeResourcesFitArgs(nil, &test.args)
if err != nil {
if test.expect == "" {
t.Errorf("case[%v]: unexpected validation error %v", test.name, err)
} else if !strings.Contains(err.Error(), test.expect) {
t.Errorf("case[%v]: error details do not include %v", test.name, err)
}
} else if test.expect != "" {
t.Errorf("case[%v]: expected validation error", test.name)
}
})
}
@@ -974,7 +990,7 @@ func TestValidateRequestedToCapacityRatioScoringStrategy(t *testing.T) {
wantErrs: field.ErrorList{
{
Type: field.ErrorTypeRequired,
Field: "scoringStrategy.shape",
Field: "scoringStrategy.requestedToCapacityRatio.shape",
},
},
},
@@ -1026,7 +1042,7 @@ func TestValidateRequestedToCapacityRatioScoringStrategy(t *testing.T) {
wantErrs: field.ErrorList{
{
Type: field.ErrorTypeInvalid,
Field: "scoringStrategy.shape[0].utilization",
Field: "scoringStrategy.requestedToCapacityRatio.shape[0].utilization",
},
},
},
@@ -1041,7 +1057,7 @@ func TestValidateRequestedToCapacityRatioScoringStrategy(t *testing.T) {
wantErrs: field.ErrorList{
{
Type: field.ErrorTypeInvalid,
Field: "scoringStrategy.shape[0].utilization",
Field: "scoringStrategy.requestedToCapacityRatio.shape[0].utilization",
},
},
},
@@ -1060,7 +1076,7 @@ func TestValidateRequestedToCapacityRatioScoringStrategy(t *testing.T) {
wantErrs: field.ErrorList{
{
Type: field.ErrorTypeInvalid,
Field: "scoringStrategy.shape[1].utilization",
Field: "scoringStrategy.requestedToCapacityRatio.shape[1].utilization",
},
},
},
@@ -1101,7 +1117,7 @@ func TestValidateRequestedToCapacityRatioScoringStrategy(t *testing.T) {
wantErrs: field.ErrorList{
{
Type: field.ErrorTypeInvalid,
Field: "scoringStrategy.shape[2].utilization",
Field: "scoringStrategy.requestedToCapacityRatio.shape[2].utilization",
},
},
},
@@ -1116,7 +1132,7 @@ func TestValidateRequestedToCapacityRatioScoringStrategy(t *testing.T) {
wantErrs: field.ErrorList{
{
Type: field.ErrorTypeInvalid,
Field: "scoringStrategy.shape[0].score",
Field: "scoringStrategy.requestedToCapacityRatio.shape[0].score",
},
},
},
@@ -1131,7 +1147,7 @@ func TestValidateRequestedToCapacityRatioScoringStrategy(t *testing.T) {
wantErrs: field.ErrorList{
{
Type: field.ErrorTypeInvalid,
Field: "scoringStrategy.shape[0].score",
Field: "scoringStrategy.requestedToCapacityRatio.shape[0].score",
},
},
},