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

fix bug that spreadconstraints are not validated correctly #3232

Merged

Conversation

whitewindmills
Copy link
Member

@whitewindmills whitewindmills commented Mar 2, 2023

What type of PR is this?
/kind feature
/kind cleanup

What this PR does / why we need it:
fix bug that spreadconstraints are not validated correctly
Which issue(s) this PR fixes:
Fixes #3231
Special notes for your reviewer:

Does this PR introduce a user-facing change?:

`karmada-webhook`: Introduced validation to ensure the `.spec.placement.spreadConstraints.maxGroups/minGroups` in PropagationPolicy is declared with a reasonable value.

@karmada-bot karmada-bot added the kind/bug Categorizes issue or PR as related to a bug. label Mar 2, 2023
@karmada-bot karmada-bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Mar 2, 2023
@codecov-commenter
Copy link

codecov-commenter commented Mar 2, 2023

Codecov Report

Merging #3232 (ae325b8) into master (0652cfb) will increase coverage by 0.07%.
The diff coverage is 100.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##           master    #3232      +/-   ##
==========================================
+ Coverage   49.07%   49.15%   +0.07%     
==========================================
  Files         203      206       +3     
  Lines       18354    18377      +23     
==========================================
+ Hits         9008     9033      +25     
  Misses       8856     8856              
+ Partials      490      488       -2     
Flag Coverage Δ
unittests 49.15% <100.00%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pkg/util/validation/validation.go 86.56% <100.00%> (+0.62%) ⬆️
pkg/util/worker.go 66.66% <0.00%> (-4.77%) ⬇️
pkg/search/proxy/store/util.go 93.36% <0.00%> (-0.95%) ⬇️
pkg/util/helper/work.go 42.45% <0.00%> (-0.82%) ⬇️
pkg/util/helper/binding.go 72.00% <0.00%> (-0.34%) ⬇️
pkg/util/helper/workstatus.go 10.19% <0.00%> (-0.10%) ⬇️
pkg/controllers/binding/binding_controller.go 0.00% <0.00%> (ø)
...ers/binding/cluster_resource_binding_controller.go 0.00% <0.00%> (ø)
pkg/controllers/status/workstatus_controller.go
pkg/controllers/status/common.go 0.00% <0.00%> (ø)
... and 7 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@XiShanYongYe-Chang XiShanYongYe-Chang left a comment

Choose a reason for hiding this comment

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

Perhaps we need to specify in the API declaration as well:

// MaxGroups restricts the maximum number of cluster groups to be selected.
// +optional
MaxGroups int `json:"maxGroups,omitempty"`
// MinGroups restricts the minimum number of cluster groups to be selected.
// Defaults to 1.
// +optional
MinGroups int `json:"minGroups,omitempty"`

@@ -25,6 +25,11 @@ func SetDefaultSpreadConstraints(spreadConstraints []policyv1alpha1.SpreadConstr
klog.Infof("Setting default MinGroups to 1")
spreadConstraints[i].MinGroups = 1
}

if spreadConstraints[i].MaxGroups == 0 && spreadConstraints[i].MinGroups > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

For spreadConstraints[i].MinGroups > 0, this part of the judgment seems redundant. It has been ensured before that the value is greater than 0.

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 minGroups is less than zero? This part of the judgment is necessary.

Copy link
Member

Choose a reason for hiding this comment

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

Isn't this a situation that has been intercepted by validate?

Copy link
Member Author

Choose a reason for hiding this comment

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

intercepted

No, mutating webhook works first!

Copy link
Member

@chaunceyjiang chaunceyjiang Mar 2, 2023

Choose a reason for hiding this comment

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

I recommend using +kubebuilder:validation:Minimum=1 and +kubebuilder:default=1.

Copy link
Member

Choose a reason for hiding this comment

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

No, mutating webhook works first!

Ok

I recommend using +kubebuilder:validation:Minimum=1.

Does this conflict with // +optional?

Copy link
Member Author

Choose a reason for hiding this comment

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

I recommend using +kubebuilder:validation:Minimum=1 and +kubebuilder:default=1.

I don't see any difference in performance, because the webhook will always be called, but this part of the verification is done by the apiserver.

Copy link
Member Author

Choose a reason for hiding this comment

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

Now that there is a validating webhook, it is better to hand over the verification work to webhook instead of webhook and apiserver co-work this validating work.

if constraint.MaxGroups < 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Index(index), constraint, "maxGroups lower than 0 is not allowed"))
}

Copy link
Member

Choose a reason for hiding this comment

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

Do we need to check that MaxGroups cannot be less than MinGroups?

Copy link
Member Author

Choose a reason for hiding this comment

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

