diff --git a/pkg/scaling/scale_handler.go b/pkg/scaling/scale_handler.go index d77a9f5c53d..badbe2ea0e9 100644 --- a/pkg/scaling/scale_handler.go +++ b/pkg/scaling/scale_handler.go @@ -43,7 +43,7 @@ import ( "github.com/kedacore/keda/v2/pkg/scaling/cache/metricscache" "github.com/kedacore/keda/v2/pkg/scaling/executor" "github.com/kedacore/keda/v2/pkg/scaling/resolver" - "github.com/kedacore/keda/v2/pkg/scaling/utils" + "github.com/kedacore/keda/v2/pkg/scaling/scaledjob" ) var log = logf.Log.WithName("scale_handler") @@ -630,7 +630,6 @@ type scalerMetrics struct { // getScaledJobMetrics returns metrics for specified metric name for a ScaledJob identified by its name and namespace. // It could either query the metric value directly from the scaler or from a cache, that's being stored for the scaler. func (h *scaleHandler) getScaledJobMetrics(ctx context.Context, scaledJob *kedav1alpha1.ScaledJob) []scalerMetrics { - // TODO this loop should be probably done similar way the ScaledObject loop is done cache, err := h.GetScalersCache(ctx, scaledJob) if err != nil { log.Error(err, "error getting scalers cache", "scaledJob.Namespace", scaledJob.Namespace, "scaledJob.Name", scaledJob.Name) @@ -655,7 +654,6 @@ func (h *scaleHandler) getScaledJobMetrics(ctx context.Context, scaledJob *kedav continue } - // TODO here we should probably loop through all metrics in a Scaler metrics, isTriggerActive, _, err := cache.GetMetricsAndActivityForScaler(ctx, i, metricSpecs[0].External.Metric.Name) if err != nil { scalerLogger.V(1).Info("Error getting scaler metrics and activity, but continue", "error", err) @@ -663,7 +661,7 @@ func (h *scaleHandler) getScaledJobMetrics(ctx context.Context, scaledJob *kedav continue } - targetAverageValue = utils.GetTargetAverageValue(metricSpecs) + targetAverageValue = scaledjob.GetTargetAverageValue(metricSpecs) var metricValue float64 for _, m := range metrics { diff --git a/pkg/scaling/scale_handler_test.go b/pkg/scaling/scale_handler_test.go index 80f0c8c3b59..f057e0026ef 100644 --- a/pkg/scaling/scale_handler_test.go +++ b/pkg/scaling/scale_handler_test.go @@ -41,7 +41,7 @@ import ( "github.com/kedacore/keda/v2/pkg/scalers" "github.com/kedacore/keda/v2/pkg/scaling/cache" "github.com/kedacore/keda/v2/pkg/scaling/cache/metricscache" - "github.com/kedacore/keda/v2/pkg/scaling/utils" + "github.com/kedacore/keda/v2/pkg/scaling/scaledjob" ) func TestGetScaledObjectMetrics_DirectCall(t *testing.T) { @@ -56,7 +56,7 @@ func TestGetScaledObjectMetrics_DirectCall(t *testing.T) { mockExecutor := mock_executor.NewMockScaleExecutor(ctrl) mockStatusWriter := mock_client.NewMockStatusWriter(ctrl) - metricsSpecs := []v2.MetricSpec{utils.CreateMetricSpec(10, metricName)} + metricsSpecs := []v2.MetricSpec{scaledjob.CreateMetricSpec(10, metricName)} metricValue := scalers.GenerateMetricInMili(metricName, float64(10)) metricsRecord := map[string]metricscache.MetricsRecord{} @@ -147,7 +147,7 @@ func TestGetScaledObjectMetrics_FromCache(t *testing.T) { mockExecutor := mock_executor.NewMockScaleExecutor(ctrl) mockStatusWriter := mock_client.NewMockStatusWriter(ctrl) - metricsSpecs := []v2.MetricSpec{utils.CreateMetricSpec(10, metricName)} + metricsSpecs := []v2.MetricSpec{scaledjob.CreateMetricSpec(10, metricName)} metricValue := scalers.GenerateMetricInMili(metricName, float64(10)) metricsRecord := map[string]metricscache.MetricsRecord{} @@ -231,7 +231,7 @@ func TestCheckScaledObjectScalersWithError(t *testing.T) { mockExecutor := mock_executor.NewMockScaleExecutor(ctrl) recorder := record.NewFakeRecorder(1) - metricsSpecs := []v2.MetricSpec{utils.CreateMetricSpec(1, "metric-name")} + metricsSpecs := []v2.MetricSpec{scaledjob.CreateMetricSpec(1, "metric-name")} scaler := mock_scalers.NewMockScaler(ctrl) scaler.EXPECT().GetMetricSpecForScaling(gomock.Any()).Return(metricsSpecs) @@ -292,7 +292,7 @@ func TestCheckScaledObjectFindFirstActiveNotIgnoreOthers(t *testing.T) { mockExecutor := mock_executor.NewMockScaleExecutor(ctrl) recorder := record.NewFakeRecorder(1) - metricsSpecs := []v2.MetricSpec{utils.CreateMetricSpec(1, "metric-name")} + metricsSpecs := []v2.MetricSpec{scaledjob.CreateMetricSpec(1, "metric-name")} activeFactory := func() (scalers.Scaler, *scalers.ScalerConfig, error) { scaler := mock_scalers.NewMockScaler(ctrl) @@ -603,7 +603,7 @@ func createScaledJob(minReplicaCount int32, maxReplicaCount int32, multipleScale func createScaler(ctrl *gomock.Controller, queueLength int64, averageValue int64, isActive bool, metricName string) *mock_scalers.MockScaler { scaler := mock_scalers.NewMockScaler(ctrl) - metricsSpecs := []v2.MetricSpec{utils.CreateMetricSpec(averageValue, metricName)} + metricsSpecs := []v2.MetricSpec{scaledjob.CreateMetricSpec(averageValue, metricName)} metrics := []external_metrics.ExternalMetricValue{ { diff --git a/pkg/scaling/utils/metrics.go b/pkg/scaling/scaledjob/metrics.go similarity index 83% rename from pkg/scaling/utils/metrics.go rename to pkg/scaling/scaledjob/metrics.go index c1a98ddda42..d535dbe0d25 100644 --- a/pkg/scaling/utils/metrics.go +++ b/pkg/scaling/scaledjob/metrics.go @@ -1,10 +1,11 @@ -package utils +package scaledjob import ( v2 "k8s.io/api/autoscaling/v2" "k8s.io/apimachinery/pkg/api/resource" ) +// GetTargetAverageValue returns the average of all the metrics' average value. func GetTargetAverageValue(metricSpecs []v2.MetricSpec) float64 { var totalAverageValue float64 var metricValue float64 @@ -24,6 +25,7 @@ func GetTargetAverageValue(metricSpecs []v2.MetricSpec) float64 { return 0 } +// CreateMetricSpec creates MetricSpec for given metric name and target value. func CreateMetricSpec(averageValue int64, metricName string) v2.MetricSpec { qty := resource.NewQuantity(averageValue, resource.DecimalSI) return v2.MetricSpec{ diff --git a/pkg/scaling/utils/metrics_test.go b/pkg/scaling/scaledjob/metrics_test.go similarity index 98% rename from pkg/scaling/utils/metrics_test.go rename to pkg/scaling/scaledjob/metrics_test.go index f96b5446a69..db3a5a92f40 100644 --- a/pkg/scaling/utils/metrics_test.go +++ b/pkg/scaling/scaledjob/metrics_test.go @@ -1,4 +1,4 @@ -package utils +package scaledjob import ( "testing"