diff --git a/pkg/fallback/fallback.go b/pkg/fallback/fallback.go index 29786705662..7c03a250c30 100644 --- a/pkg/fallback/fallback.go +++ b/pkg/fallback/fallback.go @@ -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 { @@ -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 } diff --git a/pkg/fallback/fallback_test.go b/pkg/fallback/fallback_test.go index b97ed5efd42..2982c0468e0 100644 --- a/pkg/fallback/fallback_test.go +++ b/pkg/fallback/fallback_test.go @@ -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()) @@ -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() @@ -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{ @@ -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{ @@ -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{ diff --git a/pkg/scaling/executor/scale_scaledobjects_test.go b/pkg/scaling/executor/scale_scaledobjects_test.go index 7d4b83f4aff..dab26113880 100644 --- a/pkg/scaling/executor/scale_scaledobjects_test.go +++ b/pkg/scaling/executor/scale_scaledobjects_test.go @@ -545,8 +545,8 @@ func TestScaleToFallbackWithCurrentReplicasAsMinimum(t *testing.T) { Name: "name", }, Fallback: &v1alpha1.Fallback{ - FailureThreshold: 3, - Replicas: 5, + FailureThreshold: 3, + Replicas: 5, UseCurrentReplicasAsMinimum: &useCurrentAsMin, }, }, @@ -612,8 +612,8 @@ func TestScaleToFallbackIgnoringLowerCurrentReplicas(t *testing.T) { Name: "name", }, Fallback: &v1alpha1.Fallback{ - FailureThreshold: 3, - Replicas: 5, + FailureThreshold: 3, + Replicas: 5, UseCurrentReplicasAsMinimum: &useCurrentAsMin, }, }, @@ -679,8 +679,8 @@ func TestScaleToFallbackWithoutCurrentReplicasAsMinimum(t *testing.T) { Name: "name", }, Fallback: &v1alpha1.Fallback{ - FailureThreshold: 3, - Replicas: 5, + FailureThreshold: 3, + Replicas: 5, UseCurrentReplicasAsMinimum: &useCurrentAsMin, }, }, diff --git a/pkg/scaling/resolver/scale_resolvers.go b/pkg/scaling/resolver/scale_resolvers.go index 4c6d648c6a8..4a57002c4fa 100644 --- a/pkg/scaling/resolver/scale_resolvers.go +++ b/pkg/scaling/resolver/scale_resolvers.go @@ -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" @@ -47,6 +47,9 @@ const ( boolTrue = true boolFalse = false defaultServiceAccount = "default" + appsGroup = "apps" + deploymentKind = "Deployment" + statefulSetKind = "StatefulSet" ) var ( @@ -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") @@ -644,7 +647,7 @@ func GetCurrentReplicas(ctx context.Context, client client.Client, scaleClient s } 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 diff --git a/pkg/scaling/scale_handler.go b/pkg/scaling/scale_handler.go index 813baf4006d..f67496b288c 100644 --- a/pkg/scaling/scale_handler.go +++ b/pkg/scaling/scale_handler.go @@ -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)