-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vpa-recommender: Add support for configuring global max allowed resources #7560
base: master
Are you sure you want to change the base?
Changes from 3 commits
a4ec949
dd5add6
04e3340
caa47f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ import ( | |
|
||
"github.com/spf13/pflag" | ||
apiv1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/uuid" | ||
"k8s.io/client-go/informers" | ||
|
@@ -103,6 +104,8 @@ var ( | |
var ( | ||
// CPU as integer to benefit for CPU management Static Policy ( https://kubernetes.io/docs/tasks/administer-cluster/cpu-management-policies/#static-policy ) | ||
postProcessorCPUasInteger = flag.Bool("cpu-integer-post-processor-enabled", false, "Enable the cpu-integer recommendation post processor. The post processor will round up CPU recommendations to a whole CPU for pods which were opted in by setting an appropriate label on VPA object (experimental)") | ||
maxAllowedCPU = resource.QuantityValue{} | ||
maxAllowedMemory = resource.QuantityValue{} | ||
) | ||
|
||
const ( | ||
|
@@ -116,6 +119,9 @@ const ( | |
) | ||
|
||
func main() { | ||
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.") | ||
ialidzhikov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.") | ||
|
||
commonFlags := common.InitCommonFlags() | ||
klog.InitFlags(nil) | ||
common.InitLoggingFlags() | ||
|
@@ -215,8 +221,16 @@ func run(healthCheck *metrics.HealthCheck, commonFlag *common.CommonFlags) { | |
postProcessors = append(postProcessors, &routines.IntegerCPUPostProcessor{}) | ||
} | ||
|
||
var globalMaxAllowed apiv1.ResourceList | ||
if !maxAllowedCPU.Quantity.IsZero() { | ||
setGlobalMaxAllowed(&globalMaxAllowed, apiv1.ResourceCPU, maxAllowedCPU.Quantity) | ||
} | ||
if !maxAllowedMemory.Quantity.IsZero() { | ||
setGlobalMaxAllowed(&globalMaxAllowed, apiv1.ResourceMemory, maxAllowedMemory.Quantity) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should there be some logic to ensure that the global max is greater than the global min? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a perfect world, I agree that validation should exists. In #7147 (comment) I suggested to deprecate the global Pod-level min allowed flags because:
If you agree, I can open a dedicated issue for deprecated the global Pod-level min allowed flags and introduce new container-level min allowed equivalents. And validation can be added between the global container-level min and max allowed flags. WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, I agree. I can't figure out a way to make the validation work.
I don't think deprecation is necessary yet. Pod level resources may be in the VPA's future, so those flags may be used for Pod level resources. |
||
} | ||
|
||
// CappingPostProcessor, should always come in the last position for post-processing | ||
postProcessors = append(postProcessors, &routines.CappingPostProcessor{}) | ||
postProcessors = append(postProcessors, routines.NewCappingRecommendationProcessor(globalMaxAllowed)) | ||
var source input_metrics.PodMetricsLister | ||
if *useExternalMetrics { | ||
resourceMetrics := map[apiv1.ResourceName]string{} | ||
|
@@ -307,3 +321,11 @@ func run(healthCheck *metrics.HealthCheck, commonFlag *common.CommonFlags) { | |
healthCheck.UpdateLastActivity() | ||
} | ||
} | ||
|
||
func setGlobalMaxAllowed(globalMaxAllowed *apiv1.ResourceList, key apiv1.ResourceName, value resource.Quantity) { | ||
ialidzhikov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if *globalMaxAllowed == nil { | ||
*globalMaxAllowed = make(map[apiv1.ResourceName]resource.Quantity, 2) | ||
} | ||
|
||
(*globalMaxAllowed)[key] = value | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -184,22 +184,39 @@ func applyVPAPolicy(recommendation apiv1.ResourceList, policy *vpa_types.Contain | |||
|
||||
func applyVPAPolicyForContainer(containerName string, | ||||
containerRecommendation *vpa_types.RecommendedContainerResources, | ||||
policy *vpa_types.PodResourcePolicy) (*vpa_types.RecommendedContainerResources, error) { | ||||
policy *vpa_types.PodResourcePolicy, | ||||
globalMaxAllowed apiv1.ResourceList) (*vpa_types.RecommendedContainerResources, error) { | ||||
if containerRecommendation == nil { | ||||
return nil, fmt.Errorf("no recommendation available for container name %v", containerName) | ||||
} | ||||
cappedRecommendations := containerRecommendation.DeepCopy() | ||||
// containerPolicy can be nil (user does not have to configure it). | ||||
containerPolicy := GetContainerResourcePolicy(containerName, policy) | ||||
if containerPolicy == nil { | ||||
return cappedRecommendations, nil | ||||
} | ||||
|
||||
process := func(recommendation apiv1.ResourceList) { | ||||
for resourceName, recommended := range recommendation { | ||||
cappedToMin, _ := maybeCapToPolicyMin(recommended, resourceName, containerPolicy) | ||||
recommendation[resourceName] = cappedToMin | ||||
cappedToMax, _ := maybeCapToPolicyMax(cappedToMin, resourceName, containerPolicy) | ||||
var maxAllowed apiv1.ResourceList | ||||
// containerPolicy can be nil (user does not have to configure it). | ||||
if containerPolicy != nil { | ||||
cappedToMin, _ := maybeCapToPolicyMin(recommended, resourceName, containerPolicy) | ||||
recommendation[resourceName] = cappedToMin | ||||
|
||||
maxAllowed = containerPolicy.MaxAllowed | ||||
} | ||||
|
||||
if globalMaxAllowed != nil { | ||||
ialidzhikov marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
if maxAllowed == nil { | ||||
maxAllowed = globalMaxAllowed | ||||
} else { | ||||
// Set resources from the global maxAllowed if the VPA maxAllowed is missing them. | ||||
for resourceName, quantity := range globalMaxAllowed { | ||||
if _, ok := maxAllowed[resourceName]; !ok { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a comment here that we only override this if the user did not explicitly set a maximum in their container policy in the VPA. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is already
Let me know if you have suggestions for improvements. |
||||
maxAllowed[resourceName] = quantity | ||||
} | ||||
} | ||||
} | ||||
} | ||||
|
||||
cappedToMax, _ := maybeCapToMax(recommendation[resourceName], resourceName, maxAllowed) | ||||
recommendation[resourceName] = cappedToMax | ||||
} | ||||
} | ||||
|
@@ -241,19 +258,16 @@ func maybeCapToMin(recommended resource.Quantity, resourceName apiv1.ResourceNam | |||
|
||||
// ApplyVPAPolicy returns a recommendation, adjusted to obey policy. | ||||
func ApplyVPAPolicy(podRecommendation *vpa_types.RecommendedPodResources, | ||||
policy *vpa_types.PodResourcePolicy) (*vpa_types.RecommendedPodResources, error) { | ||||
policy *vpa_types.PodResourcePolicy, globalMaxAllowed apiv1.ResourceList) (*vpa_types.RecommendedPodResources, error) { | ||||
if podRecommendation == nil { | ||||
return nil, nil | ||||
} | ||||
if policy == nil { | ||||
return podRecommendation, nil | ||||
} | ||||
|
||||
updatedRecommendations := []vpa_types.RecommendedContainerResources{} | ||||
for _, containerRecommendation := range podRecommendation.ContainerRecommendations { | ||||
containerName := containerRecommendation.ContainerName | ||||
updatedContainerResources, err := applyVPAPolicyForContainer(containerName, | ||||
&containerRecommendation, policy) | ||||
&containerRecommendation, policy, globalMaxAllowed) | ||||
if err != nil { | ||||
return nil, fmt.Errorf("cannot apply policy on recommendation for container name %v", containerName) | ||||
} | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about moving this section move to the "features.md" page?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are many sections in examples.md which actually describe features of VPA (Starting multiple recommenders, Custom memory bump-up after OOMKill, Using CPU management with static policy, Controlling eviction behavior based on scaling direction and resource, etc.). I don't think the newly introduced section is different from the existing section.
IMO, examples and features are overlapping conceptually. If you describe a feature, you usually also add example(s) of how the feature can be used.
examples.md
andfeatures.md
should be merged IMO. This is out-of-scope of the existing PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with you here. The features page is new, and I want to start moving the examples across to the features page, with more description about that feature.
I thought it would be nice for the "global max" feature to be added to features from the start, since it's a better fit there, and will require work to move at a later stage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(but it's up to you though, the documentation needs a lot of work in general)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to keep it consistent with the existing doc. In another PR all the examples doc can be reworked as features sections.