Skip to content
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

Add resource validation #4798

Merged

Conversation

mowangdk
Copy link
Contributor

@mowangdk mowangdk commented Apr 7, 2022

Which component this PR applies to?

vertical-pod-autoscaler

What type of PR is this?

/kind bug

What this PR does / why we need it:

Add Validation for VPA

Which issue(s) this PR fixes:

Fixes #4790

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 the kind/bug Categorizes issue or PR as related to a bug. label Apr 7, 2022
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Apr 7, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: mowangdk / name: geyingqi (68d7593)

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Apr 7, 2022
@k8s-ci-robot
Copy link
Contributor

Welcome @mowangdk!

It looks like this is your first PR to kubernetes/autoscaler 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/autoscaler has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Apr 7, 2022
@mowangdk mowangdk force-pushed the fix/add_resource_validation branch from 1ed2ae4 to 68d7593 Compare April 7, 2022 12:21
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Apr 7, 2022
@mowangdk mowangdk force-pushed the fix/add_resource_validation branch from 68d7593 to 4ce04dd Compare April 7, 2022 12:26
@mowangdk
Copy link
Contributor Author

mowangdk commented Apr 7, 2022

@jbartosik PTAL

Copy link
Collaborator

@jbartosik jbartosik left a comment

Choose a reason for hiding this comment

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

Thank you for the change. I left a few comments. (Also I'll respond faster in the future)

@mowangdk mowangdk force-pushed the fix/add_resource_validation branch from 4ce04dd to 93febe6 Compare April 16, 2022 03:58
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 16, 2022
@mowangdk mowangdk force-pushed the fix/add_resource_validation branch from 93febe6 to 48b4e45 Compare April 17, 2022 06:22
@mowangdk
Copy link
Contributor Author

@jbartosik comments fixed, ptal

Copy link
Collaborator

@jbartosik jbartosik left a comment

Choose a reason for hiding this comment

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

Thank you. I think this is close to being ready for submit.

There is one change needed (validate resolution of max even if there is no corresponding min). I also left a few requests for smaller improvements.

expectError: fmt.Errorf("Memory [%v] must be a whole number of bytes", resource.MustParse("100m")),
},
{
name: "bad manAllowed cpu value",
Copy link
Collaborator

Choose a reason for hiding this comment

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

typo: s/manAllowed/maxAllowed/

@@ -36,6 +37,7 @@ func TestValidateVPA(t *testing.T) {
badMinReplicas := int32(0)
validMinReplicas := int32(1)
badScalingMode := vpa_types.ContainerScalingMode("bad")
badCPUValue := resource.MustParse("187500u")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use resource.MustParse("187500u") directly, like with other values.

if found && max.Cmp(min) < 0 {
return fmt.Errorf("max resource for %v is lower than min", resource)
if found {
if err := validate(resource, max); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should check if max has correct resolution in a loop:

for resource, min := range policy.MaxAllowed {
  if err := validate(resource, max); err != nil {
    //...

current implementation doesn't validate max when min is not specified.

@@ -133,9 +136,17 @@ func validateVPA(vpa *vpa_types.VerticalPodAutoscaler, isCreate bool) error {
}
}
for resource, min := range policy.MinAllowed {
if err := validate(resource, min); err != nil {
return err
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be nice to mention in the error message which field has improper value.

You can add this information here:

  return fmt.Errorf("MinAllowed: %s", err)

or pass the name of the field to validate so it can include it in the error message it returns.

@@ -157,3 +168,27 @@ func validateVPA(vpa *vpa_types.VerticalPodAutoscaler, isCreate bool) error {

return nil
}

func validate(name corev1.ResourceName, val apires.Quantity) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please make the name more specific. For example validateResourceResolution

@mowangdk mowangdk force-pushed the fix/add_resource_validation branch from dfc6601 to 9a80be0 Compare April 22, 2022 10:04
@mowangdk
Copy link
Contributor Author

@jbartosik ptal, There is no way to call string() directly on resource.MustParse("187500u") return value , so i kept badCPUResource

@jbartosik
Copy link
Collaborator

Thanks, just a couple small comments.

Please squash commits.

Joachim ptal, There is no way to call string() directly on resource.MustParse("187500u") return value , so i kept badCPUResource

I think you can do this the same way you're handling other resource amounts.

Copy link
Collaborator

@jbartosik jbartosik left a comment

Choose a reason for hiding this comment

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

Looks good, only a few small comments left:

  • No need to change if
  • Consistent approach to amounts in unit tests
  • Please squash commits

@jbartosik jbartosik self-requested a review April 22, 2022 15:36
@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 22, 2022
@mowangdk mowangdk force-pushed the fix/add_resource_validation branch from 9a80be0 to c85142b Compare April 23, 2022 06:24
@mowangdk mowangdk force-pushed the fix/add_resource_validation branch from c85142b to 82f6707 Compare April 23, 2022 06:30
@mowangdk
Copy link
Contributor Author

@jbartosik make some changes according to your comments, ptal

Copy link
Collaborator

@jbartosik jbartosik left a comment

Choose a reason for hiding this comment

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

Thank you

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 25, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jbartosik, mowangdk

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

The pull request process is described 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/vertical-pod-autoscaler cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VPA should validate that resources are specified with supported resolution
3 participants