Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Brouwer <[email protected]>
  • Loading branch information
rickbrouwer committed Jan 6, 2025
1 parent 13c95df commit 7513c7f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
10 changes: 5 additions & 5 deletions pkg/fallback/fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func doFallback(scaledObject *kedav1alpha1.ScaledObject, metricSpec v2.MetricSpe
replicas := int64(scaledObject.Spec.Fallback.Replicas)

// Check if we should use current replicas as minimum
if scaledObject.Spec.Fallback.UseCurrentReplicasAsMinimum != nil &&
if scaledObject.Spec.Fallback.UseCurrentReplicasAsMinimum != nil &&
*scaledObject.Spec.Fallback.UseCurrentReplicasAsMinimum {
currentReplicasCount := int64(currentReplicas)
if currentReplicasCount > replicas {
Expand All @@ -137,10 +137,10 @@ func doFallback(scaledObject *kedav1alpha1.ScaledObject, metricSpec v2.MetricSpe
}
fallbackMetrics := []external_metrics.ExternalMetricValue{metric}

log.Info("Suppressing error, using fallback metrics",
"scaledObject.Namespace", scaledObject.Namespace,
"scaledObject.Name", scaledObject.Name,
"suppressedError", suppressedError,
log.Info("Suppressing error, using fallback metrics",
"scaledObject.Namespace", scaledObject.Namespace,
"scaledObject.Name", scaledObject.Name,
"suppressedError", suppressedError,
"fallback.replicas", replicas)
return fallbackMetrics
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/fallback/fallback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ var _ = Describe("fallback", func() {
mockScaleAndDeployment(ctrl, client, scaleClient, 5)

metrics, _, err := scaler.GetMetricsAndActivity(context.Background(), metricName)
metrics, _, err = GetMetricsWithFallback(context.Background(), client, scaleClient, metrics, err, metricName, so, metricSpec)
_, _, err = GetMetricsWithFallback(context.Background(), client, scaleClient, metrics, err, metricName, so, metricSpec)

Expect(err).ToNot(HaveOccurred())
condition := so.Status.Conditions.GetFallbackCondition()
Expect(condition.IsTrue()).Should(BeTrue())
Expand Down Expand Up @@ -368,7 +368,7 @@ var _ = Describe("fallback", func() {

metrics, _, err := scaler.GetMetricsAndActivity(context.Background(), metricName)
_, _, err = GetMetricsWithFallback(context.Background(), client, scaleClient, metrics, err, metricName, so, metricSpec)

Expect(err).ShouldNot(BeNil())
Expect(err.Error()).Should(Equal("some error"))
condition := so.Status.Conditions.GetFallbackCondition()
Expand All @@ -382,8 +382,8 @@ var _ = Describe("fallback", func() {

so := buildScaledObject(
&kedav1alpha1.Fallback{
FailureThreshold: int32(3),
Replicas: int32(10),
FailureThreshold: int32(3),
Replicas: int32(10),
UseCurrentReplicasAsMinimum: &useCurrentAsMin,
},
&kedav1alpha1.ScaledObjectStatus{
Expand Down Expand Up @@ -416,8 +416,8 @@ var _ = Describe("fallback", func() {

so := buildScaledObject(
&kedav1alpha1.Fallback{
FailureThreshold: int32(3),
Replicas: int32(10),
FailureThreshold: int32(3),
Replicas: int32(10),
UseCurrentReplicasAsMinimum: &useCurrentAsMin,
},
&kedav1alpha1.ScaledObjectStatus{
Expand Down Expand Up @@ -450,7 +450,7 @@ var _ = Describe("fallback", func() {
so := buildScaledObject(
&kedav1alpha1.Fallback{
FailureThreshold: int32(3),
Replicas: int32(10),
Replicas: int32(10),
// UseCurrentReplicasAsMinimum is nil
},
&kedav1alpha1.ScaledObjectStatus{
Expand Down
12 changes: 6 additions & 6 deletions pkg/scaling/executor/scale_scaledobjects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,8 @@ func TestScaleToFallbackWithCurrentReplicasAsMinimum(t *testing.T) {
Name: "name",
},
Fallback: &v1alpha1.Fallback{
FailureThreshold: 3,
Replicas: 5,
FailureThreshold: 3,
Replicas: 5,
UseCurrentReplicasAsMinimum: &useCurrentAsMin,
},
},
Expand Down Expand Up @@ -612,8 +612,8 @@ func TestScaleToFallbackIgnoringLowerCurrentReplicas(t *testing.T) {
Name: "name",
},
Fallback: &v1alpha1.Fallback{
FailureThreshold: 3,
Replicas: 5,
FailureThreshold: 3,
Replicas: 5,
UseCurrentReplicasAsMinimum: &useCurrentAsMin,
},
},
Expand Down Expand Up @@ -679,8 +679,8 @@ func TestScaleToFallbackWithoutCurrentReplicasAsMinimum(t *testing.T) {
Name: "name",
},
Fallback: &v1alpha1.Fallback{
FailureThreshold: 3,
Replicas: 5,
FailureThreshold: 3,
Replicas: 5,
UseCurrentReplicasAsMinimum: &useCurrentAsMin,
},
},
Expand Down
17 changes: 10 additions & 7 deletions pkg/scaling/resolver/scale_resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
corev1listers "k8s.io/client-go/listers/core/v1"
Expand All @@ -47,6 +47,9 @@ const (
boolTrue = true
boolFalse = false
defaultServiceAccount = "default"
appsGroup = "apps"
deploymentKind = "Deployment"
statefulSetKind = "StatefulSet"
)

var (
Expand Down Expand Up @@ -623,28 +626,28 @@ func GetCurrentReplicas(ctx context.Context, client client.Client, scaleClient s
targetName := scaledObject.Spec.ScaleTargetRef.Name
targetGVKR := scaledObject.Status.ScaleTargetGVKR

logger := log.WithValues("scaledObject.Namespace", scaledObject.Namespace,
"scaledObject.Name", scaledObject.Name,
"resource", fmt.Sprintf("%s/%s", targetGVKR.Group, targetGVKR.Kind),
logger := log.WithValues("scaledObject.Namespace", scaledObject.Namespace,
"scaledObject.Name", scaledObject.Name,
"resource", fmt.Sprintf("%s/%s", targetGVKR.Group, targetGVKR.Kind),
"name", targetName)

switch {
case targetGVKR.Group == "apps" && targetGVKR.Kind == "Deployment":
case targetGVKR.Group == appsGroup && targetGVKR.Kind == deploymentKind:
deployment := &appsv1.Deployment{}
if err := client.Get(ctx, types.NamespacedName{Name: targetName, Namespace: scaledObject.Namespace}, deployment); err != nil {
logger.Error(err, "target deployment doesn't exist")
return 0, err
}
return *deployment.Spec.Replicas, nil
case targetGVKR.Group == "apps" && targetGVKR.Kind == "StatefulSet":
case targetGVKR.Group == appsGroup && targetGVKR.Kind == statefulSetKind:
statefulSet := &appsv1.StatefulSet{}
if err := client.Get(ctx, types.NamespacedName{Name: targetName, Namespace: scaledObject.Namespace}, statefulSet); err != nil {
logger.Error(err, "target statefulset doesn't exist")
return 0, err
}
return *statefulSet.Spec.Replicas, nil
default:
scale, err := scaleClient.Scales(scaledObject.Namespace).Get(ctx, targetGVKR.GroupResource(), targetName, v1.GetOptions{})
scale, err := scaleClient.Scales(scaledObject.Namespace).Get(ctx, targetGVKR.GroupResource(), targetName, metav1.GetOptions{})
if err != nil {
logger.Error(err, "error getting scale subresource")
return 0, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/scaling/scale_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ func (h *scaleHandler) GetScaledObjectMetrics(ctx context.Context, scaledObjectN
metricTriggerPairList[key] = value
}
// check if we need to set a fallback
metrics, fallbackActive, err := fallback.GetMetricsWithFallback(ctx, h.client,h.scaleClient, result.metrics, result.err, result.metricName, scaledObject, result.metricSpec)
metrics, fallbackActive, err := fallback.GetMetricsWithFallback(ctx, h.client, h.scaleClient, result.metrics, result.err, result.metricName, scaledObject, result.metricSpec)
if err != nil {
isScalerError = true
logger.Error(err, "error getting metric for trigger", "trigger", result.triggerName)
Expand Down

0 comments on commit 7513c7f

Please sign in to comment.