Skip to content

Commit

Permalink
Add several azure scalers
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Turrado <jorge.turrado@docplanner.com>
  • Loading branch information
Jorge Turrado committed Oct 9, 2021
1 parent f931c75 commit cafcbc3
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 58 deletions.
27 changes: 15 additions & 12 deletions pkg/scalers/azure_blob_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ type azureBlobScaler struct {
}

type azureBlobMetadata struct {
targetBlobCount int
blobContainerName string
blobDelimiter string
blobPrefix string
connection string
accountName string
metricName string
endpointSuffix string
targetBlobCount int
blobContainerName string
blobDelimiter string
blobPrefix string
connection string
accountName string
externalMetricName string
endpointSuffix string
}

var azureBlobLog = logf.Log.WithName("azure_blob_scaler")
Expand Down Expand Up @@ -117,15 +117,18 @@ func parseAzureBlobMetadata(config *ScalerConfig) (*azureBlobMetadata, kedav1alp
}

if val, ok := config.TriggerMetadata["metricName"]; ok {
meta.metricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "azure-blob", val))
meta.externalMetricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "azure-blob", val))
} else {
if meta.blobPrefix != "" {
meta.metricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s-%s", "azure-blob", meta.blobContainerName, meta.blobPrefix))
meta.externalMetricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s-%s", "azure-blob", meta.blobContainerName, meta.blobPrefix))
} else {
meta.metricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "azure-blob", meta.blobContainerName))
meta.externalMetricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "azure-blob", meta.blobContainerName))
}
}

// Update externalMetricName with the index
meta.externalMetricName = GenerateMetricNameWithIndex(config, meta.externalMetricName)

// If the Use AAD Pod Identity is not present, or set to "none"
// then check for connection string
switch config.PodIdentity {
Expand Down Expand Up @@ -185,7 +188,7 @@ func (s *azureBlobScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec {
targetBlobCount := resource.NewQuantity(int64(s.metadata.targetBlobCount), resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: s.metadata.metricName,
Name: s.metadata.externalMetricName,
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
9 changes: 5 additions & 4 deletions pkg/scalers/azure_blob_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type parseAzBlobMetadataTestData struct {

type azBlobMetricIdentifier struct {
metadataTestData *parseAzBlobMetadataTestData
scalerIndex int
name string
}

Expand Down Expand Up @@ -72,9 +73,9 @@ var testAzBlobMetadata = []parseAzBlobMetadataTestData{
}

var azBlobMetricIdentifiers = []azBlobMetricIdentifier{
{&testAzBlobMetadata[1], "azure-blob-sample-blobsubpath-"},
{&testAzBlobMetadata[2], "azure-blob-customname"},
{&testAzBlobMetadata[5], "azure-blob-sample_container"},
{&testAzBlobMetadata[1], 0, "s0-azure-blob-sample-blobsubpath-"},
{&testAzBlobMetadata[2], 1, "s1-azure-blob-customname"},
{&testAzBlobMetadata[5], 2, "s2-azure-blob-sample_container"},
}

func TestAzBlobParseMetadata(t *testing.T) {
Expand All @@ -94,7 +95,7 @@ func TestAzBlobParseMetadata(t *testing.T) {

func TestAzBlobGetMetricSpecForScaling(t *testing.T) {
for _, testData := range azBlobMetricIdentifiers {
meta, podIdentity, err := parseAzureBlobMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ResolvedEnv: testData.metadataTestData.resolvedEnv, AuthParams: testData.metadataTestData.authParams, PodIdentity: testData.metadataTestData.podIdentity})
meta, podIdentity, err := parseAzureBlobMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ResolvedEnv: testData.metadataTestData.resolvedEnv, AuthParams: testData.metadataTestData.authParams, PodIdentity: testData.metadataTestData.podIdentity, Index: testData.scalerIndex})
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/scalers/azure_eventhub_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ type azureEventHubScaler struct {
}

type eventHubMetadata struct {
eventHubInfo azure.EventHubInfo
threshold int64
eventHubInfo azure.EventHubInfo
threshold int64
externalMetricName string
}

// NewAzureEventHubScaler creates a new scaler for eventHub
Expand Down Expand Up @@ -152,6 +153,11 @@ func parseAzureEventHubMetadata(config *ScalerConfig) (*eventHubMetadata, error)
}
}

meta.externalMetricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s-%s", "azure-eventhub", meta.eventHubInfo.EventHubConnection, meta.eventHubInfo.EventHubConsumerGroup))

// Update externalMetricName with the index
meta.externalMetricName = GenerateMetricNameWithIndex(config, meta.externalMetricName)

return &meta, nil
}

Expand Down Expand Up @@ -252,7 +258,7 @@ func (scaler *azureEventHubScaler) GetMetricSpecForScaling() []v2beta2.MetricSpe
targetMetricVal := resource.NewQuantity(scaler.metadata.threshold, resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: kedautil.NormalizeString(fmt.Sprintf("%s-%s-%s", "azure-eventhub", scaler.metadata.eventHubInfo.EventHubConnection, scaler.metadata.eventHubInfo.EventHubConsumerGroup)),
Name: scaler.metadata.externalMetricName,
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
6 changes: 4 additions & 2 deletions pkg/scalers/azure_eventhub_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type parseEventHubMetadataTestData struct {

type eventHubMetricIdentifier struct {
metadataTestData *parseEventHubMetadataTestData
scalerIndex int
name string
}

Expand Down Expand Up @@ -65,7 +66,8 @@ var parseEventHubMetadataDatasetWithPodIdentity = []parseEventHubMetadataTestDat
}

var eventHubMetricIdentifiers = []eventHubMetricIdentifier{
{&parseEventHubMetadataDataset[1], "azure-eventhub-none-testEventHubConsumerGroup"},
{&parseEventHubMetadataDataset[1], 0, "s0-azure-eventhub-none-testEventHubConsumerGroup"},
{&parseEventHubMetadataDataset[1], 1, "s1-azure-eventhub-none-testEventHubConsumerGroup"},
}

var testEventHubScaler = azureEventHubScaler{
Expand Down Expand Up @@ -435,7 +437,7 @@ func DeleteContainerInStorage(ctx context.Context, endpoint *url.URL, credential

func TestEventHubGetMetricSpecForScaling(t *testing.T) {
for _, testData := range eventHubMetricIdentifiers {
meta, err := parseAzureEventHubMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ResolvedEnv: sampleEventHubResolvedEnv, AuthParams: map[string]string{}})
meta, err := parseAzureEventHubMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ResolvedEnv: sampleEventHubResolvedEnv, AuthParams: map[string]string{}, Index: testData.scalerIndex})
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
Expand Down
25 changes: 14 additions & 11 deletions pkg/scalers/azure_log_analytics_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ type azureLogAnalyticsScaler struct {
}

type azureLogAnalyticsMetadata struct {
tenantID string
clientID string
clientSecret string
workspaceID string
podIdentity string
query string
threshold int64
metricName string // Custom metric name for trigger
tenantID string
clientID string
clientSecret string
workspaceID string
podIdentity string
query string
threshold int64
externalMetricName string // Custom metric name for trigger
}

type sessionCache struct {
Expand Down Expand Up @@ -180,11 +180,14 @@ func parseAzureLogAnalyticsMetadata(config *ScalerConfig) (*azureLogAnalyticsMet

// Resolve metricName
if val, ok := config.TriggerMetadata["metricName"]; ok {
meta.metricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "azure-log-analytics", val))
meta.externalMetricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "azure-log-analytics", val))
} else {
meta.metricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "azure-log-analytics", meta.workspaceID))
meta.externalMetricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "azure-log-analytics", meta.workspaceID))
}

// Update externalMetricName with the index
meta.externalMetricName = GenerateMetricNameWithIndex(config, meta.externalMetricName)

return &meta, nil
}

Expand Down Expand Up @@ -222,7 +225,7 @@ func (s *azureLogAnalyticsScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec

externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: s.metadata.metricName,
Name: s.metadata.externalMetricName,
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
21 changes: 12 additions & 9 deletions pkg/scalers/azure_log_analytics_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type parseLogAnalyticsMetadataTestData struct {

type LogAnalyticsMetricIdentifier struct {
metadataTestData *parseLogAnalyticsMetadataTestData
scalerIndex int
name string
}

Expand Down Expand Up @@ -90,7 +91,8 @@ var testLogAnalyticsMetadata = []parseLogAnalyticsMetadataTestData{
}

var LogAnalyticsMetricIdentifiers = []LogAnalyticsMetricIdentifier{
{&testLogAnalyticsMetadata[7], "azure-log-analytics-074dd9f8-c368-4220-9400-acb6e80fc325"},
{&testLogAnalyticsMetadata[7], 0, "s0-azure-log-analytics-074dd9f8-c368-4220-9400-acb6e80fc325"},
{&testLogAnalyticsMetadata[7], 1, "s1-azure-log-analytics-074dd9f8-c368-4220-9400-acb6e80fc325"},
}

var testLogAnalyticsMetadataWithEmptyAuthParams = []parseLogAnalyticsMetadataTestData{
Expand Down Expand Up @@ -159,7 +161,7 @@ func TestLogAnalyticsParseMetadata(t *testing.T) {

func TestLogAnalyticsGetMetricSpecForScaling(t *testing.T) {
for _, testData := range LogAnalyticsMetricIdentifiers {
meta, err := parseAzureLogAnalyticsMetadata(&ScalerConfig{ResolvedEnv: sampleLogAnalyticsResolvedEnv, TriggerMetadata: testData.metadataTestData.metadata, AuthParams: nil, PodIdentity: ""})
meta, err := parseAzureLogAnalyticsMetadata(&ScalerConfig{ResolvedEnv: sampleLogAnalyticsResolvedEnv, TriggerMetadata: testData.metadataTestData.metadata, AuthParams: nil, PodIdentity: "", Index: testData.scalerIndex})
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
Expand All @@ -181,25 +183,26 @@ func TestLogAnalyticsGetMetricSpecForScaling(t *testing.T) {
}

type parseMetadataMetricNameTestData struct {
metadata map[string]string
metricName string
metadata map[string]string
scalerIndex int
metricName string
}

var testParseMetadataMetricName = []parseMetadataMetricNameTestData{
// WorkspaceID
{map[string]string{"tenantIdFromEnv": "d248da64-0e1e-4f79-b8c6-72ab7aa055eb", "clientIdFromEnv": "41826dd4-9e0a-4357-a5bd-a88ad771ea7d", "clientSecretFromEnv": "U6DtAX5r6RPZxd~l12Ri3X8J9urt5Q-xs", "workspaceIdFromEnv": "074dd9f8-c368-4220-9400-acb6e80fc325", "query": query, "threshold": "1900000000"}, "azure-log-analytics-074dd9f8-c368-4220-9400-acb6e80fc325"},
{map[string]string{"tenantIdFromEnv": "d248da64-0e1e-4f79-b8c6-72ab7aa055eb", "clientIdFromEnv": "41826dd4-9e0a-4357-a5bd-a88ad771ea7d", "clientSecretFromEnv": "U6DtAX5r6RPZxd~l12Ri3X8J9urt5Q-xs", "workspaceIdFromEnv": "074dd9f8-c368-4220-9400-acb6e80fc325", "query": query, "threshold": "1900000000"}, 0, "s0-azure-log-analytics-074dd9f8-c368-4220-9400-acb6e80fc325"},
// Custom Name
{map[string]string{"metricName": "testName", "tenantIdFromEnv": "d248da64-0e1e-4f79-b8c6-72ab7aa055eb", "clientIdFromEnv": "41826dd4-9e0a-4357-a5bd-a88ad771ea7d", "clientSecretFromEnv": "U6DtAX5r6RPZxd~l12Ri3X8J9urt5Q-xs", "workspaceIdFromEnv": "074dd9f8-c368-4220-9400-acb6e80fc325", "query": query, "threshold": "1900000000"}, "azure-log-analytics-testName"},
{map[string]string{"metricName": "testName", "tenantIdFromEnv": "d248da64-0e1e-4f79-b8c6-72ab7aa055eb", "clientIdFromEnv": "41826dd4-9e0a-4357-a5bd-a88ad771ea7d", "clientSecretFromEnv": "U6DtAX5r6RPZxd~l12Ri3X8J9urt5Q-xs", "workspaceIdFromEnv": "074dd9f8-c368-4220-9400-acb6e80fc325", "query": query, "threshold": "1900000000"}, 1, "s1-azure-log-analytics-testName"},
}

func TestLogAnalyticsParseMetadataMetricName(t *testing.T) {
for _, testData := range testParseMetadataMetricName {
meta, err := parseAzureLogAnalyticsMetadata(&ScalerConfig{ResolvedEnv: sampleLogAnalyticsResolvedEnv, TriggerMetadata: testData.metadata, AuthParams: nil, PodIdentity: ""})
meta, err := parseAzureLogAnalyticsMetadata(&ScalerConfig{ResolvedEnv: sampleLogAnalyticsResolvedEnv, TriggerMetadata: testData.metadata, AuthParams: nil, PodIdentity: "", Index: testData.scalerIndex})
if err != nil {
t.Error("Expected success but got error", err)
}
if meta.metricName != testData.metricName {
t.Errorf("Expected %s but got %s", testData.metricName, meta.metricName)
if meta.externalMetricName != testData.metricName {
t.Errorf("Expected %s but got %s", testData.metricName, meta.externalMetricName)
}
}
}
12 changes: 9 additions & 3 deletions pkg/scalers/azure_monitor_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ type azureMonitorScaler struct {
}

type azureMonitorMetadata struct {
azureMonitorInfo azure.MonitorInfo
targetValue int
azureMonitorInfo azure.MonitorInfo
targetValue int
externalMetricName string
}

var azureMonitorLog = logf.Log.WithName("azure_monitor_scaler")
Expand Down Expand Up @@ -145,6 +146,11 @@ func parseAzureMonitorMetadata(config *ScalerConfig) (*azureMonitorMetadata, err
meta.azureMonitorInfo.ClientID = clientID
meta.azureMonitorInfo.ClientPassword = clientPassword

meta.externalMetricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s-%s-%s", "azure-monitor", meta.azureMonitorInfo.ResourceURI, meta.azureMonitorInfo.ResourceGroupName, meta.azureMonitorInfo.Name))

// Update externalMetricName with the index
meta.externalMetricName = GenerateMetricNameWithIndex(config, meta.externalMetricName)

return &meta, nil
}

Expand Down Expand Up @@ -191,7 +197,7 @@ func (s *azureMonitorScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec {
targetMetricVal := resource.NewQuantity(int64(s.metadata.targetValue), resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: kedautil.NormalizeString(fmt.Sprintf("%s-%s-%s-%s", "azure-monitor", s.metadata.azureMonitorInfo.ResourceURI, s.metadata.azureMonitorInfo.ResourceGroupName, s.metadata.azureMonitorInfo.Name)),
Name: s.metadata.externalMetricName,
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
6 changes: 4 additions & 2 deletions pkg/scalers/azure_monitor_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type parseAzMonitorMetadataTestData struct {

type azMonitorMetricIdentifier struct {
metadataTestData *parseAzMonitorMetadataTestData
scalerIndex int
name string
}

Expand Down Expand Up @@ -80,7 +81,8 @@ var testParseAzMonitorMetadata = []parseAzMonitorMetadataTestData{
}

var azMonitorMetricIdentifiers = []azMonitorMetricIdentifier{
{&testParseAzMonitorMetadata[1], "azure-monitor-test-resource-uri-test-metric"},
{&testParseAzMonitorMetadata[1], 0, "s0-azure-monitor-test-resource-uri-test-metric"},
{&testParseAzMonitorMetadata[1], 1, "s1-azure-monitor-test-resource-uri-test-metric"},
}

func TestAzMonitorParseMetadata(t *testing.T) {
Expand All @@ -97,7 +99,7 @@ func TestAzMonitorParseMetadata(t *testing.T) {

func TestAzMonitorGetMetricSpecForScaling(t *testing.T) {
for _, testData := range azMonitorMetricIdentifiers {
meta, err := parseAzureMonitorMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ResolvedEnv: testData.metadataTestData.resolvedEnv, AuthParams: testData.metadataTestData.authParams, PodIdentity: testData.metadataTestData.podIdentity})
meta, err := parseAzureMonitorMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ResolvedEnv: testData.metadataTestData.resolvedEnv, AuthParams: testData.metadataTestData.authParams, PodIdentity: testData.metadataTestData.podIdentity, Index: testData.scalerIndex})
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/scalers/azure_pipelines_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type azurePipelinesMetadata struct {
personalAccessToken string
poolID string
targetPipelinesQueueLength int
externalMetricName string
}

var azurePipelinesLog = logf.Log.WithName("azure_pipelines_scaler")
Expand Down Expand Up @@ -95,6 +96,11 @@ func parseAzurePipelinesMetadata(config *ScalerConfig) (*azurePipelinesMetadata,
return nil, fmt.Errorf("no poolID given")
}

meta.externalMetricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s-%s", "azure-pipelines-queue", meta.organizationName, meta.poolID))

// Update externalMetricName with the index
meta.externalMetricName = GenerateMetricNameWithIndex(config, meta.externalMetricName)

return &meta, nil
}

Expand Down Expand Up @@ -166,7 +172,7 @@ func (s *azurePipelinesScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec {
targetPipelinesQueueLengthQty := resource.NewQuantity(int64(s.metadata.targetPipelinesQueueLength), resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: kedautil.NormalizeString(fmt.Sprintf("%s-%s-%s", "azure-pipelines-queue", s.metadata.organizationName, s.metadata.poolID)),
Name: s.metadata.externalMetricName,
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
6 changes: 4 additions & 2 deletions pkg/scalers/azure_pipelines_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type parseAzurePipelinesMetadataTestData struct {

type azurePipelinesMetricIdentifier struct {
metadataTestData *parseAzurePipelinesMetadataTestData
scalerIndex int
name string
}

Expand All @@ -38,7 +39,8 @@ var testAzurePipelinesMetadata = []parseAzurePipelinesMetadataTestData{
}

var azurePipelinesMetricIdentifiers = []azurePipelinesMetricIdentifier{
{&testAzurePipelinesMetadata[1], "azure-pipelines-queue-sample-1"},
{&testAzurePipelinesMetadata[1], 0, "s0-azure-pipelines-queue-sample-1"},
{&testAzurePipelinesMetadata[1], 1, "s1-azure-pipelines-queue-sample-1"},
}

func TestParseAzurePipelinesMetadata(t *testing.T) {
Expand All @@ -55,7 +57,7 @@ func TestParseAzurePipelinesMetadata(t *testing.T) {

func TestAzurePipelinesGetMetricSpecForScaling(t *testing.T) {
for _, testData := range azurePipelinesMetricIdentifiers {
meta, err := parseAzurePipelinesMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ResolvedEnv: testData.metadataTestData.resolvedEnv, AuthParams: testData.metadataTestData.authParams})
meta, err := parseAzurePipelinesMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ResolvedEnv: testData.metadataTestData.resolvedEnv, AuthParams: testData.metadataTestData.authParams, Index: testData.scalerIndex})
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
Expand Down
Loading

0 comments on commit cafcbc3

Please sign in to comment.