From 69c00c6a8f121e7881d1b3e2b5a06745961dee86 Mon Sep 17 00:00:00 2001 From: mortx Date: Wed, 2 Mar 2022 14:55:37 +0000 Subject: [PATCH 1/4] Added Demand checker to validate job Signed-off-by: mortx --- pkg/scalers/azure_pipelines_scaler.go | 36 +++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/pkg/scalers/azure_pipelines_scaler.go b/pkg/scalers/azure_pipelines_scaler.go index 8d0d8ec7ce8..1ff5cebff0e 100644 --- a/pkg/scalers/azure_pipelines_scaler.go +++ b/pkg/scalers/azure_pipelines_scaler.go @@ -41,6 +41,8 @@ type azurePipelinesMetadata struct { organizationURL string organizationName string personalAccessToken string + parent string + scalarName string poolID int targetPipelinesQueueLength int scalerIndex int @@ -100,6 +102,12 @@ func parseAzurePipelinesMetadata(ctx context.Context, config *ScalerConfig, http return nil, fmt.Errorf("no personalAccessToken given") } + if val, ok := config.TriggerMetadata["parent"]; ok && val != "" { + meta.parent = config.TriggerMetadata["parent"] + } else { + meta.parent = "" + } + if val, ok := config.TriggerMetadata["poolName"]; ok && val != "" { var err error meta.poolID, err = getPoolIDFromName(ctx, val, &meta, httpClient) @@ -119,6 +127,7 @@ func parseAzurePipelinesMetadata(ctx context.Context, config *ScalerConfig, http } meta.scalerIndex = config.ScalerIndex + meta.scalarName = config.Name return &meta, nil } @@ -230,13 +239,36 @@ func (s *azurePipelinesScaler) GetAzurePipelinesQueueLength(ctx context.Context) for _, value := range jobs { v := value.(map[string]interface{}) if v["result"] == nil { - count++ + if s.metadata.parent == "" { + // keep the old template working + count++ + } else { + if getCanAgentFulfilJob(v, s.metadata) { + count++ + } + } } } - return count, err } +// Determine if the Job and Agent have matching capabilities +func getCanAgentFulfilJob(v map[string]interface{}, metadata *azurePipelinesMetadata) bool { + matchedAgents, ok := v["matchedAgents"].([]interface{}) + if !ok { + // ADO is already processing + return false + } + + for _, m := range matchedAgents { + n := m.(map[string]interface{}) + if metadata.parent == n["name"].(string) { + return true + } + } + return false +} + func (s *azurePipelinesScaler) GetMetricSpecForScaling(context.Context) []v2beta2.MetricSpec { targetPipelinesQueueLengthQty := resource.NewQuantity(int64(s.metadata.targetPipelinesQueueLength), resource.DecimalSI) externalMetric := &v2beta2.ExternalMetricSource{ From a29cefdd37581ec16696e277b8252bd6595c5752 Mon Sep 17 00:00:00 2001 From: mortx Date: Wed, 2 Mar 2022 15:01:44 +0000 Subject: [PATCH 2/4] golint advisor - if else@245 Signed-off-by: mortx --- pkg/scalers/azure_pipelines_scaler.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/scalers/azure_pipelines_scaler.go b/pkg/scalers/azure_pipelines_scaler.go index 1ff5cebff0e..6844a40c7d5 100644 --- a/pkg/scalers/azure_pipelines_scaler.go +++ b/pkg/scalers/azure_pipelines_scaler.go @@ -242,10 +242,8 @@ func (s *azurePipelinesScaler) GetAzurePipelinesQueueLength(ctx context.Context) if s.metadata.parent == "" { // keep the old template working count++ - } else { - if getCanAgentFulfilJob(v, s.metadata) { - count++ - } + } else if getCanAgentFulfilJob(v, s.metadata) { + count++ } } } From 6979a0e4c1cce0aa48f4729a5f06ebafb01e67a3 Mon Sep 17 00:00:00 2001 From: Eldarrin <32762846+Eldarrin@users.noreply.github.com> Date: Thu, 3 Mar 2022 09:04:52 +0000 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 073fba8c069..e879f926b3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ - **Azure Queue:** Don't call Azure queue GetProperties API unnecessarily ([#2613](https://github.com/kedacore/keda/pull/2613)) - **Datadog Scaler:** Validate query to contain `{` to prevent panic on invalid query ([#2625](https://github.com/kedacore/keda/issues/2625)) - **Kafka Scaler** Make "disable" a valid value for tls auth parameter ([#2608](https://github.com/kedacore/keda/issues/2608)) +- **Azure Pipelines:** Added the ability to support ADO demands by referencing a master template ([#2707](https://github.com/kedacore/keda/pull/2707)) ### Breaking Changes From bb81edde9bd5705764fc1e8afd1b2351d4c01c4f Mon Sep 17 00:00:00 2001 From: mortx Date: Tue, 22 Mar 2022 14:40:34 +0000 Subject: [PATCH 4/4] Added Tests to the Pipelines demand checker Signed-off-by: mortx --- CHANGELOG.md | 3 ++ pkg/scalers/azure_pipelines_scaler.go | 2 -- pkg/scalers/azure_pipelines_scaler_test.go | 40 ++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 073fba8c069..51733475951 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ ### Improvements +- **Azure Pipelines:** Added the ability to support ADO demands by referencing a master template ([#2707](https://github.com/kedacore/keda/pull/2707)) - **Azure Queue:** Don't call Azure queue GetProperties API unnecessarily ([#2613](https://github.com/kedacore/keda/pull/2613)) - **Datadog Scaler:** Validate query to contain `{` to prevent panic on invalid query ([#2625](https://github.com/kedacore/keda/issues/2625)) - **Kafka Scaler** Make "disable" a valid value for tls auth parameter ([#2608](https://github.com/kedacore/keda/issues/2608)) @@ -538,3 +539,5 @@ None. ### Other None. + + diff --git a/pkg/scalers/azure_pipelines_scaler.go b/pkg/scalers/azure_pipelines_scaler.go index 6844a40c7d5..df95f861bb9 100644 --- a/pkg/scalers/azure_pipelines_scaler.go +++ b/pkg/scalers/azure_pipelines_scaler.go @@ -42,7 +42,6 @@ type azurePipelinesMetadata struct { organizationName string personalAccessToken string parent string - scalarName string poolID int targetPipelinesQueueLength int scalerIndex int @@ -127,7 +126,6 @@ func parseAzurePipelinesMetadata(ctx context.Context, config *ScalerConfig, http } meta.scalerIndex = config.ScalerIndex - meta.scalarName = config.Name return &meta, nil } diff --git a/pkg/scalers/azure_pipelines_scaler_test.go b/pkg/scalers/azure_pipelines_scaler_test.go index be69a8b596a..db650ada0ad 100644 --- a/pkg/scalers/azure_pipelines_scaler_test.go +++ b/pkg/scalers/azure_pipelines_scaler_test.go @@ -2,6 +2,7 @@ package scalers import ( "context" + "fmt" "net/http" "net/http/httptest" "testing" @@ -158,3 +159,42 @@ func TestAzurePipelinesGetMetricSpecForScaling(t *testing.T) { } } } + +func getMatchedAgentMetaData(url string) *azurePipelinesMetadata { + meta := azurePipelinesMetadata{} + meta.organizationName = "test" + meta.organizationURL = url + meta.parent = "test-keda-template" + meta.personalAccessToken = "test" + meta.poolID = 1 + meta.targetPipelinesQueueLength = 1 + + return &meta +} + +func TestAzurePipelinesMatchedAgent(t *testing.T) { + var response = `{"count":1,"value":[{"demands":["Agent.Version -gtVersion 2.144.0"],"matchedAgents":[{"id":1,"name":"test-keda-template"}]}]}` + + var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(response)) + })) + + meta := getMatchedAgentMetaData(apiStub.URL) + + mockAzurePipelinesScaler := azurePipelinesScaler{ + metadata: meta, + httpClient: http.DefaultClient, + } + + queuelen, err := mockAzurePipelinesScaler.GetAzurePipelinesQueueLength(context.TODO()) + + if err != nil { + t.Fail() + fmt.Println(err) + } + + if queuelen < 1 { + t.Fail() + } +}