Skip to content

Commit

Permalink
nil checkers a refactors, new fallback unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gauron99 committed Jul 7, 2023
1 parent 1d6a251 commit a7151a6
Show file tree
Hide file tree
Showing 5 changed files with 567 additions and 103 deletions.
7 changes: 7 additions & 0 deletions apis/keda/v1alpha1/condition_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ func (c *Conditions) GetPausedCondition() Condition {
return c.getCondition(ConditionPaused)
}

func (c *Conditions) GetExternalFallbackCondition() Condition {
if *c == nil {
c = GetInitializedConditions()
}
return c.getCondition(ConditionExternalFallback)
}

func (c Conditions) getCondition(conditionType ConditionType) Condition {
for i := range c {
if c[i].Type == conditionType {
Expand Down
5 changes: 3 additions & 2 deletions controllers/keda/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,10 @@ func (r *ScaledObjectReconciler) getScaledObjectMetricSpecs(ctx context.Context,

updateHealthStatus(scaledObject, externalMetricNames, status)

// if ComplexScalingLogic struct is not nil, expect Formula or ExternalCalculation
// if ComplexScalingLogic struct is not empty, expect Formula or ExternalCalculation
// to be non-empty. If target is > 0.0 create a compositeScaler structure
if !reflect.DeepEqual(scaledObject.Spec.Advanced.ComplexScalingLogic, kedav1alpha1.ComplexScalingLogic{}) {
if scaledObject.Spec.Advanced != nil &&
!reflect.DeepEqual(scaledObject.Spec.Advanced.ComplexScalingLogic, kedav1alpha1.ComplexScalingLogic{}) {
validNumTarget, validMetricType, err := validateCompositeScalingLogic(scaledObject, scaledObjectMetricSpecs)
if err != nil {
logger.Error(err, "error validating compositeScalingLogic")
Expand Down
28 changes: 12 additions & 16 deletions pkg/fallback/fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func isFallbackEnabled(scaledObject *kedav1alpha1.ScaledObject, metricSpec v2.Me
return false
}
case externalCalculatorStr:
if scaledObject.Spec.Advanced.ComplexScalingLogic.Fallback == nil {
// check for nil pointer & only then check for fallback
if scaledObject.Spec.Advanced == nil || scaledObject.Spec.Advanced.ComplexScalingLogic.Fallback == nil {
return false
}
default:
Expand All @@ -59,9 +60,7 @@ func isFallbackEnabled(scaledObject *kedav1alpha1.ScaledObject, metricSpec v2.Me
return true
}

func GetMetricsWithFallbackExternalCalculator(ctx context.Context, client runtimeclient.Client, metrics *cl.MetricsList, suppressedError error, metricName string, scaledObject *kedav1alpha1.ScaledObject) (bool, error) {
// init empty list if it is nil because of an error earlier

func GetMetricsWithFallbackExternalCalculator(ctx context.Context, client runtimeclient.Client, metrics []*cl.Metric, suppressedError error, metricName string, scaledObject *kedav1alpha1.ScaledObject) ([]*cl.Metric, bool, error) {
const determiner string = "externalcalculator"
status := scaledObject.Status.DeepCopy()

Expand All @@ -72,7 +71,7 @@ func GetMetricsWithFallbackExternalCalculator(ctx context.Context, client runtim
if healthStatus == nil {
// should never be nil
err := fmt.Errorf("internal error getting health status in GetMetricsWithFallbackExternalCalculator - wrong determiner")
return false, err
return []*cl.Metric{}, false, err
}

// if there is no error
Expand All @@ -84,7 +83,7 @@ func GetMetricsWithFallbackExternalCalculator(ctx context.Context, client runtim

updateStatus(ctx, client, scaledObject, status, v2.MetricSpec{})

return false, nil
return metrics, false, nil
}

healthStatus.Status = kedav1alpha1.HealthStatusFailing
Expand All @@ -94,16 +93,14 @@ func GetMetricsWithFallbackExternalCalculator(ctx context.Context, client runtim

switch {
case !isFallbackEnabled(scaledObject, v2.MetricSpec{}, determiner):
return false, suppressedError
return metrics, false, suppressedError
case !validateFallback(scaledObject, determiner):
log.Info("Failed to validate ScaledObject ComplexScalingLogic Fallback. Please check that parameters are positive integers", "scaledObject.Namespace", scaledObject.Namespace, "scaledObject.Name", scaledObject.Name)
return false, suppressedError
return metrics, false, suppressedError
case *healthStatus.NumberOfFailures > scaledObject.Spec.Advanced.ComplexScalingLogic.Fallback.FailureThreshold:
doExternalCalculationFallback(scaledObject, metrics, metricName, suppressedError)

return true, nil
return doExternalCalculationFallback(scaledObject, metrics, metricName, suppressedError), true, nil
default:
return false, suppressedError
return metrics, false, suppressedError
}
}

Expand Down Expand Up @@ -200,21 +197,20 @@ func doFallback(scaledObject *kedav1alpha1.ScaledObject, metricSpec v2.MetricSpe
return fallbackMetrics
}

func doExternalCalculationFallback(scaledObject *kedav1alpha1.ScaledObject, metrics *cl.MetricsList, metricName string, suppressedError error) {
func doExternalCalculationFallback(scaledObject *kedav1alpha1.ScaledObject, metrics []*cl.Metric, metricName string, suppressedError error) []*cl.Metric {
replicas := int64(scaledObject.Spec.Advanced.ComplexScalingLogic.Fallback.Replicas)
normalisationValue, err := strconv.ParseFloat(scaledObject.Spec.Advanced.ComplexScalingLogic.Target, 64)
if err != nil {
log.Error(err, "error converting string to float in ExternalCalculation fallback")
return
return metrics
}
metric := cl.Metric{
Name: metricName,
Value: float32(normalisationValue * float64(replicas)),
}

metrics.MetricValues = []*cl.Metric{&metric}

log.Info(fmt.Sprintf("Suppressing error, externalCalculator falling back to %d fallback.replicas", scaledObject.Spec.Advanced.ComplexScalingLogic.Fallback.Replicas), "scaledObject.Namespace", scaledObject.Namespace, "scaledObject.Name", scaledObject.Name, "suppressedError", suppressedError)
return []*cl.Metric{&metric}
}

func updateStatus(ctx context.Context, client runtimeclient.Client, scaledObject *kedav1alpha1.ScaledObject, status *kedav1alpha1.ScaledObjectStatus, metricSpec v2.MetricSpec) {
Expand Down
Loading

0 comments on commit a7151a6

Please sign in to comment.