-
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?
Changes from 5 commits
d951cec
e3cb479
59079d8
4299fd6
e8a4052
5d80f1c
55120ac
fae8688
55d48fa
f6f5c51
05b6dc4
542b9df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
return so.Spec.MinReplicaCount | ||
} | ||
tmp := defaultHPAMinReplicas | ||
return &tmp | ||
} | ||
|
||
// 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 commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this needs a receiver, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
make sense |
||
} | ||
|
||
// getHPAMaxReplicas returns MaxReplicas based on definition in ScaledObject or default value if not defined | ||
func (so *ScaledObject) GetHPAMaxReplicas() int32 { | ||
if so.Spec.MaxReplicaCount != nil { | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. same here, having this as a method on |
||
} | ||
|
||
// CheckReplicasNotNegative checks that Min/Max ReplicaCount defined in ScaledObject are not negative | ||
func (so *ScaledObject) CheckReplicasNotNegative(replicas ...int32) bool { | ||
for _, replica := range replicas { | ||
if replica < 0 { | ||
return false | ||
} | ||
} | ||
return true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
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 commentThe reason will be displayed to describe this comment to others. Learn more. imho, the check is more readable than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ok, I will follow the code style you suggest. |
||
} | ||
|
||
// checkReplicaCountBoundsAreValid checks that Idle/Min/Max ReplicaCount defined in ScaledObject are correctly specified | ||
// i.e. that Min is not greater than Max or Idle greater or equal to Min | ||
func CheckReplicaCountBoundsAreValid(scaledObject *ScaledObject) error { | ||
min := int32(0) | ||
if scaledObject.Spec.MinReplicaCount != nil { | ||
min = *scaledObject.GetHPAMinReplicas() | ||
var idleReplicas *int32 | ||
minReplicas := *scaledObject.GetHPAMinReplicas() | ||
maxReplicas := scaledObject.GetHPAMaxReplicas() | ||
idleReplicasDefined := scaledObject.GetIdleReplicasIfDefined() | ||
idleReplicas = scaledObject.Spec.IdleReplicaCount | ||
|
||
if !scaledObject.CheckReplicasNotNegative(minReplicas, maxReplicas) { | ||
return fmt.Errorf("MinReplicaCount=%d, MaxReplicaCount=%d must not be negative", minReplicas, maxReplicas) | ||
} | ||
|
||
if idleReplicasDefined && *idleReplicas < 0 { | ||
return fmt.Errorf("IdleReplicaCount=%d must not be negative", *idleReplicas) | ||
} | ||
max := scaledObject.GetHPAMaxReplicas() | ||
|
||
if min > max { | ||
return fmt.Errorf("MinReplicaCount=%d must be less than MaxReplicaCount=%d", min, max) | ||
if minReplicas > maxReplicas { | ||
return fmt.Errorf("MinReplicaCount=%d must not be greater than MaxReplicaCount=%d", minReplicas, maxReplicas) | ||
} | ||
|
||
if scaledObject.Spec.IdleReplicaCount != nil && *scaledObject.Spec.IdleReplicaCount >= min { | ||
return fmt.Errorf("IdleReplicaCount=%d must be less than MinReplicaCount=%d", *scaledObject.Spec.IdleReplicaCount, min) | ||
if idleReplicasDefined && *idleReplicas >= minReplicas { | ||
return fmt.Errorf("IdleReplicaCount=%d must be less than MinReplicaCount=%d", *scaledObject.Spec.IdleReplicaCount, minReplicas) | ||
} | ||
|
||
return nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
func verifyFallback(incomingSo *ScaledObject, action string, _ bool) error { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,19 @@ func (r *ScaledObjectReconciler) newHPAForScaledObject(ctx context.Context, logg | |
minReplicas := scaledObject.GetHPAMinReplicas() | ||
maxReplicas := scaledObject.GetHPAMaxReplicas() | ||
|
||
if *minReplicas == 0 { | ||
minReplicas = scaledObject.GetDefaultHPAMinReplicas() | ||
} | ||
|
||
if maxReplicas == 0 { | ||
maxReplicas = scaledObject.GetDefaultHPAMaxReplicas() | ||
} | ||
|
||
if *minReplicas < 0 || maxReplicas < 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: no need to check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
make sense |
||
err := fmt.Errorf("MinReplicaCount=%d and MaxReplicaCount=%d must not be negative", minReplicas, maxReplicas) | ||
return nil, err | ||
} | ||
|
||
pausedCount, err := executor.GetPausedReplicaCount(scaledObject) | ||
if err != nil { | ||
return nil, 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.
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.
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.
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 this is just to prevent something like minReplicas -10, but it's true that it's already covered in explicitly outside this function
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.
@JorTurFer One case, when user wants to set minReplicas 10, but mis-input as -10. There no error returned from webhook and user can't find the problem, the real minReplicas is 0, which is not the 10 user want to set. I think validation webhook should not be used to set default value for invalid value.