Skip to content

Commit b7b30d4

Browse files
committed
Address review feedback from raywainman
1 parent 3314cf9 commit b7b30d4

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()
@@ -227,14 +229,7 @@ func run(healthCheck *metrics.HealthCheck, commonFlag *common.CommonFlags) {
227229
postProcessors = append(postProcessors, &routines.IntegerCPUPostProcessor{})
228230
}
229231

230-
var globalMaxAllowed apiv1.ResourceList
231-
if !maxAllowedCPU.Quantity.IsZero() {
232-
setGlobalMaxAllowed(&globalMaxAllowed, apiv1.ResourceCPU, maxAllowedCPU.Quantity)
233-
}
234-
if !maxAllowedMemory.Quantity.IsZero() {
235-
setGlobalMaxAllowed(&globalMaxAllowed, apiv1.ResourceMemory, maxAllowedMemory.Quantity)
236-
}
237-
232+
globalMaxAllowed := initGlobalMaxAllowed()
238233
// CappingPostProcessor, should always come in the last position for post-processing
239234
postProcessors = append(postProcessors, routines.NewCappingRecommendationProcessor(globalMaxAllowed))
240235
var source input_metrics.PodMetricsLister
@@ -331,10 +326,14 @@ func run(healthCheck *metrics.HealthCheck, commonFlag *common.CommonFlags) {
331326
}
332327
}
333328

334-
func setGlobalMaxAllowed(globalMaxAllowed *apiv1.ResourceList, key apiv1.ResourceName, value resource.Quantity) {
335-
if *globalMaxAllowed == nil {
336-
*globalMaxAllowed = make(map[apiv1.ResourceName]resource.Quantity, 2)
329+
func initGlobalMaxAllowed() apiv1.ResourceList {
330+
result := make(apiv1.ResourceList)
331+
if !maxAllowedCPU.Quantity.IsZero() {
332+
result[apiv1.ResourceCPU] = maxAllowedCPU.Quantity
333+
}
334+
if !maxAllowedMemory.Quantity.IsZero() {
335+
result[apiv1.ResourceMemory] = maxAllowedMemory.Quantity
337336
}
338337

339-
(*globalMaxAllowed)[key] = value
338+
return result
340339
}

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

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

207-
if globalMaxAllowed != nil {
208-
if maxAllowed == nil {
209-
maxAllowed = globalMaxAllowed
210-
} else {
211-
// Set resources from the global maxAllowed if the VPA maxAllowed is missing them.
212-
for resourceName, quantity := range globalMaxAllowed {
213-
if _, ok := maxAllowed[resourceName]; !ok {
214-
maxAllowed[resourceName] = quantity
215-
}
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
216214
}
217215
}
218216
}

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

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

0 commit comments

Comments
 (0)