Skip to content

Refactor(hpa): extract common metric target validation logic #132405

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

googs1025
Copy link
Member

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

  • This PR refactors the validation logic for various metric sources in the Horizontal Pod Autoscaler (HPA), extracting common validation patterns into reusable functions.

Previously, similar checks for mutually exclusive fields such as Value/AverageValue and AverageUtilization/AverageValue were duplicated across multiple validation functions like validateExternalSource, validateResourceSource, etc. This change consolidates that logic into shared validators to improve code maintainability and readability.

Which issue(s) this PR is related to:

Special notes for your reviewer:

Does this PR introduce a user-facing change?

None

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

None

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Jun 19, 2025
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Jun 19, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: googs1025
Once this PR has been reviewed and has the lgtm label, please assign liggitt for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot requested review from dims and sttts June 19, 2025 15:27
@adrianmoisey
Copy link
Member

/sig autoscaling

@k8s-ci-robot k8s-ci-robot added sig/autoscaling Categorizes an issue or PR as relevant to SIG Autoscaling. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Jun 19, 2025
@omerap12
Copy link
Member

/assign

@@ -327,10 +327,7 @@ func validateObjectSource(src *autoscaling.ObjectMetricSource, fldPath *field.Pa
allErrs = append(allErrs, ValidateCrossVersionObjectReference(src.DescribedObject, fldPath.Child("describedObject"))...)
allErrs = append(allErrs, validateMetricIdentifier(src.Metric, fldPath.Child("metric"))...)
allErrs = append(allErrs, validateMetricTarget(src.Target, fldPath.Child("target"))...)

if src.Target.Value == nil && src.Target.AverageValue == nil {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, it seems not to just check if both are not empty. Should we still check if both exist at the same time?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I understand your question but we need to check that exactly one of the values must be set. So first we check that at least one value is set and then we need to ensure both values aren't set simultaneously.

@googs1025
Copy link
Member Author

@adrianmoisey @omerap12 can you help this? 😄


if target.Value != nil && target.AverageValue != nil {
allErrs = append(allErrs, field.Forbidden(fldPath.Child("value"),
"must not set both a target value or averageValue"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo, how about something like: "must not set both a target value and averageValue"
"or" is grammatically incorrect when describing what cannot be set together. It should be "and" since we're referring to the prohibition of setting both fields simultaneously.

if target.AverageUtilization != nil && target.AverageValue != nil {
allErrs = append(allErrs, field.Forbidden(
fldPath.Child("averageValue"),
"must not set both a target raw value and a target utilization",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as a above

@@ -327,10 +327,7 @@ func validateObjectSource(src *autoscaling.ObjectMetricSource, fldPath *field.Pa
allErrs = append(allErrs, ValidateCrossVersionObjectReference(src.DescribedObject, fldPath.Child("describedObject"))...)
allErrs = append(allErrs, validateMetricIdentifier(src.Metric, fldPath.Child("metric"))...)
allErrs = append(allErrs, validateMetricTarget(src.Target, fldPath.Child("target"))...)

if src.Target.Value == nil && src.Target.AverageValue == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I understand your question but we need to check that exactly one of the values must be set. So first we check that at least one value is set and then we need to ensure both values aren't set simultaneously.

if src.Target.Value == nil && src.Target.AverageValue == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("target").Child("averageValue"), "must set either a target value or averageValue"))
}
allErrs = append(allErrs, validateValueOrAverageValue(src.Target, fldPath.Child("target"))...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will change behaviour for users. If they have (incorrect) HPAs that define both, they used to apply successfully and now they won't after this change. (https://www.hyrumslaw.com).

While I like that this is being added, I'm never sure how these situations should be handled to get this into k/k.

May be @soltysh can advise?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. release-note-none Denotes a PR that doesn't merit a release note. sig/autoscaling Categorizes an issue or PR as relevant to SIG Autoscaling. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants