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: optimize all checkRepliacas code and fix the problem does not take effect #6344

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

ctccxxd
Copy link

@ctccxxd ctccxxd commented Nov 19, 2024

fix and optimize all checkReplicas code, originally
1, Webhook for replicas check does not take effect.
2, No check for negative value.
3, Variable max or min is same with build-in function name, not graceful
4, Have no defaultMaxReplicas for hpa.
5, Code has redundency.

Checklist

related issue
#6356

@ctccxxd ctccxxd requested a review from a team as a code owner November 19, 2024 03:13
Signed-off-by: Shane <[email protected]>
@ctccxxd ctccxxd marked this pull request as draft November 19, 2024 04:35
@ctccxxd ctccxxd marked this pull request as ready for review November 19, 2024 05:31
@ctccxxd ctccxxd changed the title fix and optimize all checkRepliacas code optimize all checkRepliacas code and fix some Nov 19, 2024
@ctccxxd ctccxxd changed the title optimize all checkRepliacas code and fix some optimize all checkRepliacas code and fix the problem does not take effect Nov 19, 2024
@ctccxxd ctccxxd changed the title optimize all checkRepliacas code and fix the problem does not take effect fix: optimize all checkRepliacas code and fix the problem does not take effect Nov 21, 2024
@ctccxxd
Copy link
Author

ctccxxd commented Nov 21, 2024

Friendly ping @zroubalik @wozniakjan @SpiritZhou @psi @wonko @fivesheep @JorTurFer hope you to review and discuss, much thanks.

Copy link
Member

@JorTurFer JorTurFer left a comment

Choose a reason for hiding this comment

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

I think that I'm missing something. Can't we just include the negative check message in the current GetHPAMin/MaxReplicas funcs and it'd do almost the same, wouldn't?

@ctccxxd
Copy link
Author

ctccxxd commented Nov 24, 2024

I think that I'm missing something. Can't we just include the negative check message in the current GetHPAMin/MaxReplicas funcs and it'd do almost the same, wouldn't?

Much thanks for your comments. Originally. it has much logic in GetHPAMin/MaxReplicas funcs including default replicas logic. I
hink it is not very explicit for user to read. I want to make the logic more clean. Besides, I have more purposes,

1, Webhook for replicas check does not take effect.
2, No check for negative value.
3, Variable max or min is same with build-in function name, not graceful
4, Have no defaultMaxReplicas for hpa.
5, Code has redundency. Hope for your reply.

@JorTurFer PTAL much thanks.

change changelog

Signed-off-by: Shane <[email protected]>
@ctccxxd
Copy link
Author

ctccxxd commented Nov 28, 2024

I think that I'm missing something. Can't we just include the negative check message in the current GetHPAMin/MaxReplicas funcs and it'd do almost the same, wouldn't?

Much thanks for your comments. Originally. it has much logic in GetHPAMin/MaxReplicas funcs including default replicas logic. I hink it is not very explicit for user to read. I want to make the logic more clean. Besides, I have more purposes,

1, Webhook for replicas check does not take effect. 2, No check for negative value. 3, Variable max or min is same with build-in function name, not graceful 4, Have no defaultMaxReplicas for hpa. 5, Code has redundency. Hope for your reply.

@JorTurFer @wozniakjan PTAL much thanks.

Copy link
Member

@wozniakjan wozniakjan left a comment

Choose a reason for hiding this comment

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

I'm wondering why the webhook only logged the wrong min/max and never prevented the SO from creation. So the bug is imho real but we should ensure the coding style is matching conventions.

// GetDefaultHPAMinReplicas returns defaultHPAMinReplicas
func (so *ScaledObject) GetDefaultHPAMinReplicas() *int32 {
tmp := defaultHPAMinReplicas
return &tmp
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 this needs a receiver, the so is ignored in the body

Copy link
Author

Choose a reason for hiding this comment

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

Quote reply

make sense

@@ -254,21 +260,49 @@ func (so *ScaledObject) GetHPAMaxReplicas() int32 {
return defaultHPAMaxReplicas
}

// GetDefaultHPAMaxReplicas returns defaultHPAMaxReplicas
func (so *ScaledObject) GetDefaultHPAMaxReplicas() int32 {
return defaultHPAMaxReplicas
Copy link
Member

Choose a reason for hiding this comment

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

same here, having this as a method on ScaledObject structure feels unnecessary

return false
}
}
return true
Copy link
Member

Choose a reason for hiding this comment

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

and here too, the pointer receiver is ignored. But I think the code can be made even cleaner without this function entirely

Copy link
Author

Choose a reason for hiding this comment

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

and here too, the pointer receiver is ignored. But I think the code can be made even cleaner without this function entirely

ok


