-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Shane <[email protected]>
Signed-off-by: Shane <[email protected]>
Signed-off-by: Shane <[email protected]>
Friendly ping @zroubalik @wozniakjan @SpiritZhou @psi @wonko @fivesheep @JorTurFer hope you to review and discuss, much thanks. |
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 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 1, Webhook for replicas check does not take effect. @JorTurFer PTAL much thanks. |
change changelog Signed-off-by: Shane <[email protected]>
|
Signed-off-by: Shane <[email protected]>
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'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 |
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 don't think this needs a receiver, the so
is ignored in the body
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.
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 |
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.
same here, having this as a method on ScaledObject
structure feels unnecessary
return false | ||
} | ||
} | ||
return true |
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.
and here too, the pointer receiver is ignored. But I think the code can be made even cleaner without this function entirely
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.
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 |
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.
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.
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.
imho, the check is more readable than
GetIdleReplicasIfDefined
name, I'd ditch this helper too and keepso.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 |
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.
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"}
@@ -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 { |
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.
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.
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.
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.
maxReplicas = scaledObject.GetDefaultHPAMaxReplicas() | ||
} | ||
|
||
if *minReplicas < 0 || maxReplicas < 0 { |
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.
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
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.
nit: no need to check
max
for less than0
, it has to be bigger thanmin
andmin
is checked for larger than0
so transitivelymax
is already larger
make sense
Signed-off-by: Shane <[email protected]>
Signed-off-by: Shane <[email protected]>
Signed-off-by: Shane <[email protected]>
Signed-off-by: Shane <[email protected]>
Signed-off-by: Shane <[email protected]>
Friendly ping @wozniakjan @JorTurFer I have almostly followed your opinion, hope you to review and discuss, much thanks. |
@JorTurFer @wozniakjan I have follow your opinions. Hope for your review, and e2e test, much thanks. |
Signed-off-by: Shane <[email protected]>
Signed-off-by: Shane <[email protected]>
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