From 2062bf0f0a70e46b45f163a5644713ff3beb0b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xo=C3=A1n=20Mall=C3=B3n?= Date: Tue, 15 Mar 2022 21:29:37 +0100 Subject: [PATCH 1/3] feat: add logic for prometheus scaler X-Scope-OrgID header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Xoán Mallón --- pkg/scalers/prometheus_scaler.go | 21 +++++++++++++----- pkg/scalers/prometheus_scaler_test.go | 32 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/pkg/scalers/prometheus_scaler.go b/pkg/scalers/prometheus_scaler.go index beec6f965c6..005c7aa5fd9 100644 --- a/pkg/scalers/prometheus_scaler.go +++ b/pkg/scalers/prometheus_scaler.go @@ -22,11 +22,13 @@ import ( ) const ( - promServerAddress = "serverAddress" - promMetricName = "metricName" - promQuery = "query" - promThreshold = "threshold" - promNamespace = "namespace" + promServerAddress = "serverAddress" + promMetricName = "metricName" + promQuery = "query" + promThreshold = "threshold" + promNamespace = "namespace" + promCortexScopeOrgId = "cortexOrgId" + promCortexHeaderKey = "X-Scope-OrgID" ) type prometheusScaler struct { @@ -42,6 +44,7 @@ type prometheusMetadata struct { prometheusAuth *authentication.AuthMeta namespace string scalerIndex int + cortexOrgId string } type promQueryResult struct { @@ -118,6 +121,10 @@ func parsePrometheusMetadata(config *ScalerConfig) (meta *prometheusMetadata, er meta.namespace = val } + if val, ok := config.TriggerMetadata[promCortexScopeOrgId]; ok && val != "" { + meta.cortexOrgId = val + } + meta.scalerIndex = config.ScalerIndex // parse auth configs from ScalerConfig @@ -182,6 +189,10 @@ func (s *prometheusScaler) ExecutePromQuery(ctx context.Context) (float64, error req.SetBasicAuth(s.metadata.prometheusAuth.Username, s.metadata.prometheusAuth.Password) } + if s.metadata.cortexOrgId != "" { + req.Header.Add(promCortexHeaderKey, s.metadata.cortexOrgId) + } + r, err := s.httpClient.Do(req) if err != nil { return -1, err diff --git a/pkg/scalers/prometheus_scaler_test.go b/pkg/scalers/prometheus_scaler_test.go index 2caf0bb8046..b22939122ee 100644 --- a/pkg/scalers/prometheus_scaler_test.go +++ b/pkg/scalers/prometheus_scaler_test.go @@ -208,3 +208,35 @@ func TestPrometheusScalerExecutePromQuery(t *testing.T) { }) } } + +func TestPrometheusScalerCortexHeader(t *testing.T) { + testData := prometheusQromQueryResultTestData{ + name: "no values", + bodyStr: `{"data":{"result":[]}}`, + responseStatus: http.StatusOK, + expectedValue: 0, + isError: false, + } + cortexOrgValue := "my-org" + server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + reqHeader := request.Header.Get(promCortexHeaderKey) + assert.Equal(t, reqHeader, cortexOrgValue) + writer.WriteHeader(testData.responseStatus) + if _, err := writer.Write([]byte(testData.bodyStr)); err != nil { + t.Fatal(err) + } + })) + + scaler := prometheusScaler{ + metadata: &prometheusMetadata{ + serverAddress: server.URL, + cortexOrgId: cortexOrgValue, + }, + httpClient: http.DefaultClient, + } + + _, err := scaler.ExecutePromQuery(context.TODO()) + + assert.NoError(t, err) + +} From 14c690914ba794b15ab0c6db27363eb4447e65e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xo=C3=A1n=20Mall=C3=B3n?= Date: Tue, 15 Mar 2022 21:33:38 +0100 Subject: [PATCH 2/3] docs: update CHANGELOG MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Xoán Mallón --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2c8a0ded92..d6c2c599879 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ - **Datadog Scaler:** Several improvements, including a new optional parameter `metricUnavailableValue` to fill data when no Datadog metric was returned ([#2657](https://github.com/kedacore/keda/issues/2657)) - **GCP Pubsub Scaler** Adding e2e test for GCP PubSub scaler ([#1528](https://github.com/kedacore/keda/issues/1528)) - **Kafka Scaler** Make "disable" a valid value for tls auth parameter ([#2608](https://github.com/kedacore/keda/issues/2608)) +- **Prometheus Scaler:** Support for `X-Scope-OrgID` header in Prometheus scaler ([#2667](https://github.com/kedacore/keda/issues/2667)) - **RabbitMQ Scaler:** Include `vhost` for RabbitMQ when retrieving queue info with `useRegex` ([#2498](https://github.com/kedacore/keda/issues/2498)) ### Breaking Changes From 7e4a9edf020710df34678b2f6d5b2e79b6a210c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xo=C3=A1n=20Mall=C3=B3n?= Date: Tue, 15 Mar 2022 20:49:55 +0000 Subject: [PATCH 3/3] fix: solve golangci errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Xoán Mallón --- pkg/scalers/prometheus_scaler.go | 12 ++++++------ pkg/scalers/prometheus_scaler_test.go | 3 +-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/pkg/scalers/prometheus_scaler.go b/pkg/scalers/prometheus_scaler.go index 005c7aa5fd9..d58f289d945 100644 --- a/pkg/scalers/prometheus_scaler.go +++ b/pkg/scalers/prometheus_scaler.go @@ -27,7 +27,7 @@ const ( promQuery = "query" promThreshold = "threshold" promNamespace = "namespace" - promCortexScopeOrgId = "cortexOrgId" + promCortexScopeOrgID = "cortexOrgID" promCortexHeaderKey = "X-Scope-OrgID" ) @@ -44,7 +44,7 @@ type prometheusMetadata struct { prometheusAuth *authentication.AuthMeta namespace string scalerIndex int - cortexOrgId string + cortexOrgID string } type promQueryResult struct { @@ -121,8 +121,8 @@ func parsePrometheusMetadata(config *ScalerConfig) (meta *prometheusMetadata, er meta.namespace = val } - if val, ok := config.TriggerMetadata[promCortexScopeOrgId]; ok && val != "" { - meta.cortexOrgId = val + if val, ok := config.TriggerMetadata[promCortexScopeOrgID]; ok && val != "" { + meta.cortexOrgID = val } meta.scalerIndex = config.ScalerIndex @@ -189,8 +189,8 @@ func (s *prometheusScaler) ExecutePromQuery(ctx context.Context) (float64, error req.SetBasicAuth(s.metadata.prometheusAuth.Username, s.metadata.prometheusAuth.Password) } - if s.metadata.cortexOrgId != "" { - req.Header.Add(promCortexHeaderKey, s.metadata.cortexOrgId) + if s.metadata.cortexOrgID != "" { + req.Header.Add(promCortexHeaderKey, s.metadata.cortexOrgID) } r, err := s.httpClient.Do(req) diff --git a/pkg/scalers/prometheus_scaler_test.go b/pkg/scalers/prometheus_scaler_test.go index b22939122ee..01fa93e13f8 100644 --- a/pkg/scalers/prometheus_scaler_test.go +++ b/pkg/scalers/prometheus_scaler_test.go @@ -230,7 +230,7 @@ func TestPrometheusScalerCortexHeader(t *testing.T) { scaler := prometheusScaler{ metadata: &prometheusMetadata{ serverAddress: server.URL, - cortexOrgId: cortexOrgValue, + cortexOrgID: cortexOrgValue, }, httpClient: http.DefaultClient, } @@ -238,5 +238,4 @@ func TestPrometheusScalerCortexHeader(t *testing.T) { _, err := scaler.ExecutePromQuery(context.TODO()) assert.NoError(t, err) - }