// GetIdleReplicasIfDefined returns bool based on whether idleRelicas is defined
func (so *ScaledObject) GetIdleReplicasIfDefined() bool {
return so.Spec.IdleReplicaCount != nil
Copy link
Member

Choose a reason for hiding this comment

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

imho, the check is more readable than GetIdleReplicasIfDefined name, I'd ditch this helper too and keep so.Spec.IdleReplicaCount != nil which is very common convention in kubernetes codebase.

Copy link
Author

Choose a reason for hiding this comment

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

imho, the check is more readable than GetIdleReplicasIfDefined name, I'd ditch this helper too and keep so.Spec.IdleReplicaCount != nil which is very common convention in kubernetes codebase.

ok, I will follow the code style you suggest.

@@ -184,7 +184,7 @@ func verifyReplicaCount(incomingSo *ScaledObject, action string, _ bool) error {
scaledobjectlog.WithValues("name", incomingSo.Name).Error(err, "validation error")
metricscollector.RecordScaledObjectValidatingErrors(incomingSo.Namespace, action, "incorrect-replicas")
}
return nil
return err
Copy link
Member

Choose a reason for hiding this comment

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

hmm, @JorTurFer, @zroubalik, any thoughts why the webhook used to allow wrong min/max? I just tried it with 2.16.0 and I can easily create SO with min=25 and max=10.

2024-12-02T14:09:36Z    ERROR   scaledobject-validation-webhook validation error        {"name": "http-server", "error": "MinReplicaCount=25 must be less than MaxReplicaCount=10"}

Copy link
Member

Choose a reason for hiding this comment

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

Probably it was a mistake, the admission webhook should prevent that clear misconfiguration

@@ -239,13 +239,19 @@ func (so *ScaledObject) IsUsingModifiers() bool {

// getHPAMinReplicas returns MinReplicas based on definition in ScaledObject or default value if not defined
func (so *ScaledObject) GetHPAMinReplicas() *int32 {
if so.Spec.MinReplicaCount != nil && *so.Spec.MinReplicaCount > 0 {
if so.Spec.MinReplicaCount != nil {
Copy link
Member

Choose a reason for hiding this comment

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

this was imho better before, this should be used by the HPA controller which then needs to reimplement the logic of figuring out the 0.

Copy link
Author

Choose a reason for hiding this comment

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

this was imho better before, this should be used by the HPA controller which then needs to reimplement the logic of figuring out the 0.

Originally, there is no defaultMaxReplicas in HPA and I will add defaultMaxReplicas and defaultMinReplicas together this time. I think it is more clear in this way. Although, I give up the original logic here.

Copy link
Member

Choose a reason for hiding this comment

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

I think that this is just to prevent something like minReplicas -10, but it's true that it's already covered in explicitly outside this function

maxReplicas = scaledObject.GetDefaultHPAMaxReplicas()
}

if *minReplicas < 0 || maxReplicas < 0 {
Copy link
Member

Choose a reason for hiding this comment

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

nit: no need to check max for less than 0, it has to be bigger than min and min is checked for larger than 0 so transitively max is already larger

Copy link
Author

Choose a reason for hiding this comment

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

nit: no need to check max for less than 0, it has to be bigger than min and min is checked for larger than 0 so transitively max is already larger

make sense

@ctccxxd
Copy link
Author

ctccxxd commented Dec 12, 2024

Friendly ping @wozniakjan @JorTurFer I have almostly followed your opinion, hope you to review and discuss, much thanks.

@ctccxxd
Copy link
Author

ctccxxd commented Dec 17, 2024

@JorTurFer @wozniakjan I have follow your opinions. Hope for your review, and e2e test, much thanks.

@@ -184,7 +184,7 @@ func verifyReplicaCount(incomingSo *ScaledObject, action string, _ bool) error {
scaledobjectlog.WithValues("name", incomingSo.Name).Error(err, "validation error")
metricscollector.RecordScaledObjectValidatingErrors(incomingSo.Namespace, action, "incorrect-replicas")
}
return nil
return err
Copy link
Member

Choose a reason for hiding this comment

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

Probably it was a mistake, the admission webhook should prevent that clear misconfiguration

@@ -239,13 +239,19 @@ func (so *ScaledObject) IsUsingModifiers() bool {

// getHPAMinReplicas returns MinReplicas based on definition in ScaledObject or default value if not defined
func (so *ScaledObject) GetHPAMinReplicas() *int32 {
if so.Spec.MinReplicaCount != nil && *so.Spec.MinReplicaCount > 0 {
if so.Spec.MinReplicaCount != nil {
Copy link
Member

Choose a reason for hiding this comment

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

I think that this is just to prevent something like minReplicas -10, but it's true that it's already covered in explicitly outside this function

Comment on lines +288 to +289
if scaledObject.Spec.IdleReplicaCount != nil && *idleReplicas >= minReplicas {
return fmt.Errorf("IdleReplicaCount=%d must be less than MinReplicaCount=%d", *scaledObject.Spec.IdleReplicaCount, minReplicas)
Copy link
Member

Choose a reason for hiding this comment

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

Actually idleReplicas only can be 0 nowadays, other value will be wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants