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

optimize all checkRepliacas code and fix the problem does not take effect #6344

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio

### New

- TODO ([#XXX](https://github.com/kedacore/keda/issues/XXX))
- **General**: Fix and 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 {
return so.Spec.MinReplicaCount
}
tmp := defaultHPAMinReplicas
return &tmp
}

// GetDefaultHPAMinReplicas returns defaultHPAMinReplicas
func (so *ScaledObject) GetDefaultHPAMinReplicas() *int32 {
tmp := defaultHPAMinReplicas
return &tmp
}

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

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

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

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

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