From 0d970b00b170d931b6d08f0624c6da53c74ee308 Mon Sep 17 00:00:00 2001 From: VAN KELST Baptiste Date: Wed, 3 Jan 2024 15:14:39 +0100 Subject: [PATCH 1/4] feat(gcp-stackdriver): handling value if no timeseries is return Signed-off-by: VAN KELST Baptiste --- pkg/scalers/gcp_cloud_tasks_scaler.go | 2 +- pkg/scalers/gcp_stackdriver_client.go | 8 ++++++-- pkg/scalers/gcp_stackdriver_scaler.go | 15 ++++++++++++++- pkg/scalers/gcp_stackdriver_scaler_test.go | 4 ++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/pkg/scalers/gcp_cloud_tasks_scaler.go b/pkg/scalers/gcp_cloud_tasks_scaler.go index ba2f7c42b22..dee2cc38798 100644 --- a/pkg/scalers/gcp_cloud_tasks_scaler.go +++ b/pkg/scalers/gcp_cloud_tasks_scaler.go @@ -179,5 +179,5 @@ func (s *gcpCloudTasksScaler) getMetrics(ctx context.Context, metricType string) // Cloud Tasks metrics are collected every 60 seconds so no need to aggregate them. // See: https://cloud.google.com/monitoring/api/metrics_gcp#gcp-cloudtasks - return s.client.GetMetrics(ctx, filter, s.metadata.projectID, nil) + return s.client.GetMetrics(ctx, filter, s.metadata.projectID, nil, nil) } diff --git a/pkg/scalers/gcp_stackdriver_client.go b/pkg/scalers/gcp_stackdriver_client.go index b6a0901f6d2..ca20f3ceb5c 100644 --- a/pkg/scalers/gcp_stackdriver_client.go +++ b/pkg/scalers/gcp_stackdriver_client.go @@ -213,7 +213,8 @@ func (s StackDriverClient) GetMetrics( ctx context.Context, filter string, projectID string, - aggregation *monitoringpb.Aggregation) (float64, error) { + aggregation *monitoringpb.Aggregation, + valueIfNull *float64) (float64, error) { // Set the start time to 1 minute ago startTime := time.Now().UTC().Add(time.Minute * -2) @@ -246,7 +247,10 @@ func (s StackDriverClient) GetMetrics( resp, err := it.Next() if err == iterator.Done { - return value, fmt.Errorf("could not find stackdriver metric with filter %s", filter) + if valueIfNull == nil { + return value, fmt.Errorf("could not find stackdriver metric with filter %s", filter) + } + return *valueIfNull, nil } if err != nil { diff --git a/pkg/scalers/gcp_stackdriver_scaler.go b/pkg/scalers/gcp_stackdriver_scaler.go index eb4b3a60bd2..d6543011389 100644 --- a/pkg/scalers/gcp_stackdriver_scaler.go +++ b/pkg/scalers/gcp_stackdriver_scaler.go @@ -30,6 +30,7 @@ type stackdriverMetadata struct { targetValue float64 activationTargetValue float64 metricName string + valueIfNull *float64 gcpAuthorization *gcpAuthorizationMetadata aggregation *monitoringpb.Aggregation @@ -109,6 +110,18 @@ func parseStackdriverMetadata(config *ScalerConfig, logger logr.Logger) (*stackd meta.activationTargetValue = activationTargetValue } + if val, ok := config.TriggerMetadata["valueIfNull"]; ok { + if val == "" { + meta.valueIfNull = nil + } else { + valueIfNull, err := strconv.ParseFloat(val, 64) + if err != nil { + return nil, fmt.Errorf("valueIfNull parsing error %w", err) + } + meta.valueIfNull = &valueIfNull + } + } + auth, err := getGCPAuthorization(config) if err != nil { return nil, err @@ -207,7 +220,7 @@ func (s *stackdriverScaler) GetMetricsAndActivity(ctx context.Context, metricNam // getMetrics gets metric type value from stackdriver api func (s *stackdriverScaler) getMetrics(ctx context.Context) (float64, error) { - val, err := s.client.GetMetrics(ctx, s.metadata.filter, s.metadata.projectID, s.metadata.aggregation) + val, err := s.client.GetMetrics(ctx, s.metadata.filter, s.metadata.projectID, s.metadata.aggregation, s.metadata.valueIfNull) if err == nil { s.logger.V(1).Info( fmt.Sprintf("Getting metrics for project %s, filter %s and aggregation %v. Result: %f", diff --git a/pkg/scalers/gcp_stackdriver_scaler_test.go b/pkg/scalers/gcp_stackdriver_scaler_test.go index 54d1ca08c2f..46c20709127 100644 --- a/pkg/scalers/gcp_stackdriver_scaler_test.go +++ b/pkg/scalers/gcp_stackdriver_scaler_test.go @@ -55,6 +55,10 @@ var testStackdriverMetadata = []parseStackdriverMetadataTestData{ {nil, map[string]string{"projectId": "myProject", "filter": sdFilter, "credentialsFromEnv": "SAMPLE_CREDS", "alignmentPeriodSeconds": "a"}, true}, // properly formed float targetValue and activationTargetValue {nil, map[string]string{"projectId": "myProject", "filter": sdFilter, "credentialsFromEnv": "SAMPLE_CREDS", "targetValue": "1.1", "activationTargetValue": "2.1"}, false}, + //properly formed float valueIfNull + {nil, map[string]string{"projectId": "myProject", "filter": sdFilter, "credentialsFromEnv": "SAMPLE_CREDS", "targetValue": "1.1", "activationTargetValue": "2.1", "valueIfNull": "1.0"}, false}, + // With bad valueIfNull + {nil, map[string]string{"projectId": "myProject", "filter": sdFilter, "credentialsFromEnv": "SAMPLE_CREDS", "targetValue": "1.1", "activationTargetValue": "2.1", "valueIfNull": "toto"}, true}, } var gcpStackdriverMetricIdentifiers = []gcpStackdriverMetricIdentifier{ From dc1f4568dcf84e4526d67c2c9f67ddd557e9341e Mon Sep 17 00:00:00 2001 From: VAN KELST Baptiste Date: Wed, 3 Jan 2024 15:35:08 +0100 Subject: [PATCH 2/4] fix bad space Signed-off-by: VAN KELST Baptiste --- pkg/scalers/gcp_stackdriver_scaler_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/scalers/gcp_stackdriver_scaler_test.go b/pkg/scalers/gcp_stackdriver_scaler_test.go index 46c20709127..f20bd3e4250 100644 --- a/pkg/scalers/gcp_stackdriver_scaler_test.go +++ b/pkg/scalers/gcp_stackdriver_scaler_test.go @@ -55,7 +55,7 @@ var testStackdriverMetadata = []parseStackdriverMetadataTestData{ {nil, map[string]string{"projectId": "myProject", "filter": sdFilter, "credentialsFromEnv": "SAMPLE_CREDS", "alignmentPeriodSeconds": "a"}, true}, // properly formed float targetValue and activationTargetValue {nil, map[string]string{"projectId": "myProject", "filter": sdFilter, "credentialsFromEnv": "SAMPLE_CREDS", "targetValue": "1.1", "activationTargetValue": "2.1"}, false}, - //properly formed float valueIfNull + // properly formed float valueIfNull {nil, map[string]string{"projectId": "myProject", "filter": sdFilter, "credentialsFromEnv": "SAMPLE_CREDS", "targetValue": "1.1", "activationTargetValue": "2.1", "valueIfNull": "1.0"}, false}, // With bad valueIfNull {nil, map[string]string{"projectId": "myProject", "filter": sdFilter, "credentialsFromEnv": "SAMPLE_CREDS", "targetValue": "1.1", "activationTargetValue": "2.1", "valueIfNull": "toto"}, true}, From 1b1e8021e1e6fecdc2de1eb70f0d6155d95888f7 Mon Sep 17 00:00:00 2001 From: VAN KELST Baptiste Date: Wed, 3 Jan 2024 16:47:24 +0100 Subject: [PATCH 3/4] update changelog Signed-off-by: VAN KELST Baptiste --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 099b2f18014..a123f4d6304 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,7 @@ Here is an overview of all new **experimental** features: - **General**: Support TriggerAuthentication properties from ConfigMap ([#4830](https://github.com/kedacore/keda/issues/4830)) - **General**: Use client-side round-robin load balancing for grpc calls ([#5224](https://github.com/kedacore/keda/issues/5224)) - **GCP pubsub scaler**: Support distribution-valued metrics and metrics from topics ([#5070](https://github.com/kedacore/keda/issues/5070)) +- **GCP stackdriver scaler**: Support valueIfNull parameter ([#5345](https://github.com/kedacore/keda/pull/5345)) - **Hashicorp Vault**: Add support to get secret that needs write operation (e.g. pki) ([#5067](https://github.com/kedacore/keda/issues/5067)) - **Hashicorp Vault**: Fix operator panic when spec.hashiCorpVault.credential.serviceAccount is not set ([#4964](https://github.com/kedacore/keda/issues/4964)) - **Hashicorp Vault**: Fix operator panic when using root token to authenticate to vault server ([#5192](https://github.com/kedacore/keda/issues/5192)) From d4d8e74ac7d0ad583e170e5f3d95a4c5c84b49d8 Mon Sep 17 00:00:00 2001 From: lindmin Date: Wed, 3 Jan 2024 17:32:20 +0100 Subject: [PATCH 4/4] Update pkg/scalers/gcp_stackdriver_scaler.go Co-authored-by: Jorge Turrado Ferrero Signed-off-by: lindmin --- pkg/scalers/gcp_stackdriver_scaler.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/pkg/scalers/gcp_stackdriver_scaler.go b/pkg/scalers/gcp_stackdriver_scaler.go index d6543011389..1b63bd09cb5 100644 --- a/pkg/scalers/gcp_stackdriver_scaler.go +++ b/pkg/scalers/gcp_stackdriver_scaler.go @@ -110,16 +110,12 @@ func parseStackdriverMetadata(config *ScalerConfig, logger logr.Logger) (*stackd meta.activationTargetValue = activationTargetValue } - if val, ok := config.TriggerMetadata["valueIfNull"]; ok { - if val == "" { - meta.valueIfNull = nil - } else { - valueIfNull, err := strconv.ParseFloat(val, 64) - if err != nil { - return nil, fmt.Errorf("valueIfNull parsing error %w", err) - } - meta.valueIfNull = &valueIfNull + if val, ok := config.TriggerMetadata["valueIfNull"]; ok && val != "" { + valueIfNull, err := strconv.ParseFloat(val, 64) + if err != nil { + return nil, fmt.Errorf("valueIfNull parsing error %w", err) } + meta.valueIfNull = &valueIfNull } auth, err := getGCPAuthorization(config)