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 5, 2025
1 parent 13c95df commit f6fbcf4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 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
12 changes: 6 additions & 6 deletions pkg/fallback/fallback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ 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()
Expand Down Expand Up @@ -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
15 changes: 9 additions & 6 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"

Check failure on line 29 in pkg/scaling/resolver/scale_resolvers.go

View workflow job for this annotation

GitHub Actions / Static Checks

"k8s.io/apimachinery/pkg/apis/meta/v1" imported as metav1 and not used

Check failure on line 29 in pkg/scaling/resolver/scale_resolvers.go

View workflow job for this annotation

GitHub Actions / Static Checks

"k8s.io/apimachinery/pkg/apis/meta/v1" imported as metav1 and not used

Check failure on line 29 in pkg/scaling/resolver/scale_resolvers.go

View workflow job for this annotation

GitHub Actions / Static Checks

"k8s.io/apimachinery/pkg/apis/meta/v1" imported as metav1 and not used

Check failure on line 29 in pkg/scaling/resolver/scale_resolvers.go

View workflow job for this annotation

GitHub Actions / Static Checks

"k8s.io/apimachinery/pkg/apis/meta/v1" imported as metav1 and not used
"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,20 +626,20 @@ 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")
Expand Down

0 comments on commit f6fbcf4

Please sign in to comment.