From 1c57a93700de42267f0b3d5ce2674001c331397b Mon Sep 17 00:00:00 2001 From: Jorge Turrado Ferrero Date: Tue, 14 Jun 2022 20:08:10 +0200 Subject: [PATCH] feat: Use mili scale to support more precision (#3125) --- .gitignore | 3 + CHANGELOG.md | 1 + pkg/scalers/activemq_scaler.go | 8 +-- pkg/scalers/artemis_scaler.go | 8 +-- pkg/scalers/aws_cloudwatch_scaler.go | 40 ++++++++----- pkg/scalers/aws_dynamodb_scaler.go | 12 +--- pkg/scalers/aws_kinesis_stream_scaler.go | 8 +-- pkg/scalers/aws_sqs_queue_scaler.go | 8 +-- pkg/scalers/azure/azure_app_insights.go | 7 +-- pkg/scalers/azure/azure_app_insights_test.go | 4 +- pkg/scalers/azure/azure_data_explorer.go | 12 ++-- pkg/scalers/azure/azure_monitor.go | 10 +--- pkg/scalers/azure_app_insights_scaler.go | 14 ++--- pkg/scalers/azure_blob_scaler.go | 8 +-- pkg/scalers/azure_data_explorer_scaler.go | 12 +--- pkg/scalers/azure_eventhub_scaler.go | 8 +-- pkg/scalers/azure_log_analytics_scaler.go | 30 ++++------ pkg/scalers/azure_monitor_scaler.go | 14 ++--- pkg/scalers/azure_pipelines_scaler.go | 8 +-- pkg/scalers/azure_queue_scaler.go | 8 +-- pkg/scalers/azure_servicebus_scaler.go | 8 +-- pkg/scalers/cassandra_scaler.go | 8 +-- pkg/scalers/cron_scaler.go | 8 +-- pkg/scalers/datadog_scaler.go | 14 ++--- pkg/scalers/elasticsearch_scaler.go | 16 ++--- pkg/scalers/external_scaler.go | 9 +-- pkg/scalers/gcp_pubsub_scaler.go | 8 +-- pkg/scalers/gcp_stackdriver_scaler.go | 8 +-- pkg/scalers/gcp_storage_scaler.go | 8 +-- pkg/scalers/graphite_scaler.go | 14 ++--- pkg/scalers/huawei_cloudeye_scaler.go | 11 +--- pkg/scalers/ibmmq_scaler.go | 13 +---- pkg/scalers/influxdb_scaler.go | 10 +--- pkg/scalers/kafka_scaler.go | 8 +-- pkg/scalers/kubernetes_workload_scaler.go | 14 ++--- pkg/scalers/liiklus_scaler.go | 8 +-- pkg/scalers/metrics_api_scaler.go | 41 ++++++------- pkg/scalers/metrics_api_scaler_test.go | 16 ++--- pkg/scalers/mongo_scaler.go | 8 +-- pkg/scalers/mssql_scaler.go | 18 ++---- pkg/scalers/mssql_scaler_test.go | 4 +- pkg/scalers/mysql_scaler.go | 18 ++---- pkg/scalers/newrelic_scaler.go | 14 ++--- pkg/scalers/openstack_metrics_scaler.go | 10 +--- pkg/scalers/openstack_swift_scaler.go | 8 +-- pkg/scalers/postgresql_scaler.go | 18 ++---- pkg/scalers/predictkube_scaler.go | 28 ++++----- pkg/scalers/predictkube_scaler_test.go | 2 +- pkg/scalers/prometheus_scaler.go | 14 ++--- pkg/scalers/rabbitmq_scaler.go | 22 +++---- pkg/scalers/redis_scaler.go | 8 +-- pkg/scalers/redis_streams_scaler.go | 8 +-- pkg/scalers/scaler.go | 29 ++++++++++ pkg/scalers/selenium_grid_scaler.go | 10 +--- pkg/scalers/solace_scaler.go | 14 +---- pkg/scalers/stan_scaler.go | 9 +-- pkg/scaling/cache/scalers_cache.go | 61 +++++++++----------- pkg/scaling/cache/scalers_cache_test.go | 14 ++--- 58 files changed, 255 insertions(+), 507 deletions(-) diff --git a/.gitignore b/.gitignore index ab105f41d86..46c16ccef07 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,6 @@ cover.out # GO debug binary cmd/manager/debug.test + +# GO Test result +report.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index f8b803efeda..be5c72c658b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ To learn more about our roadmap, we recommend reading [this document](ROADMAP.md ### Improvements +- **General:** Use `mili` scale for the returned metrics ([#3135](https://github.com/kedacore/keda/issue/3135)) - **General:** Use more readable timestamps in KEDA Operator logs ([#3066](https://github.com/kedacore/keda/issue/3066)) - **Prometheus Scaler:** Add ignoreNullValues to return error when prometheus return null in values ([#3065](https://github.com/kedacore/keda/issues/3065)) - **Selenium Grid Scaler:** Edge active sessions not being properly counted ([#2709](https://github.com/kedacore/keda/issues/2709)) diff --git a/pkg/scalers/activemq_scaler.go b/pkg/scalers/activemq_scaler.go index 94913e9cae9..5270c0f0312 100644 --- a/pkg/scalers/activemq_scaler.go +++ b/pkg/scalers/activemq_scaler.go @@ -13,8 +13,6 @@ import ( "text/template" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -275,11 +273,7 @@ func (s *activeMQScaler) GetMetrics(ctx context.Context, metricName string, metr return nil, fmt.Errorf("error inspecting ActiveMQ queue size: %s", err) } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(queueSize, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(queueSize)) return []external_metrics.ExternalMetricValue{metric}, nil } diff --git a/pkg/scalers/artemis_scaler.go b/pkg/scalers/artemis_scaler.go index 53e9f184840..e13d4eda12f 100644 --- a/pkg/scalers/artemis_scaler.go +++ b/pkg/scalers/artemis_scaler.go @@ -11,8 +11,6 @@ import ( "strings" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -277,11 +275,7 @@ func (s *artemisScaler) GetMetrics(ctx context.Context, metricName string, metri return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(messages, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(messages)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/aws_cloudwatch_scaler.go b/pkg/scalers/aws_cloudwatch_scaler.go index fe8cb2795db..a163cfd0643 100644 --- a/pkg/scalers/aws_cloudwatch_scaler.go +++ b/pkg/scalers/aws_cloudwatch_scaler.go @@ -14,8 +14,6 @@ import ( "github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -43,8 +41,8 @@ type awsCloudwatchMetadata struct { dimensionValue []string expression string - targetMetricValue int64 - minMetricValue int64 + targetMetricValue float64 + minMetricValue float64 metricCollectionTime int64 metricStat string @@ -96,6 +94,22 @@ func getIntMetadataValue(metadata map[string]string, key string, required bool, return defaultValue, nil } +func getFloatMetadataValue(metadata map[string]string, key string, required bool, defaultValue float64) (float64, error) { + if val, ok := metadata[key]; ok && val != "" { + value, err := strconv.ParseFloat(val, 64) + if err != nil { + return 0, fmt.Errorf("error parsing %s metadata: %v", key, err) + } + return value, nil + } + + if required { + return 0, fmt.Errorf("metadata %s not given", key) + } + + return defaultValue, nil +} + func createCloudwatchClient(metadata *awsCloudwatchMetadata) *cloudwatch.CloudWatch { sess := session.Must(session.NewSession(&aws.Config{ Region: aws.String(metadata.awsRegion), @@ -167,12 +181,12 @@ func parseAwsCloudwatchMetadata(config *ScalerConfig) (*awsCloudwatchMetadata, e } } - meta.targetMetricValue, err = getIntMetadataValue(config.TriggerMetadata, "targetMetricValue", true, 0) + meta.targetMetricValue, err = getFloatMetadataValue(config.TriggerMetadata, "targetMetricValue", true, 0) if err != nil { return nil, err } - meta.minMetricValue, err = getIntMetadataValue(config.TriggerMetadata, "minMetricValue", true, 0) + meta.minMetricValue, err = getFloatMetadataValue(config.TriggerMetadata, "minMetricValue", true, 0) if err != nil { return nil, err } @@ -278,11 +292,7 @@ func (c *awsCloudwatchScaler) GetMetrics(ctx context.Context, metricName string, return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(metricValue, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, metricValue) return append([]external_metrics.ExternalMetricValue{}, metric), nil } @@ -300,7 +310,7 @@ func (c *awsCloudwatchScaler) GetMetricSpecForScaling(context.Context) []v2beta2 Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(c.metadata.scalerIndex, kedautil.NormalizeString(fmt.Sprintf("aws-cloudwatch-%s", metricNameSuffix))), }, - Target: GetMetricTarget(c.metricType, c.metadata.targetMetricValue), + Target: GetMetricTargetMili(c.metricType, c.metadata.targetMetricValue), } metricSpec := v2beta2.MetricSpec{External: externalMetric, Type: externalMetricType} return []v2beta2.MetricSpec{metricSpec} @@ -320,7 +330,7 @@ func (c *awsCloudwatchScaler) Close(context.Context) error { return nil } -func (c *awsCloudwatchScaler) GetCloudwatchMetrics() (int64, error) { +func (c *awsCloudwatchScaler) GetCloudwatchMetrics() (float64, error) { var input cloudwatch.GetMetricDataInput startTime, endTime := computeQueryWindow(time.Now(), c.metadata.metricStatPeriod, c.metadata.metricEndTimeOffset, c.metadata.metricCollectionTime) @@ -384,9 +394,9 @@ func (c *awsCloudwatchScaler) GetCloudwatchMetrics() (int64, error) { } cloudwatchLog.V(1).Info("Received Metric Data", "data", output) - var metricValue int64 + var metricValue float64 if len(output.MetricDataResults) > 0 && len(output.MetricDataResults[0].Values) > 0 { - metricValue = int64(*output.MetricDataResults[0].Values[0]) + metricValue = *output.MetricDataResults[0].Values[0] } else { cloudwatchLog.Info("empty metric data received, returning minMetricValue") metricValue = c.metadata.minMetricValue diff --git a/pkg/scalers/aws_dynamodb_scaler.go b/pkg/scalers/aws_dynamodb_scaler.go index e4a143f8820..b2985946cae 100644 --- a/pkg/scalers/aws_dynamodb_scaler.go +++ b/pkg/scalers/aws_dynamodb_scaler.go @@ -15,8 +15,6 @@ import ( "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface" "go.mongodb.org/mongo-driver/bson" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -168,11 +166,7 @@ func (c *awsDynamoDBScaler) GetMetrics(ctx context.Context, metricName string, m return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(metricValue, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, metricValue) return append([]external_metrics.ExternalMetricValue{}, metric), nil } @@ -204,7 +198,7 @@ func (c *awsDynamoDBScaler) Close(context.Context) error { return nil } -func (c *awsDynamoDBScaler) GetQueryMetrics() (int64, error) { +func (c *awsDynamoDBScaler) GetQueryMetrics() (float64, error) { dimensions := dynamodb.QueryInput{ TableName: aws.String(c.metadata.tableName), KeyConditionExpression: aws.String(c.metadata.keyConditionExpression), @@ -218,7 +212,7 @@ func (c *awsDynamoDBScaler) GetQueryMetrics() (int64, error) { return 0, err } - return *res.Count, nil + return float64(*res.Count), nil } // json2Map convert Json to map[string]string diff --git a/pkg/scalers/aws_kinesis_stream_scaler.go b/pkg/scalers/aws_kinesis_stream_scaler.go index 2803a9ab96d..d3973ae8120 100644 --- a/pkg/scalers/aws_kinesis_stream_scaler.go +++ b/pkg/scalers/aws_kinesis_stream_scaler.go @@ -12,8 +12,6 @@ import ( "github.com/aws/aws-sdk-go/service/kinesis" "github.com/aws/aws-sdk-go/service/kinesis/kinesisiface" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -158,11 +156,7 @@ func (s *awsKinesisStreamScaler) GetMetrics(ctx context.Context, metricName stri return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(shardCount, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(shardCount)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/aws_sqs_queue_scaler.go b/pkg/scalers/aws_sqs_queue_scaler.go index 4dfff673a50..55c4edf2eb6 100644 --- a/pkg/scalers/aws_sqs_queue_scaler.go +++ b/pkg/scalers/aws_sqs_queue_scaler.go @@ -14,8 +14,6 @@ import ( "github.com/aws/aws-sdk-go/service/sqs" "github.com/aws/aws-sdk-go/service/sqs/sqsiface" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -181,11 +179,7 @@ func (s *awsSqsQueueScaler) GetMetrics(ctx context.Context, metricName string, m return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(queuelen, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(queuelen)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/azure/azure_app_insights.go b/pkg/scalers/azure/azure_app_insights.go index 22a0371f97e..c1993127dcc 100644 --- a/pkg/scalers/azure/azure_app_insights.go +++ b/pkg/scalers/azure/azure_app_insights.go @@ -3,7 +3,6 @@ package azure import ( "context" "fmt" - "math" "net/http" "strconv" "strings" @@ -77,7 +76,7 @@ func getAuthConfig(ctx context.Context, info AppInsightsInfo, podIdentity kedav1 return nil } -func extractAppInsightValue(info AppInsightsInfo, metric ApplicationInsightsMetric) (int64, error) { +func extractAppInsightValue(info AppInsightsInfo, metric ApplicationInsightsMetric) (float64, error) { if _, ok := metric.Value[info.MetricID]; !ok { return -1, fmt.Errorf("metric named %s not found in app insights response", info.MetricID) } @@ -94,7 +93,7 @@ func extractAppInsightValue(info AppInsightsInfo, metric ApplicationInsightsMetr azureAppInsightsLog.V(2).Info("value extracted from metric request", "metric type", info.AggregationType, "metric value", floatVal) - return int64(math.Round(floatVal)), nil + return floatVal, nil } func queryParamsForAppInsightsRequest(info AppInsightsInfo) (map[string]interface{}, error) { @@ -115,7 +114,7 @@ func queryParamsForAppInsightsRequest(info AppInsightsInfo) (map[string]interfac } // GetAzureAppInsightsMetricValue returns the value of an Azure App Insights metric, rounded to the nearest int -func GetAzureAppInsightsMetricValue(ctx context.Context, info AppInsightsInfo, podIdentity kedav1alpha1.PodIdentityProvider) (int64, error) { +func GetAzureAppInsightsMetricValue(ctx context.Context, info AppInsightsInfo, podIdentity kedav1alpha1.PodIdentityProvider) (float64, error) { config := getAuthConfig(ctx, info, podIdentity) authorizer, err := config.Authorizer() if err != nil { diff --git a/pkg/scalers/azure/azure_app_insights_test.go b/pkg/scalers/azure/azure_app_insights_test.go index b7006f13a7e..433d92f933d 100644 --- a/pkg/scalers/azure/azure_app_insights_test.go +++ b/pkg/scalers/azure/azure_app_insights_test.go @@ -12,7 +12,7 @@ import ( type testExtractAzAppInsightsTestData struct { testName string isError bool - expectedValue int64 + expectedValue float64 info AppInsightsInfo metricResult ApplicationInsightsMetric } @@ -48,8 +48,6 @@ var testExtractAzAppInsightsData = []testExtractAzAppInsightsTestData{ {"metric not found", true, -1, mockAppInsightsInfo("avg"), mockAppInsightsMetric("test/test", "avg", newMetricValue(0.0))}, {"metric is nil", true, -1, mockAppInsightsInfo("avg"), mockAppInsightsMetric("testns/test", "avg", nil)}, {"incorrect aggregation type", true, -1, mockAppInsightsInfo("avg"), mockAppInsightsMetric("testns/test", "max", newMetricValue(0.0))}, - {"success round down value", false, 5, mockAppInsightsInfo("max"), mockAppInsightsMetric("testns/test", "max", newMetricValue(5.2))}, - {"success round up value", false, 6, mockAppInsightsInfo("max"), mockAppInsightsMetric("testns/test", "max", newMetricValue(5.5))}, } func TestAzGetAzureAppInsightsMetricValue(t *testing.T) { diff --git a/pkg/scalers/azure/azure_data_explorer.go b/pkg/scalers/azure/azure_data_explorer.go index 3bc5d1bc840..66c054670dd 100644 --- a/pkg/scalers/azure/azure_data_explorer.go +++ b/pkg/scalers/azure/azure_data_explorer.go @@ -40,7 +40,7 @@ type DataExplorerMetadata struct { PodIdentity kedav1alpha1.PodIdentityProvider Query string TenantID string - Threshold int64 + Threshold float64 ActiveDirectoryEndpoint string } @@ -95,7 +95,7 @@ func getDataExplorerAuthConfig(ctx context.Context, metadata *DataExplorerMetada return nil, fmt.Errorf("missing credentials. please reconfigure your scaled object metadata") } -func GetAzureDataExplorerMetricValue(ctx context.Context, client *kusto.Client, db string, query string) (int64, error) { +func GetAzureDataExplorerMetricValue(ctx context.Context, client *kusto.Client, db string, query string) (float64, error) { azureDataExplorerLogger.V(1).Info("Querying Azure Data Explorer", "db", db, "query", query) iter, err := client.Query(ctx, db, kusto.NewStmt("", kusto.UnsafeStmt(unsafe.Stmt{Add: true, SuppressWarning: false})).UnsafeAdd(query)) @@ -130,7 +130,7 @@ func GetAzureDataExplorerMetricValue(ctx context.Context, client *kusto.Client, return metricValue, nil } -func extractDataExplorerMetricValue(row *table.Row) (int64, error) { +func extractDataExplorerMetricValue(row *table.Row) (float64, error) { if row == nil || len(row.ColumnTypes) == 0 { return -1, fmt.Errorf("query has no results") } @@ -141,14 +141,14 @@ func extractDataExplorerMetricValue(row *table.Row) (int64, error) { return -1, fmt.Errorf("data type %s is not valid", dataType) } - value, err := strconv.Atoi(row.Values[0].String()) + value, err := strconv.ParseFloat(row.Values[0].String(), 64) if err != nil { return -1, fmt.Errorf("failed to convert result %s to int", row.Values[0].String()) } if value < 0 { - return -1, fmt.Errorf("query result must be >= 0 but received: %d", value) + return -1, fmt.Errorf("query result must be >= 0 but received: %f", value) } azureDataExplorerLogger.V(1).Info("Query Result", "value", value, "dataType", dataType) - return int64(value), nil + return value, nil } diff --git a/pkg/scalers/azure/azure_monitor.go b/pkg/scalers/azure/azure_monitor.go index c4fd65b9f75..9c5eb63b7a2 100644 --- a/pkg/scalers/azure/azure_monitor.go +++ b/pkg/scalers/azure/azure_monitor.go @@ -19,7 +19,6 @@ package azure import ( "context" "fmt" - "math" "strconv" "strings" "time" @@ -67,7 +66,7 @@ type MonitorInfo struct { var azureMonitorLog = logf.Log.WithName("azure_monitor_scaler") // GetAzureMetricValue returns the value of an Azure Monitor metric, rounded to the nearest int -func GetAzureMetricValue(ctx context.Context, info MonitorInfo, podIdentity kedav1alpha1.PodIdentityProvider) (int64, error) { +func GetAzureMetricValue(ctx context.Context, info MonitorInfo, podIdentity kedav1alpha1.PodIdentityProvider) (float64, error) { client := createMetricsClient(ctx, info, podIdentity) requestPtr, err := createMetricsRequest(info) if err != nil { @@ -128,16 +127,13 @@ func createMetricsRequest(info MonitorInfo) (*azureExternalMetricRequest, error) return &metricRequest, nil } -func executeRequest(ctx context.Context, client insights.MetricsClient, request *azureExternalMetricRequest) (int64, error) { +func executeRequest(ctx context.Context, client insights.MetricsClient, request *azureExternalMetricRequest) (float64, error) { metricResponse, err := getAzureMetric(ctx, client, *request) if err != nil { return -1, fmt.Errorf("error getting azure monitor metric %s: %w", request.MetricName, err) } - // casting drops everything after decimal, so round first - metricValue := int64(math.Round(metricResponse)) - - return metricValue, nil + return metricResponse, nil } func getAzureMetric(ctx context.Context, client insights.MetricsClient, azMetricRequest azureExternalMetricRequest) (float64, error) { diff --git a/pkg/scalers/azure_app_insights_scaler.go b/pkg/scalers/azure_app_insights_scaler.go index c8c0b197160..2e088c94a26 100644 --- a/pkg/scalers/azure_app_insights_scaler.go +++ b/pkg/scalers/azure_app_insights_scaler.go @@ -7,8 +7,6 @@ import ( "strings" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -30,7 +28,7 @@ const ( type azureAppInsightsMetadata struct { azureAppInsightsInfo azure.AppInsightsInfo - targetValue int64 + targetValue float64 scalerIndex int } @@ -70,7 +68,7 @@ func parseAzureAppInsightsMetadata(config *ScalerConfig) (*azureAppInsightsMetad if err != nil { return nil, err } - meta.targetValue, err = strconv.ParseInt(val, 10, 64) + meta.targetValue, err = strconv.ParseFloat(val, 64) if err != nil { azureAppInsightsLog.Error(err, "Error parsing azure app insights metadata", "targetValue", targetValueName) return nil, fmt.Errorf("error parsing azure app insights metadata %s: %s", targetValueName, err.Error()) @@ -172,7 +170,7 @@ func (s *azureAppInsightsScaler) GetMetricSpecForScaling(context.Context) []v2be Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, kedautil.NormalizeString(fmt.Sprintf("azure-app-insights-%s", s.metadata.azureAppInsightsInfo.MetricID))), }, - Target: GetMetricTarget(s.metricType, s.metadata.targetValue), + Target: GetMetricTargetMili(s.metricType, s.metadata.targetValue), } metricSpec := v2beta2.MetricSpec{External: externalMetric, Type: externalMetricType} return []v2beta2.MetricSpec{metricSpec} @@ -186,11 +184,7 @@ func (s *azureAppInsightsScaler) GetMetrics(ctx context.Context, metricName stri return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(val, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, val) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/azure_blob_scaler.go b/pkg/scalers/azure_blob_scaler.go index 6f46e2f5b8f..96a01ead976 100644 --- a/pkg/scalers/azure_blob_scaler.go +++ b/pkg/scalers/azure_blob_scaler.go @@ -24,8 +24,6 @@ import ( "github.com/gobwas/glob" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -215,11 +213,7 @@ func (s *azureBlobScaler) GetMetrics(ctx context.Context, metricName string, met return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(bloblen, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(bloblen)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/azure_data_explorer_scaler.go b/pkg/scalers/azure_data_explorer_scaler.go index 38952bfab3a..ba30eaf1111 100644 --- a/pkg/scalers/azure_data_explorer_scaler.go +++ b/pkg/scalers/azure_data_explorer_scaler.go @@ -23,8 +23,6 @@ import ( "github.com/Azure/azure-kusto-go/kusto" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -100,7 +98,7 @@ func parseAzureDataExplorerMetadata(config *ScalerConfig) (*azure.DataExplorerMe // Get threshold. if val, ok := config.TriggerMetadata["threshold"]; ok { - threshold, err := strconv.ParseInt(val, 10, 64) + threshold, err := strconv.ParseFloat(val, 64) if err != nil { return nil, fmt.Errorf("error parsing metadata. Details: can't parse threshold. Inner Error: %v", err) } @@ -166,11 +164,7 @@ func (s azureDataExplorerScaler) GetMetrics(ctx context.Context, metricName stri return []external_metrics.ExternalMetricValue{}, fmt.Errorf("failed to get metrics for scaled object %s in namespace %s: %v", s.name, s.namespace, err) } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(metricValue, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, metricValue) return append([]external_metrics.ExternalMetricValue{}, metric), nil } @@ -179,7 +173,7 @@ func (s azureDataExplorerScaler) GetMetricSpecForScaling(context.Context) []v2be Metric: v2beta2.MetricIdentifier{ Name: s.metadata.MetricName, }, - Target: GetMetricTarget(s.metricType, s.metadata.Threshold), + Target: GetMetricTargetMili(s.metricType, s.metadata.Threshold), } metricSpec := v2beta2.MetricSpec{External: externalMetric, Type: externalMetricType} return []v2beta2.MetricSpec{metricSpec} diff --git a/pkg/scalers/azure_eventhub_scaler.go b/pkg/scalers/azure_eventhub_scaler.go index a94717f8042..a5b02f2fc51 100644 --- a/pkg/scalers/azure_eventhub_scaler.go +++ b/pkg/scalers/azure_eventhub_scaler.go @@ -29,8 +29,6 @@ import ( "github.com/Azure/azure-storage-blob-go/azblob" az "github.com/Azure/go-autorest/autorest/azure" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -335,11 +333,7 @@ func (scaler *azureEventHubScaler) GetMetrics(ctx context.Context, metricName st eventhubLog.V(1).Info(fmt.Sprintf("Unprocessed events in event hub total: %d, scaling for a lag of %d related to %d partitions", totalUnprocessedEventCount, lagRelatedToPartitionCount, len(partitionIDs))) - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(lagRelatedToPartitionCount, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(lagRelatedToPartitionCount)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/azure_log_analytics_scaler.go b/pkg/scalers/azure_log_analytics_scaler.go index 96b9243b6f2..69182a49d96 100644 --- a/pkg/scalers/azure_log_analytics_scaler.go +++ b/pkg/scalers/azure_log_analytics_scaler.go @@ -33,8 +33,6 @@ import ( "github.com/Azure/azure-amqp-common-go/v3/auth" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -67,7 +65,7 @@ type azureLogAnalyticsMetadata struct { workspaceID string podIdentity kedav1alpha1.PodIdentityProvider query string - threshold int64 + threshold float64 metricName string // Custom metric name for trigger scalerIndex int logAnalyticsResourceURL string @@ -75,8 +73,8 @@ type azureLogAnalyticsMetadata struct { } type sessionCache struct { - metricValue int64 - metricThreshold int64 + metricValue float64 + metricThreshold float64 } type tokenData struct { @@ -91,8 +89,8 @@ type tokenData struct { } type metricsData struct { - value int64 - threshold int64 + value float64 + threshold float64 } type queryResult struct { @@ -192,7 +190,7 @@ func parseAzureLogAnalyticsMetadata(config *ScalerConfig) (*azureLogAnalyticsMet if err != nil { return nil, err } - threshold, err := strconv.ParseInt(val, 10, 64) + threshold, err := strconv.ParseFloat(val, 64) if err != nil { return nil, fmt.Errorf("error parsing metadata. Details: can't parse threshold. Inner Error: %v", err) } @@ -267,7 +265,7 @@ func (s *azureLogAnalyticsScaler) GetMetricSpecForScaling(ctx context.Context) [ Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, s.metadata.metricName), }, - Target: GetMetricTarget(s.metricType, s.cache.metricThreshold), + Target: GetMetricTargetMili(s.metricType, s.cache.metricThreshold), } metricSpec := v2beta2.MetricSpec{External: externalMetric, Type: externalMetricType} return []v2beta2.MetricSpec{metricSpec} @@ -281,11 +279,7 @@ func (s *azureLogAnalyticsScaler) GetMetrics(ctx context.Context, metricName str return []external_metrics.ExternalMetricValue{}, fmt.Errorf("failed to get metrics. Scaled object: %s. Namespace: %s. Inner Error: %v", s.name, s.namespace, err) } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(receivedMetric.value, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, receivedMetric.value) return append([]external_metrics.ExternalMetricValue{}, metric), nil } @@ -428,7 +422,7 @@ func (s *azureLogAnalyticsScaler) executeQuery(ctx context.Context, query string if len(queryData.Tables[0].Rows[0]) > 0 { metricDataType := queryData.Tables[0].Columns[0].Type metricVal := queryData.Tables[0].Rows[0][0] - parsedMetricVal, err := parseTableValueToInt64(metricVal, metricDataType) + parsedMetricVal, err := parseTableValueToFloat64(metricVal, metricDataType) if err != nil { return metricsData{}, fmt.Errorf("%s. HTTP code: %d. Body: %s", err.Error(), statusCode, string(body)) } @@ -438,7 +432,7 @@ func (s *azureLogAnalyticsScaler) executeQuery(ctx context.Context, query string if len(queryData.Tables[0].Rows[0]) > 1 { thresholdDataType := queryData.Tables[0].Columns[1].Type thresholdVal := queryData.Tables[0].Rows[0][1] - parsedThresholdVal, err := parseTableValueToInt64(thresholdVal, thresholdDataType) + parsedThresholdVal, err := parseTableValueToFloat64(thresholdVal, thresholdDataType) if err != nil { return metricsData{}, fmt.Errorf("%s. HTTP code: %d. Body: %s", err.Error(), statusCode, string(body)) } @@ -453,7 +447,7 @@ func (s *azureLogAnalyticsScaler) executeQuery(ctx context.Context, query string return metricsData{}, fmt.Errorf("error processing Log Analytics request. Details: unknown error. HTTP code: %d. Body: %s", statusCode, string(body)) } -func parseTableValueToInt64(value interface{}, dataType string) (int64, error) { +func parseTableValueToFloat64(value interface{}, dataType string) (float64, error) { if value != nil { // type can be: real, int, long if dataType == "real" || dataType == "int" || dataType == "long" { @@ -464,7 +458,7 @@ func parseTableValueToInt64(value interface{}, dataType string) (int64, error) { if convertedValue < 0 { return 0, fmt.Errorf("error validating Log Analytics request. Details: value should be >=0, but received %f", value) } - return int64(convertedValue), nil + return convertedValue, nil } return 0, fmt.Errorf("error validating Log Analytics request. Details: value data type should be real, int or long, but received %s", dataType) } diff --git a/pkg/scalers/azure_monitor_scaler.go b/pkg/scalers/azure_monitor_scaler.go index 37f1f3fcc69..52f9013a194 100644 --- a/pkg/scalers/azure_monitor_scaler.go +++ b/pkg/scalers/azure_monitor_scaler.go @@ -24,8 +24,6 @@ import ( az "github.com/Azure/go-autorest/autorest/azure" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -48,7 +46,7 @@ type azureMonitorScaler struct { type azureMonitorMetadata struct { azureMonitorInfo azure.MonitorInfo - targetValue int64 + targetValue float64 scalerIndex int } @@ -79,7 +77,7 @@ func parseAzureMonitorMetadata(config *ScalerConfig) (*azureMonitorMetadata, err } if val, ok := config.TriggerMetadata[targetValueName]; ok && val != "" { - targetValue, err := strconv.ParseInt(val, 10, 64) + targetValue, err := strconv.ParseFloat(val, 64) if err != nil { azureMonitorLog.Error(err, "Error parsing azure monitor metadata", "targetValue", targetValueName) return nil, fmt.Errorf("error parsing azure monitor metadata %s: %s", targetValueName, err.Error()) @@ -221,7 +219,7 @@ func (s *azureMonitorScaler) GetMetricSpecForScaling(context.Context) []v2beta2. Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, kedautil.NormalizeString(fmt.Sprintf("azure-monitor-%s", s.metadata.azureMonitorInfo.Name))), }, - Target: GetMetricTarget(s.metricType, s.metadata.targetValue), + Target: GetMetricTargetMili(s.metricType, s.metadata.targetValue), } metricSpec := v2beta2.MetricSpec{External: externalMetric, Type: externalMetricType} return []v2beta2.MetricSpec{metricSpec} @@ -235,11 +233,7 @@ func (s *azureMonitorScaler) GetMetrics(ctx context.Context, metricName string, return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(val, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, val) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/azure_pipelines_scaler.go b/pkg/scalers/azure_pipelines_scaler.go index fb3ce28eb28..251e7c1f75f 100644 --- a/pkg/scalers/azure_pipelines_scaler.go +++ b/pkg/scalers/azure_pipelines_scaler.go @@ -10,8 +10,6 @@ import ( "strings" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -206,11 +204,7 @@ func (s *azurePipelinesScaler) GetMetrics(ctx context.Context, metricName string return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(queuelen, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(queuelen)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/azure_queue_scaler.go b/pkg/scalers/azure_queue_scaler.go index 4189da95b97..016c3183304 100644 --- a/pkg/scalers/azure_queue_scaler.go +++ b/pkg/scalers/azure_queue_scaler.go @@ -23,8 +23,6 @@ import ( "strconv" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -196,11 +194,7 @@ func (s *azureQueueScaler) GetMetrics(ctx context.Context, metricName string, me return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(queuelen, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(queuelen)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/azure_servicebus_scaler.go b/pkg/scalers/azure_servicebus_scaler.go index 6f434be3bba..77d78bf0bac 100755 --- a/pkg/scalers/azure_servicebus_scaler.go +++ b/pkg/scalers/azure_servicebus_scaler.go @@ -26,8 +26,6 @@ import ( servicebus "github.com/Azure/azure-service-bus-go" az "github.com/Azure/go-autorest/autorest/azure" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -216,11 +214,7 @@ func (s *azureServiceBusScaler) GetMetrics(ctx context.Context, metricName strin return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(queuelen, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(queuelen)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/cassandra_scaler.go b/pkg/scalers/cassandra_scaler.go index 9e2a462d4bb..9e6fbbd926d 100644 --- a/pkg/scalers/cassandra_scaler.go +++ b/pkg/scalers/cassandra_scaler.go @@ -8,8 +8,6 @@ import ( "github.com/gocql/gocql" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -202,11 +200,7 @@ func (s *cassandraScaler) GetMetrics(ctx context.Context, metricName string, met return []external_metrics.ExternalMetricValue{}, fmt.Errorf("error inspecting cassandra: %s", err) } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(num, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(num)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/cron_scaler.go b/pkg/scalers/cron_scaler.go index 0d466485214..e438982c042 100644 --- a/pkg/scalers/cron_scaler.go +++ b/pkg/scalers/cron_scaler.go @@ -9,8 +9,6 @@ import ( "github.com/robfig/cron/v3" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -184,11 +182,7 @@ func (s *cronScaler) GetMetrics(ctx context.Context, metricName string, metricSe } /*******************************************************************************/ - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(currentReplicas, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(currentReplicas)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/datadog_scaler.go b/pkg/scalers/datadog_scaler.go index 37db2262b6a..fc59af0e8b2 100644 --- a/pkg/scalers/datadog_scaler.go +++ b/pkg/scalers/datadog_scaler.go @@ -10,8 +10,6 @@ import ( datadog "github.com/DataDog/datadog-api-client-go/api/v1/datadog" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -29,7 +27,7 @@ type datadogMetadata struct { appKey string datadogSite string query string - queryValue int64 + queryValue float64 vType v2beta2.MetricTargetType metricName string age int @@ -101,7 +99,7 @@ func parseDatadogMetadata(config *ScalerConfig) (*datadogMetadata, error) { } if val, ok := config.TriggerMetadata["queryValue"]; ok { - queryValue, err := strconv.ParseInt(val, 10, 64) + queryValue, err := strconv.ParseFloat(val, 64) if err != nil { return nil, fmt.Errorf("queryValue parsing error %s", err.Error()) } @@ -293,7 +291,7 @@ func (s *datadogScaler) GetMetricSpecForScaling(context.Context) []v2beta2.Metri Metric: v2beta2.MetricIdentifier{ Name: s.metadata.metricName, }, - Target: GetMetricTarget(s.metadata.vType, s.metadata.queryValue), + Target: GetMetricTargetMili(s.metadata.vType, s.metadata.queryValue), } metricSpec := v2beta2.MetricSpec{ External: externalMetric, Type: externalMetricType, @@ -309,11 +307,7 @@ func (s *datadogScaler) GetMetrics(ctx context.Context, metricName string, metri return []external_metrics.ExternalMetricValue{}, fmt.Errorf("error getting metrics from Datadog: %s", err) } - metric := external_metrics.ExternalMetricValue{ - MetricName: s.metadata.metricName, - Value: *resource.NewMilliQuantity(int64(num*1000), resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, num) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/elasticsearch_scaler.go b/pkg/scalers/elasticsearch_scaler.go index c41cd395dd3..d7a2716cb62 100644 --- a/pkg/scalers/elasticsearch_scaler.go +++ b/pkg/scalers/elasticsearch_scaler.go @@ -14,8 +14,6 @@ import ( "github.com/elastic/go-elasticsearch/v7" "github.com/tidwall/gjson" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -176,7 +174,7 @@ func (s *elasticsearchScaler) IsActive(ctx context.Context) (bool, error) { } // getQueryResult returns result of the scaler query -func (s *elasticsearchScaler) getQueryResult(ctx context.Context) (int64, error) { +func (s *elasticsearchScaler) getQueryResult(ctx context.Context) (float64, error) { // Build the request body. var body bytes.Buffer if err := json.NewEncoder(&body).Encode(buildQuery(s.metadata)); err != nil { @@ -223,11 +221,11 @@ func buildQuery(metadata *elasticsearchMetadata) map[string]interface{} { return query } -func getValueFromSearch(body []byte, valueLocation string) (int64, error) { +func getValueFromSearch(body []byte, valueLocation string) (float64, error) { r := gjson.GetBytes(body, valueLocation) errorMsg := "valueLocation must point to value of type number but got: '%s'" if r.Type == gjson.String { - q, err := strconv.ParseInt(r.String(), 10, 64) + q, err := strconv.ParseFloat(r.String(), 64) if err != nil { return 0, fmt.Errorf(errorMsg, r.String()) } @@ -236,7 +234,7 @@ func getValueFromSearch(body []byte, valueLocation string) (int64, error) { if r.Type != gjson.Number { return 0, fmt.Errorf(errorMsg, r.Type.String()) } - return int64(r.Num), nil + return r.Num, nil } // GetMetricSpecForScaling returns the MetricSpec for the Horizontal Pod Autoscaler @@ -260,11 +258,7 @@ func (s *elasticsearchScaler) GetMetrics(ctx context.Context, metricName string, return []external_metrics.ExternalMetricValue{}, fmt.Errorf("error inspecting elasticsearch: %s", err) } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(num, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, num) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/external_scaler.go b/pkg/scalers/external_scaler.go index aa929704f4b..3f6228a7a17 100644 --- a/pkg/scalers/external_scaler.go +++ b/pkg/scalers/external_scaler.go @@ -12,8 +12,6 @@ import ( "google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials/insecure" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -215,12 +213,7 @@ func (s *externalScaler) GetMetrics(ctx context.Context, metricName string, metr } for _, metricResult := range response.MetricValues { - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(metricResult.MetricValue, resource.DecimalSI), - Timestamp: metav1.Now(), - } - + metric := GenerateMetricInMili(metricName, float64(metricResult.MetricValue)) metrics = append(metrics, metric) } diff --git a/pkg/scalers/gcp_pubsub_scaler.go b/pkg/scalers/gcp_pubsub_scaler.go index 6f933e5ddc1..5634d47a418 100644 --- a/pkg/scalers/gcp_pubsub_scaler.go +++ b/pkg/scalers/gcp_pubsub_scaler.go @@ -9,8 +9,6 @@ import ( "strings" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -198,11 +196,7 @@ func (s *pubsubScaler) GetMetrics(ctx context.Context, metricName string, metric } } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(value, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(value)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/gcp_stackdriver_scaler.go b/pkg/scalers/gcp_stackdriver_scaler.go index 1d11588903b..89ac2863d60 100644 --- a/pkg/scalers/gcp_stackdriver_scaler.go +++ b/pkg/scalers/gcp_stackdriver_scaler.go @@ -6,8 +6,6 @@ import ( "strconv" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -169,11 +167,7 @@ func (s *stackdriverScaler) GetMetrics(ctx context.Context, metricName string, m return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(value, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(value)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/gcp_storage_scaler.go b/pkg/scalers/gcp_storage_scaler.go index 411cff2565f..60fef0c6827 100644 --- a/pkg/scalers/gcp_storage_scaler.go +++ b/pkg/scalers/gcp_storage_scaler.go @@ -10,8 +10,6 @@ import ( "google.golang.org/api/iterator" option "google.golang.org/api/option" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -174,11 +172,7 @@ func (s *gcsScaler) GetMetrics(ctx context.Context, metricName string, metricSel return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(items, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(items)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/graphite_scaler.go b/pkg/scalers/graphite_scaler.go index 0f26139a8cc..3b9692297b1 100644 --- a/pkg/scalers/graphite_scaler.go +++ b/pkg/scalers/graphite_scaler.go @@ -10,8 +10,6 @@ import ( "strconv" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -37,7 +35,7 @@ type graphiteMetadata struct { serverAddress string metricName string query string - threshold int64 + threshold float64 from string // basic auth @@ -104,7 +102,7 @@ func parseGraphiteMetadata(config *ScalerConfig) (*graphiteMetadata, error) { } if val, ok := config.TriggerMetadata[grapThreshold]; ok && val != "" { - t, err := strconv.ParseInt(val, 10, 64) + t, err := strconv.ParseFloat(val, 64) if err != nil { return nil, fmt.Errorf("error parsing %s: %s", grapThreshold, err) } @@ -155,7 +153,7 @@ func (s *graphiteScaler) GetMetricSpecForScaling(context.Context) []v2beta2.Metr Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, kedautil.NormalizeString(fmt.Sprintf("graphite-%s", s.metadata.metricName))), }, - Target: GetMetricTarget(s.metricType, s.metadata.threshold), + Target: GetMetricTargetMili(s.metricType, s.metadata.threshold), } metricSpec := v2beta2.MetricSpec{ External: externalMetric, Type: externalMetricType, @@ -218,11 +216,7 @@ func (s *graphiteScaler) GetMetrics(ctx context.Context, metricName string, metr return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(int64(val), resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, val) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/huawei_cloudeye_scaler.go b/pkg/scalers/huawei_cloudeye_scaler.go index 2abd5b3898c..a0dc1879e3d 100644 --- a/pkg/scalers/huawei_cloudeye_scaler.go +++ b/pkg/scalers/huawei_cloudeye_scaler.go @@ -11,8 +11,6 @@ import ( "github.com/Huawei/gophercloud/openstack" "github.com/Huawei/gophercloud/openstack/ces/v1/metricdata" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -239,12 +237,7 @@ func (h *huaweiCloudeyeScaler) GetMetrics(ctx context.Context, metricName string return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(int64(metricValue), resource.DecimalSI), - Timestamp: metav1.Now(), - } - + metric := GenerateMetricInMili(metricName, metricValue) return append([]external_metrics.ExternalMetricValue{}, metric), nil } @@ -253,7 +246,7 @@ func (h *huaweiCloudeyeScaler) GetMetricSpecForScaling(context.Context) []v2beta Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(h.metadata.scalerIndex, kedautil.NormalizeString(fmt.Sprintf("huawei-cloudeye-%s", h.metadata.metricsName))), }, - Target: GetMetricTarget(h.metricType, int64(h.metadata.targetMetricValue)), + Target: GetMetricTargetMili(h.metricType, h.metadata.targetMetricValue), } metricSpec := v2beta2.MetricSpec{External: externalMetric, Type: externalMetricType} return []v2beta2.MetricSpec{metricSpec} diff --git a/pkg/scalers/ibmmq_scaler.go b/pkg/scalers/ibmmq_scaler.go index e0d9f8f7098..219086f9c54 100644 --- a/pkg/scalers/ibmmq_scaler.go +++ b/pkg/scalers/ibmmq_scaler.go @@ -13,8 +13,6 @@ import ( "time" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" @@ -23,9 +21,8 @@ import ( // Default variables and settings const ( - ibmMqQueueDepthMetricName = "currentQueueDepth" - defaultTargetQueueDepth = 20 - defaultTLSDisabled = false + defaultTargetQueueDepth = 20 + defaultTLSDisabled = false ) // IBMMQScaler assigns struct data pointer to metadata variable @@ -226,11 +223,7 @@ func (s *IBMMQScaler) GetMetrics(ctx context.Context, metricName string, metricS return []external_metrics.ExternalMetricValue{}, fmt.Errorf("error inspecting IBM MQ queue depth: %s", err) } - metric := external_metrics.ExternalMetricValue{ - MetricName: ibmMqQueueDepthMetricName, - Value: *resource.NewQuantity(queueDepth, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(queueDepth)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/influxdb_scaler.go b/pkg/scalers/influxdb_scaler.go index 80f2edbb952..79f6b895e7e 100644 --- a/pkg/scalers/influxdb_scaler.go +++ b/pkg/scalers/influxdb_scaler.go @@ -9,8 +9,6 @@ import ( influxdb2 "github.com/influxdata/influxdb-client-go/v2" api "github.com/influxdata/influxdb-client-go/v2/api" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -206,11 +204,7 @@ func (s *influxDBScaler) GetMetrics(ctx context.Context, metricName string, metr return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(int64(value), resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, value) return append([]external_metrics.ExternalMetricValue{}, metric), nil } @@ -221,7 +215,7 @@ func (s *influxDBScaler) GetMetricSpecForScaling(context.Context) []v2beta2.Metr Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, s.metadata.metricName), }, - Target: GetMetricTarget(s.metricType, int64(s.metadata.thresholdValue)), + Target: GetMetricTargetMili(s.metricType, s.metadata.thresholdValue), } metricSpec := v2beta2.MetricSpec{ External: externalMetric, Type: externalMetricType, diff --git a/pkg/scalers/kafka_scaler.go b/pkg/scalers/kafka_scaler.go index 170d7eed425..961a3827d77 100644 --- a/pkg/scalers/kafka_scaler.go +++ b/pkg/scalers/kafka_scaler.go @@ -10,8 +10,6 @@ import ( "github.com/Shopify/sarama" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -487,11 +485,7 @@ func (s *kafkaScaler) GetMetrics(ctx context.Context, metricName string, metricS } } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(totalLag, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(totalLag)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/kubernetes_workload_scaler.go b/pkg/scalers/kubernetes_workload_scaler.go index b2dc90882db..d178566c1db 100644 --- a/pkg/scalers/kubernetes_workload_scaler.go +++ b/pkg/scalers/kubernetes_workload_scaler.go @@ -7,8 +7,6 @@ import ( "k8s.io/api/autoscaling/v2beta2" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" "sigs.k8s.io/controller-runtime/pkg/client" @@ -36,7 +34,7 @@ var phasesCountedAsTerminated = []corev1.PodPhase{ type kubernetesWorkloadMetadata struct { podSelector labels.Selector namespace string - value int64 + value float64 scalerIndex int } @@ -67,7 +65,7 @@ func parseWorkloadMetadata(config *ScalerConfig) (*kubernetesWorkloadMetadata, e if err != nil || meta.podSelector.String() == "" { return nil, fmt.Errorf("invalid pod selector") } - meta.value, err = strconv.ParseInt(config.TriggerMetadata[valueKey], 10, 64) + meta.value, err = strconv.ParseFloat(config.TriggerMetadata[valueKey], 64) if err != nil || meta.value == 0 { return nil, fmt.Errorf("value must be an integer greater than 0") } @@ -97,7 +95,7 @@ func (s *kubernetesWorkloadScaler) GetMetricSpecForScaling(context.Context) []v2 Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, kedautil.NormalizeString(fmt.Sprintf("workload-%s", s.metadata.namespace))), }, - Target: GetMetricTarget(s.metricType, s.metadata.value), + Target: GetMetricTargetMili(s.metricType, s.metadata.value), } metricSpec := v2beta2.MetricSpec{External: externalMetric, Type: kubernetesWorkloadMetricType} return []v2beta2.MetricSpec{metricSpec} @@ -110,11 +108,7 @@ func (s *kubernetesWorkloadScaler) GetMetrics(ctx context.Context, metricName st return []external_metrics.ExternalMetricValue{}, fmt.Errorf("error inspecting kubernetes workload: %s", err) } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(pods, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(pods)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/liiklus_scaler.go b/pkg/scalers/liiklus_scaler.go index 9575c956353..e53431f51ac 100644 --- a/pkg/scalers/liiklus_scaler.go +++ b/pkg/scalers/liiklus_scaler.go @@ -10,8 +10,6 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" @@ -82,11 +80,7 @@ func (s *liiklusScaler) GetMetrics(ctx context.Context, metricName string, metri } return []external_metrics.ExternalMetricValue{ - { - MetricName: metricName, - Timestamp: meta_v1.Now(), - Value: *resource.NewQuantity(int64(totalLag), resource.DecimalSI), - }, + GenerateMetricInMili(metricName, float64(totalLag)), }, nil } diff --git a/pkg/scalers/metrics_api_scaler.go b/pkg/scalers/metrics_api_scaler.go index 17436aa5ff2..0f378361d8d 100644 --- a/pkg/scalers/metrics_api_scaler.go +++ b/pkg/scalers/metrics_api_scaler.go @@ -13,7 +13,6 @@ import ( "github.com/tidwall/gjson" "k8s.io/api/autoscaling/v2beta2" "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -29,7 +28,7 @@ type metricsAPIScaler struct { } type metricsAPIScalerMetadata struct { - targetValue int + targetValue float64 url string valueLocation string @@ -100,7 +99,7 @@ func parseMetricsAPIMetadata(config *ScalerConfig) (*metricsAPIScalerMetadata, e meta.scalerIndex = config.ScalerIndex if val, ok := config.TriggerMetadata["targetValue"]; ok { - targetValue, err := strconv.Atoi(val) + targetValue, err := strconv.ParseFloat(val, 64) if err != nil { return nil, fmt.Errorf("targetValue parsing error %s", err.Error()) } @@ -190,46 +189,46 @@ func parseMetricsAPIMetadata(config *ScalerConfig) (*metricsAPIScalerMetadata, e } // GetValueFromResponse uses provided valueLocation to access the numeric value in provided body -func GetValueFromResponse(body []byte, valueLocation string) (*resource.Quantity, error) { +func GetValueFromResponse(body []byte, valueLocation string) (float64, error) { r := gjson.GetBytes(body, valueLocation) errorMsg := "valueLocation must point to value of type number or a string representing a Quantity got: '%s'" if r.Type == gjson.String { - q, err := resource.ParseQuantity(r.String()) + v, err := resource.ParseQuantity(r.String()) if err != nil { - return nil, fmt.Errorf(errorMsg, r.String()) + return 0, fmt.Errorf(errorMsg, r.String()) } - return &q, nil + return v.AsApproximateFloat64(), nil } if r.Type != gjson.Number { - return nil, fmt.Errorf(errorMsg, r.Type.String()) + return 0, fmt.Errorf(errorMsg, r.Type.String()) } - return resource.NewQuantity(int64(r.Num), resource.DecimalSI), nil + return r.Num, nil } -func (s *metricsAPIScaler) getMetricValue(ctx context.Context) (*resource.Quantity, error) { +func (s *metricsAPIScaler) getMetricValue(ctx context.Context) (float64, error) { request, err := getMetricAPIServerRequest(ctx, s.metadata) if err != nil { - return nil, err + return 0, err } r, err := s.client.Do(request) if err != nil { - return nil, err + return 0, err } defer r.Body.Close() if r.StatusCode != http.StatusOK { msg := fmt.Sprintf("%s: api returned %d", r.Request.URL.Path, r.StatusCode) - return nil, errors.New(msg) + return 0, errors.New(msg) } b, err := ioutil.ReadAll(r.Body) if err != nil { - return nil, err + return 0, err } v, err := GetValueFromResponse(b, s.metadata.valueLocation) if err != nil { - return nil, err + return 0, err } return v, nil } @@ -247,7 +246,7 @@ func (s *metricsAPIScaler) IsActive(ctx context.Context) (bool, error) { return false, err } - return v.AsApproximateFloat64() > 0.0, nil + return v > 0.0, nil } // GetMetricSpecForScaling returns the MetricSpec for the Horizontal Pod Autoscaler @@ -256,7 +255,7 @@ func (s *metricsAPIScaler) GetMetricSpecForScaling(context.Context) []v2beta2.Me Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, kedautil.NormalizeString(fmt.Sprintf("metric-api-%s", s.metadata.valueLocation))), }, - Target: GetMetricTarget(s.metricType, int64(s.metadata.targetValue)), + Target: GetMetricTargetMili(s.metricType, s.metadata.targetValue), } metricSpec := v2beta2.MetricSpec{ External: externalMetric, Type: externalMetricType, @@ -266,16 +265,12 @@ func (s *metricsAPIScaler) GetMetricSpecForScaling(context.Context) []v2beta2.Me // GetMetrics returns value for a supported metric and an error if there is a problem getting the metric func (s *metricsAPIScaler) GetMetrics(ctx context.Context, metricName string, metricSelector labels.Selector) ([]external_metrics.ExternalMetricValue, error) { - v, err := s.getMetricValue(ctx) + val, err := s.getMetricValue(ctx) if err != nil { return []external_metrics.ExternalMetricValue{}, fmt.Errorf("error requesting metrics endpoint: %s", err) } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *v, - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, val) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/metrics_api_scaler_test.go b/pkg/scalers/metrics_api_scaler_test.go index 99e59514492..5ad553a506f 100644 --- a/pkg/scalers/metrics_api_scaler_test.go +++ b/pkg/scalers/metrics_api_scaler_test.go @@ -118,32 +118,32 @@ func TestGetValueFromResponse(t *testing.T) { if err != nil { t.Error("Expected success but got error", err) } - if v.CmpInt64(32) != 0 { - t.Errorf("Expected %d got %d", 32, v.AsDec()) + if v != 32 { + t.Errorf("Expected %d got %f", 32, v) } v, err = GetValueFromResponse(d, "count") if err != nil { t.Error("Expected success but got error", err) } - if v.CmpInt64(2) != 0 { - t.Errorf("Expected %d got %d", 2, v.AsDec()) + if v != 2.43 { + t.Errorf("Expected %d got %f", 2, v) } v, err = GetValueFromResponse(d, "components.0.str") if err != nil { t.Error("Expected success but got error", err) } - if v.CmpInt64(64) != 0 { - t.Errorf("Expected %d got %d", 64, v.AsDec()) + if v != 64 { + t.Errorf("Expected %d got %f", 64, v) } v, err = GetValueFromResponse(d, "components.0.k") if err != nil { t.Error("Expected success but got error", err) } - if v.CmpInt64(1000) != 0 { - t.Errorf("Expected %d got %d", 1000, v.AsDec()) + if v != 1000 { + t.Errorf("Expected %d got %f", 1000, v) } _, err = GetValueFromResponse(d, "components.0.wrong") diff --git a/pkg/scalers/mongo_scaler.go b/pkg/scalers/mongo_scaler.go index 94973debb19..fa4121f6696 100644 --- a/pkg/scalers/mongo_scaler.go +++ b/pkg/scalers/mongo_scaler.go @@ -13,8 +13,6 @@ import ( "go.mongodb.org/mongo-driver/mongo/readpref" "go.mongodb.org/mongo-driver/x/bsonx" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -241,11 +239,7 @@ func (s *mongoDBScaler) GetMetrics(ctx context.Context, metricName string, metri return []external_metrics.ExternalMetricValue{}, fmt.Errorf("failed to inspect momgoDB, because of %v", err) } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(num, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(num)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/mssql_scaler.go b/pkg/scalers/mssql_scaler.go index ae584c039fe..a24fe1040e9 100644 --- a/pkg/scalers/mssql_scaler.go +++ b/pkg/scalers/mssql_scaler.go @@ -10,8 +10,6 @@ import ( // mssql driver required for this scaler _ "github.com/denisenkom/go-mssqldb" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -52,7 +50,7 @@ type mssqlMetadata struct { query string // The threshold that is used as targetAverageValue in the Horizontal Pod Autoscaler. // +required - targetValue int64 + targetValue float64 // The name of the metric to use in the Horizontal Pod Autoscaler. This value will be prefixed with "mssql-". // +optional metricName string @@ -100,7 +98,7 @@ func parseMSSQLMetadata(config *ScalerConfig) (*mssqlMetadata, error) { // Target query value if val, ok := config.TriggerMetadata["targetValue"]; ok { - targetValue, err := strconv.ParseInt(val, 10, 64) + targetValue, err := strconv.ParseFloat(val, 64) if err != nil { return nil, fmt.Errorf("targetValue parsing error %s", err.Error()) } @@ -221,7 +219,7 @@ func (s *mssqlScaler) GetMetricSpecForScaling(context.Context) []v2beta2.MetricS Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, s.metadata.metricName), }, - Target: GetMetricTarget(s.metricType, s.metadata.targetValue), + Target: GetMetricTargetMili(s.metricType, s.metadata.targetValue), } metricSpec := v2beta2.MetricSpec{ @@ -238,18 +236,14 @@ func (s *mssqlScaler) GetMetrics(ctx context.Context, metricName string, metricS return []external_metrics.ExternalMetricValue{}, fmt.Errorf("error inspecting mssql: %s", err) } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(num, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, num) return append([]external_metrics.ExternalMetricValue{}, metric), nil } // getQueryResult returns the result of the scaler query -func (s *mssqlScaler) getQueryResult(ctx context.Context) (int64, error) { - var value int64 +func (s *mssqlScaler) getQueryResult(ctx context.Context) (float64, error) { + var value float64 err := s.connection.QueryRowContext(ctx, s.metadata.query).Scan(&value) switch { case err == sql.ErrNoRows: diff --git a/pkg/scalers/mssql_scaler_test.go b/pkg/scalers/mssql_scaler_test.go index 0842968d6e5..1f02642a0cb 100644 --- a/pkg/scalers/mssql_scaler_test.go +++ b/pkg/scalers/mssql_scaler_test.go @@ -127,9 +127,9 @@ func TestMSSQLMetadataParsing(t *testing.T) { t.Errorf("Wrong query. Expected '%s' but got '%s'", expectedQuery, outputMetadata.query) } - var expectedTargetValue int64 = 1 + var expectedTargetValue float64 = 1 if outputMetadata.targetValue != expectedTargetValue { - t.Errorf("Wrong targetValue. Expected %d but got %d", expectedTargetValue, outputMetadata.targetValue) + t.Errorf("Wrong targetValue. Expected %f but got %f", expectedTargetValue, outputMetadata.targetValue) } outputConnectionString := getMSSQLConnectionString(outputMetadata) diff --git a/pkg/scalers/mysql_scaler.go b/pkg/scalers/mysql_scaler.go index 28767aa1990..70e02aacf57 100644 --- a/pkg/scalers/mysql_scaler.go +++ b/pkg/scalers/mysql_scaler.go @@ -9,8 +9,6 @@ import ( "github.com/go-sql-driver/mysql" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -32,7 +30,7 @@ type mySQLMetadata struct { port string dbName string query string - queryValue int64 + queryValue float64 metricName string } @@ -71,7 +69,7 @@ func parseMySQLMetadata(config *ScalerConfig) (*mySQLMetadata, error) { } if val, ok := config.TriggerMetadata["queryValue"]; ok { - queryValue, err := strconv.ParseInt(val, 10, 64) + queryValue, err := strconv.ParseFloat(val, 64) if err != nil { return nil, fmt.Errorf("queryValue parsing error %s", err.Error()) } @@ -194,8 +192,8 @@ func (s *mySQLScaler) IsActive(ctx context.Context) (bool, error) { } // getQueryResult returns result of the scaler query -func (s *mySQLScaler) getQueryResult(ctx context.Context) (int64, error) { - var value int64 +func (s *mySQLScaler) getQueryResult(ctx context.Context) (float64, error) { + var value float64 err := s.connection.QueryRowContext(ctx, s.metadata.query).Scan(&value) if err != nil { mySQLLog.Error(err, fmt.Sprintf("Could not query MySQL database: %s", err)) @@ -210,7 +208,7 @@ func (s *mySQLScaler) GetMetricSpecForScaling(context.Context) []v2beta2.MetricS Metric: v2beta2.MetricIdentifier{ Name: s.metadata.metricName, }, - Target: GetMetricTarget(s.metricType, s.metadata.queryValue), + Target: GetMetricTargetMili(s.metricType, s.metadata.queryValue), } metricSpec := v2beta2.MetricSpec{ External: externalMetric, Type: externalMetricType, @@ -225,11 +223,7 @@ func (s *mySQLScaler) GetMetrics(ctx context.Context, metricName string, metricS return []external_metrics.ExternalMetricValue{}, fmt.Errorf("error inspecting MySQL: %s", err) } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(num, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, num) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/newrelic_scaler.go b/pkg/scalers/newrelic_scaler.go index 40360238a0b..e5254250d7e 100644 --- a/pkg/scalers/newrelic_scaler.go +++ b/pkg/scalers/newrelic_scaler.go @@ -9,8 +9,6 @@ import ( "github.com/newrelic/newrelic-client-go/newrelic" "github.com/newrelic/newrelic-client-go/pkg/nrdb" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -40,7 +38,7 @@ type newrelicMetadata struct { queryKey string noDataError bool nrql string - threshold int64 + threshold float64 scalerIndex int } @@ -108,7 +106,7 @@ func parseNewRelicMetadata(config *ScalerConfig) (*newrelicMetadata, error) { } if val, ok := config.TriggerMetadata[threshold]; ok && val != "" { - t, err := strconv.ParseInt(val, 10, 64) + t, err := strconv.ParseFloat(val, 64) if err != nil { return nil, fmt.Errorf("error parsing %s", threshold) } @@ -171,11 +169,7 @@ func (s *newrelicScaler) GetMetrics(ctx context.Context, metricName string, metr return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(int64(val), resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, val) return append([]external_metrics.ExternalMetricValue{}, metric), nil } @@ -187,7 +181,7 @@ func (s *newrelicScaler) GetMetricSpecForScaling(context.Context) []v2beta2.Metr Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, metricName), }, - Target: GetMetricTarget(s.metricType, s.metadata.threshold), + Target: GetMetricTargetMili(s.metricType, s.metadata.threshold), } metricSpec := v2beta2.MetricSpec{ External: externalMetric, Type: externalMetricType, diff --git a/pkg/scalers/openstack_metrics_scaler.go b/pkg/scalers/openstack_metrics_scaler.go index 02a6f47e040..8efbcb5fb47 100644 --- a/pkg/scalers/openstack_metrics_scaler.go +++ b/pkg/scalers/openstack_metrics_scaler.go @@ -12,8 +12,6 @@ import ( "time" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -210,7 +208,7 @@ func (a *openstackMetricScaler) GetMetricSpecForScaling(context.Context) []v2bet Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(a.metadata.scalerIndex, metricName), }, - Target: GetMetricTarget(a.metricType, int64(a.metadata.threshold)), + Target: GetMetricTargetMili(a.metricType, a.metadata.threshold), } metricSpec := v2beta2.MetricSpec{ @@ -229,11 +227,7 @@ func (a *openstackMetricScaler) GetMetrics(ctx context.Context, metricName strin return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(int64(val), resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, val) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/openstack_swift_scaler.go b/pkg/scalers/openstack_swift_scaler.go index b0eaa7868f9..9a61b99d6cf 100644 --- a/pkg/scalers/openstack_swift_scaler.go +++ b/pkg/scalers/openstack_swift_scaler.go @@ -11,8 +11,6 @@ import ( "strings" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -379,11 +377,7 @@ func (s *openstackSwiftScaler) GetMetrics(ctx context.Context, metricName string return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(objectCount, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(objectCount)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/postgresql_scaler.go b/pkg/scalers/postgresql_scaler.go index e87ecda41e6..f6017599b6e 100644 --- a/pkg/scalers/postgresql_scaler.go +++ b/pkg/scalers/postgresql_scaler.go @@ -9,8 +9,6 @@ import ( // PostreSQL drive required for this scaler _ "github.com/lib/pq" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -25,7 +23,7 @@ type postgreSQLScaler struct { } type postgreSQLMetadata struct { - targetQueryValue int64 + targetQueryValue float64 connection string query string metricName string @@ -67,7 +65,7 @@ func parsePostgreSQLMetadata(config *ScalerConfig) (*postgreSQLMetadata, error) } if val, ok := config.TriggerMetadata["targetQueryValue"]; ok { - targetQueryValue, err := strconv.ParseInt(val, 10, 64) + targetQueryValue, err := strconv.ParseFloat(val, 64) if err != nil { return nil, fmt.Errorf("queryValue parsing error %s", err.Error()) } @@ -168,8 +166,8 @@ func (s *postgreSQLScaler) IsActive(ctx context.Context) (bool, error) { return messages > 0, nil } -func (s *postgreSQLScaler) getActiveNumber(ctx context.Context) (int64, error) { - var id int64 +func (s *postgreSQLScaler) getActiveNumber(ctx context.Context) (float64, error) { + var id float64 err := s.connection.QueryRowContext(ctx, s.metadata.query).Scan(&id) if err != nil { postgreSQLLog.Error(err, fmt.Sprintf("could not query postgreSQL: %s", err)) @@ -184,7 +182,7 @@ func (s *postgreSQLScaler) GetMetricSpecForScaling(context.Context) []v2beta2.Me Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, s.metadata.metricName), }, - Target: GetMetricTarget(s.metricType, s.metadata.targetQueryValue), + Target: GetMetricTargetMili(s.metricType, s.metadata.targetQueryValue), } metricSpec := v2beta2.MetricSpec{ External: externalMetric, Type: externalMetricType, @@ -199,11 +197,7 @@ func (s *postgreSQLScaler) GetMetrics(ctx context.Context, metricName string, me return []external_metrics.ExternalMetricValue{}, fmt.Errorf("error inspecting postgreSQL: %s", err) } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(num, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, num) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/predictkube_scaler.go b/pkg/scalers/predictkube_scaler.go index 3875315a464..f51943b901b 100644 --- a/pkg/scalers/predictkube_scaler.go +++ b/pkg/scalers/predictkube_scaler.go @@ -26,8 +26,6 @@ import ( health "google.golang.org/grpc/health/grpc_health_v1" "google.golang.org/grpc/status" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -93,7 +91,7 @@ type predictKubeMetadata struct { prometheusAddress string prometheusAuth *authentication.AuthMeta query string - threshold int64 + threshold float64 scalerIndex int } @@ -208,7 +206,7 @@ func (s *PredictKubeScaler) GetMetricSpecForScaling(context.Context) []v2beta2.M Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, metricName), }, - Target: GetMetricTarget(s.metricType, s.metadata.threshold), + Target: GetMetricTargetMili(s.metricType, s.metadata.threshold), } metricSpec := v2beta2.MetricSpec{ @@ -230,20 +228,14 @@ func (s *PredictKubeScaler) GetMetrics(ctx context.Context, metricName string, _ return nil, err } - predictKubeLog.V(1).Info(fmt.Sprintf("predict value is: %d", value)) + predictKubeLog.V(1).Info(fmt.Sprintf("predict value is: %f", value)) - val := *resource.NewQuantity(value, resource.DecimalSI) - - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: val, - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, value) return append([]external_metrics.ExternalMetricValue{}, metric), nil } -func (s *PredictKubeScaler) doPredictRequest(ctx context.Context) (int64, error) { +func (s *PredictKubeScaler) doPredictRequest(ctx context.Context) (float64, error) { results, err := s.doQuery(ctx) if err != nil { return 0, err @@ -258,14 +250,14 @@ func (s *PredictKubeScaler) doPredictRequest(ctx context.Context) (int64, error) return 0, err } - var y int64 + var y float64 if len(results) > 0 { - y = int64(results[len(results)-1].Value) + y = results[len(results)-1].Value } - x := resp.GetResultMetric() + x := float64(resp.GetResultMetric()) - return func(x, y int64) int64 { + return func(x, y float64) float64 { if x < y { return y } @@ -426,7 +418,7 @@ func parsePredictKubeMetadata(config *ScalerConfig) (result *predictKubeMetadata } if val, ok := config.TriggerMetadata["threshold"]; ok { - meta.threshold, err = strconv.ParseInt(val, 10, 64) + meta.threshold, err = strconv.ParseFloat(val, 64) if err != nil { return nil, fmt.Errorf("threshold parsing error %s", err.Error()) } diff --git a/pkg/scalers/predictkube_scaler_test.go b/pkg/scalers/predictkube_scaler_test.go index d33a79304af..7a21f29bee5 100644 --- a/pkg/scalers/predictkube_scaler_test.go +++ b/pkg/scalers/predictkube_scaler_test.go @@ -214,7 +214,7 @@ func TestPredictKubeGetMetrics(t *testing.T) { result, err := mockPredictKubeScaler.GetMetrics(context.Background(), predictKubeMetricPrefix, nil) assert.NoError(t, err) assert.Equal(t, len(result), 1) - assert.Equal(t, result[0].Value, *resource.NewQuantity(mockPredictServer.val, resource.DecimalSI)) + assert.Equal(t, result[0].Value, *resource.NewMilliQuantity(mockPredictServer.val*1000, resource.DecimalSI)) t.Logf("get: %v, want: %v, predictMetric: %d", result[0].Value, *resource.NewQuantity(mockPredictServer.val, resource.DecimalSI), mockPredictServer.val) } diff --git a/pkg/scalers/prometheus_scaler.go b/pkg/scalers/prometheus_scaler.go index 5236901d374..7a03ad1b1d3 100644 --- a/pkg/scalers/prometheus_scaler.go +++ b/pkg/scalers/prometheus_scaler.go @@ -11,8 +11,6 @@ import ( "time" "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -46,7 +44,7 @@ type prometheusMetadata struct { serverAddress string metricName string query string - threshold int64 + threshold float64 prometheusAuth *authentication.AuthMeta namespace string scalerIndex int @@ -126,7 +124,7 @@ func parsePrometheusMetadata(config *ScalerConfig) (meta *prometheusMetadata, er } if val, ok := config.TriggerMetadata[promThreshold]; ok && val != "" { - t, err := strconv.ParseInt(val, 10, 64) + t, err := strconv.ParseFloat(val, 64) if err != nil { return nil, fmt.Errorf("error parsing %s: %s", promThreshold, err) } @@ -185,7 +183,7 @@ func (s *prometheusScaler) GetMetricSpecForScaling(context.Context) []v2beta2.Me Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, metricName), }, - Target: GetMetricTarget(s.metricType, s.metadata.threshold), + Target: GetMetricTargetMili(s.metricType, s.metadata.threshold), } metricSpec := v2beta2.MetricSpec{ External: externalMetric, Type: externalMetricType, @@ -281,11 +279,7 @@ func (s *prometheusScaler) GetMetrics(ctx context.Context, metricName string, _ return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(int64(val), resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, val) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/rabbitmq_scaler.go b/pkg/scalers/rabbitmq_scaler.go index 8ac59c57957..e4d1ce04de8 100644 --- a/pkg/scalers/rabbitmq_scaler.go +++ b/pkg/scalers/rabbitmq_scaler.go @@ -13,8 +13,6 @@ import ( "github.com/streadway/amqp" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -64,7 +62,7 @@ type rabbitMQScaler struct { type rabbitMQMetadata struct { queueName string mode string // QueueLength or MessageRate - value int64 // trigger value (queue length or publish/sec. rate) + value float64 // trigger value (queue length or publish/sec. rate) host string // connection string for either HTTP or AMQP protocol protocol string // either http or amqp protocol vhostName *string // override the vhost from the connection info @@ -281,7 +279,7 @@ func parseTrigger(meta *rabbitMQMetadata, config *ScalerConfig) (*rabbitMQMetada // Parse deprecated `queueLength` value if deprecatedQueueLengthPresent { - queueLength, err := strconv.ParseInt(deprecatedQueueLengthValue, 10, 64) + queueLength, err := strconv.ParseFloat(deprecatedQueueLengthValue, 64) if err != nil { return nil, fmt.Errorf("can't parse %s: %s", rabbitQueueLengthMetricName, err) } @@ -307,7 +305,7 @@ func parseTrigger(meta *rabbitMQMetadata, config *ScalerConfig) (*rabbitMQMetada default: return nil, fmt.Errorf("trigger mode %s must be one of %s, %s", mode, rabbitModeQueueLength, rabbitModeMessageRate) } - triggerValue, err := strconv.ParseInt(value, 10, 64) + triggerValue, err := strconv.ParseFloat(value, 64) if err != nil { return nil, fmt.Errorf("can't parse %s: %s", rabbitValueTriggerConfigName, err) } @@ -465,7 +463,7 @@ func (s *rabbitMQScaler) GetMetricSpecForScaling(context.Context) []v2beta2.Metr Metric: v2beta2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, s.metadata.metricName), }, - Target: GetMetricTarget(s.metricType, s.metadata.value), + Target: GetMetricTargetMili(s.metricType, s.metadata.value), } metricSpec := v2beta2.MetricSpec{ External: externalMetric, Type: rabbitMetricType, @@ -481,17 +479,11 @@ func (s *rabbitMQScaler) GetMetrics(ctx context.Context, metricName string, metr return []external_metrics.ExternalMetricValue{}, s.anonimizeRabbitMQError(err) } - var metricValue resource.Quantity + var metric external_metrics.ExternalMetricValue if s.metadata.mode == rabbitModeQueueLength { - metricValue = *resource.NewQuantity(messages, resource.DecimalSI) + metric = GenerateMetricInMili(metricName, float64(messages)) } else { - metricValue = *resource.NewMilliQuantity(int64(publishRate*1000), resource.DecimalSI) - } - - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: metricValue, - Timestamp: metav1.Now(), + metric = GenerateMetricInMili(metricName, publishRate) } return append([]external_metrics.ExternalMetricValue{}, metric), nil diff --git a/pkg/scalers/redis_scaler.go b/pkg/scalers/redis_scaler.go index c74c141eca7..8eafe2338f5 100644 --- a/pkg/scalers/redis_scaler.go +++ b/pkg/scalers/redis_scaler.go @@ -9,8 +9,6 @@ import ( "github.com/go-redis/redis/v8" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -248,11 +246,7 @@ func (s *redisScaler) GetMetrics(ctx context.Context, metricName string, metricS return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(listLen, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(listLen)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/redis_streams_scaler.go b/pkg/scalers/redis_streams_scaler.go index 52c39f5e7f1..6793d9876ad 100644 --- a/pkg/scalers/redis_streams_scaler.go +++ b/pkg/scalers/redis_streams_scaler.go @@ -6,8 +6,6 @@ import ( "strconv" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -246,10 +244,6 @@ func (s *redisStreamsScaler) GetMetrics(ctx context.Context, metricName string, return []external_metrics.ExternalMetricValue{}, err } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(pendingEntriesCount, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(pendingEntriesCount)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/scaler.go b/pkg/scalers/scaler.go index 95f24438cc8..a6413e86332 100644 --- a/pkg/scalers/scaler.go +++ b/pkg/scalers/scaler.go @@ -25,6 +25,7 @@ import ( metrics "github.com/rcrowley/go-metrics" "k8s.io/api/autoscaling/v2beta2" "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" @@ -155,3 +156,31 @@ func GetMetricTarget(metricType v2beta2.MetricTargetType, metricValue int64) v2b return target } + +// GetMetricTargetMili returns a metric target for a valid given metric target type (Value or AverageValue) and value in mili scale +func GetMetricTargetMili(metricType v2beta2.MetricTargetType, metricValue float64) v2beta2.MetricTarget { + target := v2beta2.MetricTarget{ + Type: metricType, + } + + // Construct the target size as a quantity + metricValueMili := int64(metricValue * 1000) + targetQty := resource.NewMilliQuantity(metricValueMili, resource.DecimalSI) + if metricType == v2beta2.AverageValueMetricType { + target.AverageValue = targetQty + } else { + target.Value = targetQty + } + + return target +} + +// GenerateMetricInMili returns a externalMetricValue with mili as metric scale +func GenerateMetricInMili(metricName string, value float64) external_metrics.ExternalMetricValue { + valueMili := int64(value * 1000) + return external_metrics.ExternalMetricValue{ + MetricName: metricName, + Value: *resource.NewMilliQuantity(valueMili, resource.DecimalSI), + Timestamp: metav1.Now(), + } +} diff --git a/pkg/scalers/selenium_grid_scaler.go b/pkg/scalers/selenium_grid_scaler.go index abffbcb57ab..885a610f43a 100644 --- a/pkg/scalers/selenium_grid_scaler.go +++ b/pkg/scalers/selenium_grid_scaler.go @@ -13,8 +13,6 @@ import ( "strings" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -142,16 +140,12 @@ func (s *seleniumGridScaler) Close(context.Context) error { } func (s *seleniumGridScaler) GetMetrics(ctx context.Context, metricName string, metricSelector labels.Selector) ([]external_metrics.ExternalMetricValue, error) { - v, err := s.getSessionsCount(ctx) + sessions, err := s.getSessionsCount(ctx) if err != nil { return []external_metrics.ExternalMetricValue{}, fmt.Errorf("error requesting selenium grid endpoint: %s", err) } - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(v, resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric := GenerateMetricInMili(metricName, float64(sessions)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scalers/solace_scaler.go b/pkg/scalers/solace_scaler.go index 9261854c683..094b964c765 100644 --- a/pkg/scalers/solace_scaler.go +++ b/pkg/scalers/solace_scaler.go @@ -9,8 +9,6 @@ import ( "strings" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -335,17 +333,9 @@ func (s *SolaceScaler) GetMetrics(ctx context.Context, metricName string, metric var metric external_metrics.ExternalMetricValue switch { case strings.HasSuffix(metricName, solaceTriggermsgcount): - metric = external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(int64(metricValues.msgCount), resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric = GenerateMetricInMili(metricName, float64(metricValues.msgCount)) case strings.HasSuffix(metricName, solaceTriggermsgspoolusage): - metric = external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(int64(metricValues.msgSpoolUsage), resource.DecimalSI), - Timestamp: metav1.Now(), - } + metric = GenerateMetricInMili(metricName, float64(metricValues.msgSpoolUsage)) default: // Should never end up here err := fmt.Errorf("unidentified metric: %s", metricName) diff --git a/pkg/scalers/stan_scaler.go b/pkg/scalers/stan_scaler.go index 2440e7498a0..a06e864094b 100644 --- a/pkg/scalers/stan_scaler.go +++ b/pkg/scalers/stan_scaler.go @@ -9,8 +9,6 @@ import ( "strconv" v2beta2 "k8s.io/api/autoscaling/v2beta2" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/metrics/pkg/apis/external_metrics" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -237,12 +235,7 @@ func (s *stanScaler) GetMetrics(ctx context.Context, metricName string, metricSe } totalLag := s.getMaxMsgLag() stanLog.V(1).Info("Stan scaler: Providing metrics based on totalLag, threshold", "totalLag", totalLag, "lagThreshold", s.metadata.lagThreshold) - metric := external_metrics.ExternalMetricValue{ - MetricName: metricName, - Value: *resource.NewQuantity(totalLag, resource.DecimalSI), - Timestamp: metav1.Now(), - } - + metric := GenerateMetricInMili(metricName, float64(totalLag)) return append([]external_metrics.ExternalMetricValue{}, metric), nil } diff --git a/pkg/scaling/cache/scalers_cache.go b/pkg/scaling/cache/scalers_cache.go index d77a2f7b550..bd10eb734b4 100644 --- a/pkg/scaling/cache/scalers_cache.go +++ b/pkg/scaling/cache/scalers_cache.go @@ -19,6 +19,7 @@ package cache import ( "context" "fmt" + "math" "github.com/go-logr/logr" "k8s.io/api/autoscaling/v2beta2" @@ -116,8 +117,8 @@ func (c *ScalersCache) IsScaledObjectActive(ctx context.Context, scaledObject *k } func (c *ScalersCache) IsScaledJobActive(ctx context.Context, scaledJob *kedav1alpha1.ScaledJob) (bool, int64, int64) { - var queueLength int64 - var maxValue int64 + var queueLength float64 + var maxValue float64 isActive := false logger := logf.Log.WithName("scalemetrics") @@ -132,8 +133,8 @@ func (c *ScalersCache) IsScaledJobActive(ctx context.Context, scaledJob *kedav1a } } case "avg": - queueLengthSum := int64(0) - maxValueSum := int64(0) + queueLengthSum := float64(0) + maxValueSum := float64(0) length := 0 for _, metrics := range scalersMetrics { if metrics.isActive { @@ -144,8 +145,8 @@ func (c *ScalersCache) IsScaledJobActive(ctx context.Context, scaledJob *kedav1a } } if length != 0 { - queueLength = divideWithCeil(queueLengthSum, int64(length)) - maxValue = divideWithCeil(maxValueSum, int64(length)) + queueLength = queueLengthSum / float64(length) + maxValue = maxValueSum / float64(length) } case "sum": for _, metrics := range scalersMetrics { @@ -164,10 +165,10 @@ func (c *ScalersCache) IsScaledJobActive(ctx context.Context, scaledJob *kedav1a } } } - maxValue = min(scaledJob.MaxReplicaCount(), maxValue) + maxValue = min(float64(scaledJob.MaxReplicaCount()), maxValue) logger.V(1).WithValues("ScaledJob", scaledJob.Name).Info("Checking if ScaleJob Scalers are active", "isActive", isActive, "maxValue", maxValue, "MultipleScalersCalculation", scaledJob.Spec.ScalingStrategy.MultipleScalersCalculation) - return isActive, queueLength, maxValue + return isActive, ceilToInt64(queueLength), ceilToInt64(maxValue) } func (c *ScalersCache) GetMetrics(ctx context.Context, metricName string, metricSelector labels.Selector) ([]external_metrics.ExternalMetricValue, error) { @@ -230,18 +231,18 @@ func (c *ScalersCache) Close(ctx context.Context) { } type scalerMetrics struct { - queueLength int64 - maxValue int64 + queueLength float64 + maxValue float64 isActive bool } func (c *ScalersCache) getScaledJobMetrics(ctx context.Context, scaledJob *kedav1alpha1.ScaledJob) []scalerMetrics { var scalersMetrics []scalerMetrics for i, s := range c.Scalers { - var queueLength int64 - var targetAverageValue int64 + var queueLength float64 + var targetAverageValue float64 isActive := false - maxValue := int64(0) + maxValue := float64(0) scalerType := fmt.Sprintf("%T:", s) scalerLogger := c.Logger.WithValues("ScaledJob", scaledJob.Name, "Scaler", scalerType) @@ -278,11 +279,11 @@ func (c *ScalersCache) getScaledJobMetrics(ctx context.Context, scaledJob *kedav continue } - var metricValue int64 + var metricValue float64 for _, m := range metrics { if m.MetricName == metricSpecs[0].External.Metric.Name { - metricValue, _ = m.Value.AsInt64() + metricValue = m.Value.AsApproximateFloat64() queueLength += metricValue } } @@ -293,7 +294,8 @@ func (c *ScalersCache) getScaledJobMetrics(ctx context.Context, scaledJob *kedav } if targetAverageValue != 0 { - maxValue = min(scaledJob.MaxReplicaCount(), divideWithCeil(queueLength, targetAverageValue)) + averageLength := queueLength / targetAverageValue + maxValue = min(float64(scaledJob.MaxReplicaCount()), averageLength) } scalersMetrics = append(scalersMetrics, scalerMetrics{ queueLength: queueLength, @@ -304,40 +306,31 @@ func (c *ScalersCache) getScaledJobMetrics(ctx context.Context, scaledJob *kedav return scalersMetrics } -func getTargetAverageValue(metricSpecs []v2beta2.MetricSpec) int64 { - var targetAverageValue int64 - var metricValue int64 - var flag bool +func getTargetAverageValue(metricSpecs []v2beta2.MetricSpec) float64 { + var targetAverageValue float64 + var metricValue float64 for _, metric := range metricSpecs { if metric.External.Target.AverageValue == nil { metricValue = 0 } else { - metricValue, flag = metric.External.Target.AverageValue.AsInt64() - if !flag { - metricValue = 0 - } + metricValue = metric.External.Target.AverageValue.AsApproximateFloat64() } targetAverageValue += metricValue } - count := int64(len(metricSpecs)) + count := float64(len(metricSpecs)) if count != 0 { return targetAverageValue / count } return 0 } -func divideWithCeil(x, y int64) int64 { - ans := x / y - remainder := x % y - if remainder != 0 { - return ans + 1 - } - return ans +func ceilToInt64(x float64) int64 { + return int64(math.Ceil(x)) } -// Min function for int64 -func min(x, y int64) int64 { +// Min function for float64 +func min(x, y float64) float64 { if x > y { return y } diff --git a/pkg/scaling/cache/scalers_cache_test.go b/pkg/scaling/cache/scalers_cache_test.go index db7f3a8937f..f771454bf96 100644 --- a/pkg/scaling/cache/scalers_cache_test.go +++ b/pkg/scaling/cache/scalers_cache_test.go @@ -23,31 +23,31 @@ func TestTargetAverageValue(t *testing.T) { specs := []v2beta2.MetricSpec{} metricName := "s0-messageCount" targetAverageValue := getTargetAverageValue(specs) - assert.Equal(t, int64(0), targetAverageValue) + assert.Equal(t, float64(0), targetAverageValue) // 1 1 specs = []v2beta2.MetricSpec{ createMetricSpec(1, metricName), createMetricSpec(1, metricName), } targetAverageValue = getTargetAverageValue(specs) - assert.Equal(t, int64(1), targetAverageValue) - // 5 5 3 + assert.Equal(t, float64(1), targetAverageValue) + // 5 5 3 -> 4.333333333333333 specs = []v2beta2.MetricSpec{ createMetricSpec(5, metricName), createMetricSpec(5, metricName), createMetricSpec(3, metricName), } targetAverageValue = getTargetAverageValue(specs) - assert.Equal(t, int64(4), targetAverageValue) + assert.Equal(t, 4.333333333333333, targetAverageValue) - // 5 5 4 + // 5 5 4 -> 4.666666666666667 specs = []v2beta2.MetricSpec{ createMetricSpec(5, metricName), createMetricSpec(5, metricName), - createMetricSpec(3, metricName), + createMetricSpec(4, metricName), } targetAverageValue = getTargetAverageValue(specs) - assert.Equal(t, int64(4), targetAverageValue) + assert.Equal(t, 4.666666666666667, targetAverageValue) } func createMetricSpec(averageValue int64, metricName string) v2beta2.MetricSpec {