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

Add Fallback option behavior for dynamic fallback calculation #6464

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
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**: Add fallback option useCurrentReplicasAsMinimum to use current number of replicas ([#6450](https://github.com/kedacore/keda/issues/6450))
- **General**: Enable OpenSSF Scorecard to enhance security practices across the project ([#5913](https://github.com/kedacore/keda/issues/5913))
- **General**: Introduce new NSQ scaler ([#3281](https://github.com/kedacore/keda/issues/3281))
- **General**: Operator flag to control patching of webhook resources certificates ([#6184](https://github.com/kedacore/keda/issues/6184))
Expand Down
2 changes: 2 additions & 0 deletions apis/keda/v1alpha1/scaledobject_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ type ScaledObjectSpec struct {
type Fallback struct {
FailureThreshold int32 `json:"failureThreshold"`
Replicas int32 `json:"replicas"`
// +optional
UseCurrentReplicasAsMinimum *bool `json:"useCurrentReplicasAsMinimum,omitempty"`
wozniakjan marked this conversation as resolved.
Show resolved Hide resolved
}

// AdvancedConfig specifies advance scaling options
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/keda.sh_scaledobjects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ spec:
replicas:
format: int32
type: integer
useCurrentReplicasAsMinimum:
description: UseCurrentReplicasAsMinimum indicates whether the current number of replicas should be used as a minimum fallback.
type: boolean
required:
- failureThreshold
- replicas
Expand Down
28 changes: 24 additions & 4 deletions pkg/fallback/fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import (
v2 "k8s.io/api/autoscaling/v2"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/scale"
"k8s.io/metrics/pkg/apis/external_metrics"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
"github.com/kedacore/keda/v2/pkg/scaling/resolver"
)

var log = logf.Log.WithName("fallback")
Expand All @@ -46,7 +48,7 @@ func isFallbackEnabled(scaledObject *kedav1alpha1.ScaledObject, metricSpec v2.Me
return true
}

func GetMetricsWithFallback(ctx context.Context, client runtimeclient.Client, metrics []external_metrics.ExternalMetricValue, suppressedError error, metricName string, scaledObject *kedav1alpha1.ScaledObject, metricSpec v2.MetricSpec) ([]external_metrics.ExternalMetricValue, bool, error) {
func GetMetricsWithFallback(ctx context.Context, client runtimeclient.Client, scaleClient scale.ScalesGetter, metrics []external_metrics.ExternalMetricValue, suppressedError error, metricName string, scaledObject *kedav1alpha1.ScaledObject, metricSpec v2.MetricSpec) ([]external_metrics.ExternalMetricValue, bool, error) {
status := scaledObject.Status.DeepCopy()

initHealthStatus(status)
Expand Down Expand Up @@ -76,7 +78,11 @@ func GetMetricsWithFallback(ctx context.Context, client runtimeclient.Client, me
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:
return doFallback(scaledObject, metricSpec, metricName, suppressedError), true, nil
currentReplicas, err := resolver.GetCurrentReplicas(ctx, client, scaleClient, scaledObject)
if err != nil {
return nil, false, suppressedError
}
return doFallback(scaledObject, metricSpec, metricName, currentReplicas, suppressedError), true, nil
default:
return nil, false, suppressedError
}
Expand All @@ -103,8 +109,18 @@ func HasValidFallback(scaledObject *kedav1alpha1.ScaledObject) bool {
modifierChecking
}

func doFallback(scaledObject *kedav1alpha1.ScaledObject, metricSpec v2.MetricSpec, metricName string, suppressedError error) []external_metrics.ExternalMetricValue {
func doFallback(scaledObject *kedav1alpha1.ScaledObject, metricSpec v2.MetricSpec, metricName string, currentReplicas int32, suppressedError error) []external_metrics.ExternalMetricValue {
replicas := int64(scaledObject.Spec.Fallback.Replicas)

// Check if we should use current replicas as minimum
if scaledObject.Spec.Fallback.UseCurrentReplicasAsMinimum != nil &&
*scaledObject.Spec.Fallback.UseCurrentReplicasAsMinimum {
currentReplicasCount := int64(currentReplicas)
if currentReplicasCount > replicas {
replicas = currentReplicasCount
}
}

var normalisationValue int64
if !scaledObject.IsUsingModifiers() {
normalisationValue = int64(metricSpec.External.Target.AverageValue.AsApproximateFloat64())
Expand All @@ -121,7 +137,11 @@ func doFallback(scaledObject *kedav1alpha1.ScaledObject, metricSpec v2.MetricSpe
}
fallbackMetrics := []external_metrics.ExternalMetricValue{metric}

log.Info("Suppressing error, falling back to fallback.replicas", "scaledObject.Namespace", scaledObject.Namespace, "scaledObject.Name", scaledObject.Name, "suppressedError", suppressedError, "fallback.replicas", replicas)
log.Info("Suppressing error, using fallback metrics",
"scaledObject.Namespace", scaledObject.Namespace,
"scaledObject.Name", scaledObject.Name,
"suppressedError", suppressedError,
"fallback.replicas", replicas)
wozniakjan marked this conversation as resolved.
Show resolved Hide resolved
return fallbackMetrics
}

Expand Down
Loading
Loading