Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit feff161

Browse files
committed
Add limit request ratio validation
1 parent 0562d8e commit feff161

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

pkg/command/recommendation/options.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ func Finish(cmd *cobra.Command, appAPI applications.API, app applications.Applic
387387
}
388388
return &applications.BoundsRange{}
389389
}
390+
limitRequestRatio := patch.Configuration[0].ContainerResources.LimitRequestRatio
390391

391392
errs = append(errs, checkResourceList(
392393
mode, "limit",
@@ -405,6 +406,11 @@ func Finish(cmd *cobra.Command, appAPI applications.API, app applications.Applic
405406
targetUtilization(bounds).Min, targetUtilization(bounds).Max,
406407
cmd.CommandPath(), flagContainerResourcesTargetUtilizationMin, flagContainerResourcesTargetUtilizationMax,
407408
)...)
409+
410+
errs = append(errs, checkLimitRequestRatio(
411+
limitRequestRatio,
412+
cmd.CommandPath(), flagContainerResourcesLimitRequestRatio,
413+
)...)
408414
}
409415

410416
// Application resources are required to enable recommendations
@@ -499,6 +505,28 @@ func checkResourceList(mode applications.RecommendationsMode, name string, minLi
499505
return errs
500506
}
501507

508+
// checkLimitRequestRatio ensures the ratio does not produce invalid results.
509+
func checkLimitRequestRatio(list *applications.ResourceList, fixCommand, fixFlag string) ErrorList {
510+
var errs ErrorList
511+
512+
for _, resourceName := range []string{"cpu", "memory"} {
513+
r := list.Get(resourceName)
514+
if r == nil {
515+
continue
516+
}
517+
518+
if f := r.Float64Value(); (f > 0 && f < 1) || f < 0 {
519+
errs = append(errs, &Error{
520+
Message: fmt.Sprintf("invalid limit/request ratio for %s: %g (must be 0 or at least 1)", resourceName, f),
521+
FixCommand: fixCommand,
522+
FixFlag: fixFlag,
523+
})
524+
}
525+
}
526+
527+
return errs
528+
}
529+
502530
type Error struct {
503531
Message string
504532
FixCommand string

0 commit comments

Comments
 (0)