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

Refactored fallback logic to only patch status when the fallback is e… #5659

Merged
merged 19 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions controllers/keda/scaledobject_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package keda
import (
"context"
"fmt"
"github.com/kedacore/keda/v2/pkg/fallback"
bharathguvvala marked this conversation as resolved.
Show resolved Hide resolved
"strconv"
"sync"

Expand Down Expand Up @@ -203,6 +204,10 @@ func (r *ScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Request
conditions.SetReadyCondition(metav1.ConditionTrue, kedav1alpha1.ScaledObjectConditionReadySuccessReason, msg)
}

if scaledObject.Spec.Fallback == nil || !fallback.HasValidFallback(scaledObject) {
conditions.SetFallbackCondition(metav1.ConditionFalse, "NoFallbackFound", "No fallbacks are active on this scaled object")
}

if err := kedastatus.SetStatusConditions(ctx, r.Client, reqLogger, scaledObject, &conditions); err != nil {
r.EventEmitter.Emit(scaledObject, req.NamespacedName, corev1.EventTypeWarning, eventemitter.ScaledObjectFailedType, eventreason.ScaledObjectUpdateFailed, err.Error())
return ctrl.Result{}, err
Expand Down
16 changes: 8 additions & 8 deletions pkg/fallback/fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func GetMetricsWithFallback(ctx context.Context, client runtimeclient.Client, me
switch {
case !isFallbackEnabled(scaledObject, metricSpec):
return nil, false, suppressedError
case !validateFallback(scaledObject):
case !HasValidFallback(scaledObject):
log.Info("Failed to validate ScaledObject Spec. Please check that parameters are positive integers", "scaledObject.Namespace", scaledObject.Namespace, "scaledObject.Name", scaledObject.Name)
return nil, false, suppressedError
case *healthStatus.NumberOfFailures > scaledObject.Spec.Fallback.FailureThreshold:
Expand All @@ -79,11 +79,7 @@ func GetMetricsWithFallback(ctx context.Context, client runtimeclient.Client, me
}
}

func fallbackExistsInScaledObject(scaledObject *kedav1alpha1.ScaledObject, metricSpec v2.MetricSpec) bool {
if !isFallbackEnabled(scaledObject, metricSpec) || !validateFallback(scaledObject) {
return false
}

func fallbackExistsInScaledObject(scaledObject *kedav1alpha1.ScaledObject) bool {
for _, element := range scaledObject.Status.Health {
if element.Status == kedav1alpha1.HealthStatusFailing && *element.NumberOfFailures > scaledObject.Spec.Fallback.FailureThreshold {
return true
Expand All @@ -93,7 +89,7 @@ func fallbackExistsInScaledObject(scaledObject *kedav1alpha1.ScaledObject, metri
return false
}

func validateFallback(scaledObject *kedav1alpha1.ScaledObject) bool {
func HasValidFallback(scaledObject *kedav1alpha1.ScaledObject) bool {
return scaledObject.Spec.Fallback.FailureThreshold >= 0 &&
scaledObject.Spec.Fallback.Replicas >= 0
}
Expand All @@ -115,7 +111,11 @@ func doFallback(scaledObject *kedav1alpha1.ScaledObject, metricSpec v2.MetricSpe
func updateStatus(ctx context.Context, client runtimeclient.Client, scaledObject *kedav1alpha1.ScaledObject, status *kedav1alpha1.ScaledObjectStatus, metricSpec v2.MetricSpec) {
patch := runtimeclient.MergeFrom(scaledObject.DeepCopy())

if fallbackExistsInScaledObject(scaledObject, metricSpec) {
if !isFallbackEnabled(scaledObject, metricSpec) || !HasValidFallback(scaledObject) {
return
}

if fallbackExistsInScaledObject(scaledObject) {
status.Conditions.SetFallbackCondition(metav1.ConditionTrue, "FallbackExists", "At least one trigger is falling back on this scaled object")
} else {
status.Conditions.SetFallbackCondition(metav1.ConditionFalse, "NoFallbackFound", "No fallbacks are active on this scaled object")
Expand Down
Loading