Skip to content

Commit caa47f9

Browse files
committed
Address review feedback from raywainman
1 parent 04e3340 commit caa47f9

File tree

3 files changed

+60
-23
lines changed

3 files changed

+60
-23
lines changed

vertical-pod-autoscaler/pkg/recommender/main.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,12 @@ const (
118118
defaultResyncPeriod time.Duration = 10 * time.Minute
119119
)
120120

121-
func main() {
121+
func init() {
122122
flag.Var(&maxAllowedCPU, "container-recommendation-max-allowed-cpu", "Maximum amount of CPU that will be recommended for a container. VerticalPodAutoscaler-level maximum allowed takes precedence over the global maximum allowed.")
123123
flag.Var(&maxAllowedMemory, "container-recommendation-max-allowed-memory", "Maximum amount of memory that will be recommended for a container. VerticalPodAutoscaler-level maximum allowed takes precedence over the global maximum allowed.")
124+
}
124125

126+
func main() {
125127
commonFlags := common.InitCommonFlags()
126128
klog.InitFlags(nil)
127129
common.InitLoggingFlags()
@@ -221,14 +223,7 @@ func run(healthCheck *metrics.HealthCheck, commonFlag *common.CommonFlags) {
221223
postProcessors = append(postProcessors, &routines.IntegerCPUPostProcessor{})
222224
}
223225

224-
var globalMaxAllowed apiv1.ResourceList
225-
if !maxAllowedCPU.Quantity.IsZero() {
226-
setGlobalMaxAllowed(&globalMaxAllowed, apiv1.ResourceCPU, maxAllowedCPU.Quantity)
227-
}
228-
if !maxAllowedMemory.Quantity.IsZero() {
229-
setGlobalMaxAllowed(&globalMaxAllowed, apiv1.ResourceMemory, maxAllowedMemory.Quantity)
230-
}
231-
226+
globalMaxAllowed := initGlobalMaxAllowed()
232227
// CappingPostProcessor, should always come in the last position for post-processing
233228
postProcessors = append(postProcessors, routines.NewCappingRecommendationProcessor(globalMaxAllowed))
234229
var source input_metrics.PodMetricsLister
@@ -322,10 +317,14 @@ func run(healthCheck *metrics.HealthCheck, commonFlag *common.CommonFlags) {
322317
}
323318
}
324319

325-
func setGlobalMaxAllowed(globalMaxAllowed *apiv1.ResourceList, key apiv1.ResourceName, value resource.Quantity) {
326-
if *globalMaxAllowed == nil {
327-
*globalMaxAllowed = make(map[apiv1.ResourceName]resource.Quantity, 2)
320+
func initGlobalMaxAllowed() apiv1.ResourceList {
321+
result := make(apiv1.ResourceList)
322+
if !maxAllowedCPU.Quantity.IsZero() {
323+
result[apiv1.ResourceCPU] = maxAllowedCPU.Quantity
324+
}
325+
if !maxAllowedMemory.Quantity.IsZero() {
326+
result[apiv1.ResourceMemory] = maxAllowedMemory.Quantity
328327
}
329328

330-
(*globalMaxAllowed)[key] = value
329+
return result
331330
}

vertical-pod-autoscaler/pkg/utils/vpa/capping.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,13 @@ func applyVPAPolicyForContainer(containerName string,
203203
maxAllowed = containerPolicy.MaxAllowed
204204
}
205205

206-
if globalMaxAllowed != nil {
207-
if maxAllowed == nil {
208-
maxAllowed = globalMaxAllowed
209-
} else {
210-
// Set resources from the global maxAllowed if the VPA maxAllowed is missing them.
211-
for resourceName, quantity := range globalMaxAllowed {
212-
if _, ok := maxAllowed[resourceName]; !ok {
213-
maxAllowed[resourceName] = quantity
214-
}
206+
if maxAllowed == nil {
207+
maxAllowed = globalMaxAllowed
208+
} else {
209+
// Set resources from the global maxAllowed if the VPA maxAllowed is missing them.
210+
for resourceName, quantity := range globalMaxAllowed {
211+
if _, ok := maxAllowed[resourceName]; !ok {
212+
maxAllowed[resourceName] = quantity
215213
}
216214
}
217215
}

vertical-pod-autoscaler/pkg/utils/vpa/capping_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ func TestApplyVPAPolicy(t *testing.T) {
410410
},
411411
},
412412
{
413-
Name: "resource policy is nil and global max allowed is set",
413+
Name: "resource policy is nil and global max allowed is set for cpu and memory",
414414
PodRecommendation: recommendation,
415415
ResourcePolicy: nil,
416416
GlobalMaxAllowed: apiv1.ResourceList{
@@ -483,6 +483,46 @@ func TestApplyVPAPolicy(t *testing.T) {
483483
},
484484
},
485485
},
486+
{
487+
Name: "resource policy has max allowed for cpu and global max allowed is set for memory",
488+
PodRecommendation: recommendation,
489+
ResourcePolicy: &vpa_types.PodResourcePolicy{
490+
ContainerPolicies: []vpa_types.ContainerResourcePolicy{
491+
{
492+
ContainerName: "foo",
493+
MaxAllowed: apiv1.ResourceList{
494+
apiv1.ResourceCPU: resource.MustParse("40m"),
495+
},
496+
},
497+
},
498+
},
499+
GlobalMaxAllowed: apiv1.ResourceList{
500+
apiv1.ResourceMemory: resource.MustParse("40Mi"),
501+
},
502+
Expected: &vpa_types.RecommendedPodResources{
503+
ContainerRecommendations: []vpa_types.RecommendedContainerResources{
504+
{
505+
ContainerName: "foo",
506+
Target: apiv1.ResourceList{
507+
apiv1.ResourceCPU: resource.MustParse("40m"),
508+
apiv1.ResourceMemory: resource.MustParse("40Mi"),
509+
},
510+
LowerBound: apiv1.ResourceList{
511+
apiv1.ResourceCPU: resource.MustParse("31m"),
512+
apiv1.ResourceMemory: resource.MustParse("31Mi"),
513+
},
514+
UpperBound: apiv1.ResourceList{
515+
apiv1.ResourceCPU: resource.MustParse("40m"),
516+
apiv1.ResourceMemory: resource.MustParse("40Mi"),
517+
},
518+
UncappedTarget: apiv1.ResourceList{
519+
apiv1.ResourceCPU: resource.MustParse("42m"),
520+
apiv1.ResourceMemory: resource.MustParse("42Mi"),
521+
},
522+
},
523+
},
524+
},
525+
},
486526
{
487527
Name: "resource policy has max allowed for cpu and global max allowed is set for cpu and memory",
488528
PodRecommendation: recommendation,

0 commit comments

Comments
 (0)