e need to check that MaxGroups cannot be less than MinGroups?

Why not? MaxGroups should not be always less than MinGroups, otherwise why is it named MaxGroups and MinGroups.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I missed it. There's already a judgment in the logic.

Copy link
Member

Choose a reason for hiding this comment

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

MaxGroups could equal 0? If so, it means what? i.e., HPA minReplicas could only be >=1. I also think MinGroups should >=1

Copy link
Member Author

Choose a reason for hiding this comment

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

No, after mutating, MaxGroups could be more than 0 or less than 0, never be 0.

@whitewindmills
Copy link
Member Author

Perhaps we need to specify in the API declaration as well:

// MaxGroups restricts the maximum number of cluster groups to be selected.
// +optional
MaxGroups int `json:"maxGroups,omitempty"`
// MinGroups restricts the minimum number of cluster groups to be selected.
// Defaults to 1.
// +optional
MinGroups int `json:"minGroups,omitempty"`

You're right.

@whitewindmills
Copy link
Member Author

Perhaps we need to specify in the API declaration as well:

// MaxGroups restricts the maximum number of cluster groups to be selected.
// +optional
MaxGroups int `json:"maxGroups,omitempty"`
// MinGroups restricts the minimum number of cluster groups to be selected.
// Defaults to 1.
// +optional
MinGroups int `json:"minGroups,omitempty"`

You're right.

How about this comment?

// Defaults to MinGroups if MinGroups is more than 0.

@whitewindmills
Copy link
Member Author

Sorry, I accidentally clicked the close button.

@XiShanYongYe-Chang
Copy link
Member

// Defaults to MinGroups.

IMOP, it will be enough.

@RainbowMango
Copy link
Member

/assign

Copy link
Member

@RainbowMango RainbowMango left a comment

Choose a reason for hiding this comment

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

The validation part sounds good to me. And I believe this PR is more like focus on the validation instead of the default value:)

@@ -231,6 +231,7 @@ type SpreadConstraint struct {
SpreadByLabel string `json:"spreadByLabel,omitempty"`

// MaxGroups restricts the maximum number of cluster groups to be selected.
// Defaults to MinGroups.
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the MaxGroups deserves a default value.
In general, the default value should be a common one that meets most of the use cases.
Here, the valid range of MinGroups is [1, max(int)], in order to let the scheduler works out(or, try to) a result we set the defaults to 1, it seems reasonable, but for MaxGroups which is essentially a limitation to the scheduler that can't spread the group that much, we don't have enough evidence to show that users always set MaxGroups with the same value as MinGroups.

Copy link
Member Author

Choose a reason for hiding this comment

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

@RainbowMango Hi, thanks for your replies!
I agree with most of your thoughts, but MaxGroups would be zero which is an incomprehensible value for karmada-scheduler without a default value. In other words, how should we(or karmada-scheduler does) understand MaxGroups without a default value?

Copy link
Member

Choose a reason for hiding this comment

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

how should we(or karmada-scheduler does) understand MaxGroups without a default value?

It perhaps can't happen because there is a validation to ensure MaxGroups can't be lower than MinGroups(1).
right?

Copy link
Member Author

Choose a reason for hiding this comment

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

MaxGroups is not required, so it’s default to 0.

@whitewindmills
Copy link
Member Author

@RainbowMango I don't know if you have any other suggestions, if not, I think this PR can be closed.

@RainbowMango
Copy link
Member

I'm still thinking if the MaxGroups should be set with a default value.
Can we focus on the validation part for this PR?

@whitewindmills
Copy link
Member Author

I'm still thinking if the MaxGroups should be set with a default value.
Can we focus on the validation part for this PR?

Okay, I'll only retain the validation part for this PR due to the difference of opinion for MaxGroups.

@RainbowMango
Copy link
Member

OK, thanks.

Signed-off-by: whitewindmills <jayfantasyhjh@gmail.com>
Copy link
Member

@RainbowMango RainbowMango left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

Thanks.

@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Mar 7, 2023
@karmada-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: RainbowMango

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

@karmada-bot karmada-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 7, 2023
@RainbowMango RainbowMango removed the kind/bug Categorizes issue or PR as related to a bug. label Mar 7, 2023
@RainbowMango RainbowMango added kind/feature Categorizes issue or PR as related to a new feature. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. labels Mar 7, 2023
@RainbowMango RainbowMango added this to the v1.6 milestone Mar 7, 2023
@karmada-bot karmada-bot merged commit 1a1a4b2 into karmada-io:master Mar 7, 2023
@whitewindmills whitewindmills deleted the spreadconstraints-validation branch March 8, 2023 02:15
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. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. 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.

pp/cpp's spreadconstraints are not validated correctly
7 participants