Skip to content

Commit

Permalink
fallback first iteration + conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
gauron99 committed Jul 4, 2023
1 parent e8bd08f commit d13ae11
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 113 deletions.
21 changes: 19 additions & 2 deletions apis/keda/v1alpha1/condition_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const (
ConditionActive ConditionType = "Active"
// ConditionFallback specifies that the resource has a fallback active.
ConditionFallback ConditionType = "Fallback"
// ConditionExternalFallback specifies that the resource has external fallback active.
ConditionExternalFallback ConditionType = "ExternalFallback"
// ConditionPaused specifies that the resource is paused.
ConditionPaused ConditionType = "Paused"
)
Expand Down Expand Up @@ -88,6 +90,8 @@ func (c *Conditions) AreInitialized() bool {
foundActive := false
foundFallback := false
foundPaused := false
foundExternalFallback := false

if *c != nil {
for _, condition := range *c {
if condition.Type == ConditionReady {
Expand All @@ -113,14 +117,19 @@ func (c *Conditions) AreInitialized() bool {
break
}
}
for _, condition := range *c {
if condition.Type == ConditionExternalFallback {
foundExternalFallback = true
}
}
}

return foundReady && foundActive && foundFallback && foundPaused
return foundReady && foundActive && foundFallback && foundPaused && foundExternalFallback
}

// GetInitializedConditions returns Conditions initialized to the default -> Status: Unknown
func GetInitializedConditions() *Conditions {
return &Conditions{{Type: ConditionReady, Status: metav1.ConditionUnknown}, {Type: ConditionActive, Status: metav1.ConditionUnknown}, {Type: ConditionFallback, Status: metav1.ConditionUnknown}, {Type: ConditionPaused, Status: metav1.ConditionUnknown}}
return &Conditions{{Type: ConditionReady, Status: metav1.ConditionUnknown}, {Type: ConditionActive, Status: metav1.ConditionUnknown}, {Type: ConditionFallback, Status: metav1.ConditionUnknown}, {Type: ConditionPaused, Status: metav1.ConditionUnknown}, {Type: ConditionExternalFallback, Status: metav1.ConditionUnknown}}
}

// IsTrue is true if the condition is True
Expand Down Expand Up @@ -171,6 +180,14 @@ func (c *Conditions) SetFallbackCondition(status metav1.ConditionStatus, reason
c.setCondition(ConditionFallback, status, reason, message)
}

// SetExternalFallbackCondition modifies ExternalFallback Condition according to input parameters (for ExternalCalculators)
func (c *Conditions) SetExternalFallbackCondition(status metav1.ConditionStatus, reason string, message string) {
if *c == nil {
c = GetInitializedConditions()
}
c.setCondition(ConditionExternalFallback, status, reason, message)
}

// SetPausedCondition modifies Paused Condition according to input parameters
func (c *Conditions) SetPausedCondition(status metav1.ConditionStatus, reason string, message string) {
if *c == nil {
Expand Down
4 changes: 2 additions & 2 deletions apis/keda/v1alpha1/scaledobject_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ type ComplexScalingLogic struct {
Formula string `json:"formula,omitempty"`
// +optional
Target string `json:"target,omitempty"`
// +optional
Fallback *Fallback `json:"fallback,omitempty"`
}

// ExternalCalculation structure describes name and URL of a gRPC server
Expand All @@ -125,8 +127,6 @@ type ExternalCalculation struct {
Name string `json:"name"`
URL string `json:"url"`
Timeout string `json:"timeout"`
// +optional
FallbackReplicas int32 `json:"fallbackReplicas,omitempty"`
}

// HorizontalPodAutoscalerConfig specifies horizontal scale config
Expand Down
5 changes: 5 additions & 0 deletions apis/keda/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 0 additions & 37 deletions pkg/externalscaling/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package externalscaling
import (
"context"
"fmt"
"strconv"
"time"

"github.com/go-logr/logr"
Expand All @@ -13,7 +12,6 @@ import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/metrics/pkg/apis/external_metrics"

"github.com/kedacore/keda/v2/apis/keda/v1alpha1"
cl "github.com/kedacore/keda/v2/pkg/externalscaling/api"
)

Expand Down Expand Up @@ -103,38 +101,3 @@ func ConvertFromGeneratedStruct(inExternal *cl.MetricsList) []external_metrics.E
}
return outK8sList
}

// Fallback function returns generated structure for metrics if its given in
// scaledObject. Returned structure has one metric value. Name of the metric is
// either name of already existing metric if its the only one, otherwise it will
// be named after current external calculator
func Fallback(err error, list *cl.MetricsList, ec v1alpha1.ExternalCalculation, targetValueString string, logger logr.Logger) (*cl.MetricsList, error) {
if err == nil {
// return unmodified list when no error exists
return list, nil
}

targetValue, errParse := strconv.ParseFloat(targetValueString, 64)
if errParse != nil {
return nil, errParse
}

listOut := cl.MetricsList{}
// if list contains only one metric, return the same one (by name) otherwise
// if multiple metrics are given, return just one with the name of ExternalCalculator
metricName := ""
if len(list.MetricValues) == 1 {
metricName = list.MetricValues[0].Name
} else {
metricName = ec.Name
}
// returned metrics
metricValue := int64((targetValue * 1000) * float64(ec.FallbackReplicas))
metric := cl.Metric{
Name: metricName,
Value: float32(metricValue),
}
listOut.MetricValues = append(listOut.MetricValues, &metric)
logger.Info(fmt.Sprintf("surpressing error for externalCalculator '%s' by activating its fallback", ec.Name))
return &listOut, nil
}
Loading

0 comments on commit d13ae11

Please sign in to comment.