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 10 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio
### New

- **General**: Introduce new NSQ scaler ([#3281](https://github.com/kedacore/keda/issues/3281))
- **General**: Optimize all webhook checkReplicas code ([#6344](https://github.com/kedacore/keda/pull/6344))

#### Experimental

Expand Down
52 changes: 43 additions & 9 deletions apis/keda/v1alpha1/scaledobject_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

return so.Spec.MinReplicaCount
}
tmp := defaultHPAMinReplicas
return &tmp
}

// 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

}

// getHPAMaxReplicas returns MaxReplicas based on definition in ScaledObject or default value if not defined
func (so *ScaledObject) GetHPAMaxReplicas() int32 {
if so.Spec.MaxReplicaCount != nil {
Expand All @@ -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

}

// 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
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.

}

// 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
Expand Down
2 changes: 1 addition & 1 deletion apis/keda/v1alpha1/scaledobject_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

}

func verifyFallback(incomingSo *ScaledObject, action string, _ bool) error {
Expand Down
13 changes: 13 additions & 0 deletions controllers/keda/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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

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
Expand Down
Loading