Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v2] Unique MetricName for each Trigger in ScaledObject #966

Merged
merged 3 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions pkg/scalers/artemis_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ type artemisMonitoring struct {
}

const (
artemisQueueLengthMetricName = "queueLength"
artemisMetricType = "External"
defaultArtemisQueueLength = 10
artemisMetricType = "External"
defaultArtemisQueueLength = 10
)

var artemisLog = logf.Log.WithName("artemis_queue_scaler")
Expand Down Expand Up @@ -184,7 +183,7 @@ func (s *artemisScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec {
targetMetricValue := resource.NewQuantity(int64(s.metadata.queueLength), resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: artemisQueueLengthMetricName,
Name: fmt.Sprintf("%s-%s-%s", "artemis", s.metadata.brokerName, s.metadata.queueName),
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
25 changes: 25 additions & 0 deletions pkg/scalers/artemis_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ type parseArtemisMetadataTestData struct {
isError bool
}

type artemisMetricIdentifier struct {
metadataTestData *parseArtemisMetadataTestData
name string
}

var sampleArtemisResolvedEnv = map[string]string{
username: "artemis",
password: "artemis",
Expand Down Expand Up @@ -49,6 +54,10 @@ var testArtemisMetadata = []parseArtemisMetadataTestData{
{map[string]string{"managementEndpoint": "localhost:8161", "queueName": "queue1", "brokerName": "broker-activemq", "brokerAddress": "test", "username": "myUserName", "password": "myPassword"}, false},
}

var artemisMetricIdentifiers = []artemisMetricIdentifier{
{&testArtemisMetadata[7], "artemis-broker-activemq-queue1"},
}

var testArtemisMetadataWithEmptyAuthParams = []parseArtemisMetadataTestData{
// nothing passed
{map[string]string{}, true},
Expand Down Expand Up @@ -101,3 +110,19 @@ func TestArtemisParseMetadata(t *testing.T) {
}
}
}

func TestArtemisGetMetricSpecForScaling(t *testing.T) {
for _, testData := range artemisMetricIdentifiers {
meta, err := parseArtemisMetadata(sampleArtemisResolvedEnv, testData.metadataTestData.metadata, nil)
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
mockArtemisScaler := artemisScaler{meta}

metricSpec := mockArtemisScaler.GetMetricSpecForScaling()
metricName := metricSpec[0].External.Metric.Name
if metricName != testData.name {
t.Error("Wrong External metric source name:", metricName)
}
}
}
3 changes: 1 addition & 2 deletions pkg/scalers/aws_cloudwatch_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ func (c *awsCloudwatchScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec {
targetMetricValue := resource.NewQuantity(int64(c.metadata.targetMetricValue), resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: fmt.Sprintf("%s-%s-%s", strings.ReplaceAll(c.metadata.namespace, "/", "-"),
c.metadata.dimensionName, c.metadata.dimensionValue),
Name: fmt.Sprintf("%s-%s-%s-%s", "aws-cloudwatch", strings.ReplaceAll(c.metadata.namespace, "/", "-"), c.metadata.dimensionName, c.metadata.dimensionValue),
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
25 changes: 25 additions & 0 deletions pkg/scalers/aws_cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ type parseAWSCloudwatchMetadataTestData struct {
comment string
}

type awsCloudwatchMetricIdentifier struct {
metadataTestData *parseAWSCloudwatchMetadataTestData
name string
}

var testAWSCloudwatchMetadata = []parseAWSCloudwatchMetadataTestData{
{map[string]string{}, testAWSAuthentication, true, "Empty structures"},
// properly formed cloudwatch query and awsRegion
Expand Down Expand Up @@ -161,6 +166,10 @@ var testAWSCloudwatchMetadata = []parseAWSCloudwatchMetadataTestData{
"with AWS Role assigned on KEDA operator itself"},
}

var awsCloudwatchMetricIdentifiers = []awsCloudwatchMetricIdentifier{
{&testAWSCloudwatchMetadata[1], "aws-cloudwatch-AWS-SQS-QueueName-keda"},
}

func TestCloudwatchParseMetadata(t *testing.T) {
for _, testData := range testAWSCloudwatchMetadata {
_, err := parseAwsCloudwatchMetadata(testData.metadata, testAWSCloudwatchResolvedEnv, testData.authParams)
Expand All @@ -172,3 +181,19 @@ func TestCloudwatchParseMetadata(t *testing.T) {
}
}
}

func TestAWSCloudwatchGetMetricSpecForScaling(t *testing.T) {
for _, testData := range awsCloudwatchMetricIdentifiers {
meta, err := parseAwsCloudwatchMetadata(testData.metadataTestData.metadata, testAWSCloudwatchResolvedEnv, testData.metadataTestData.authParams)
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
mockAWSCloudwatchScaler := awsCloudwatchScaler{meta}

metricSpec := mockAWSCloudwatchScaler.GetMetricSpecForScaling()
metricName := metricSpec[0].External.Metric.Name
if metricName != testData.name {
t.Error("Wrong External metric source name:", metricName)
}
}
}
6 changes: 3 additions & 3 deletions pkg/scalers/aws_kinesis_stream_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
)

const (
awsKinesisStreamMetricName = "ShardCount"
targetShardCountDefault = 2
// awsKinesisStreamMetricName = "ShardCount"
zroubalik marked this conversation as resolved.
Show resolved Hide resolved
targetShardCountDefault = 2
)

type awsKinesisStreamScaler struct {
Expand Down Expand Up @@ -104,7 +104,7 @@ func (s *awsKinesisStreamScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec
targetShardCountQty := resource.NewQuantity(int64(s.metadata.targetShardCount), resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: fmt.Sprintf("%s-%s-%s", "AWS-Kinesis-Stream", awsKinesisStreamMetricName, s.metadata.streamName),
Name: fmt.Sprintf("%s-%s", "AWS-Kinesis-Stream", s.metadata.streamName),
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
25 changes: 25 additions & 0 deletions pkg/scalers/aws_kinesis_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type parseAWSKinesisMetadataTestData struct {
comment string
}

type awsKinesisMetricIdentifier struct {
metadataTestData *parseAWSKinesisMetadataTestData
name string
}

var testAWSKinesisMetadata = []parseAWSKinesisMetadataTestData{
{
metadata: map[string]string{},
Expand Down Expand Up @@ -170,6 +175,10 @@ var testAWSKinesisMetadata = []parseAWSKinesisMetadataTestData{
comment: "with AWS Role assigned on KEDA operator itself"},
}

var awsKinesisMetricIdentifiers = []awsKinesisMetricIdentifier{
{&testAWSKinesisMetadata[1], "AWS-Kinesis-Stream-test"},
}

func TestKinesisParseMetadata(t *testing.T) {
for _, testData := range testAWSKinesisMetadata {
result, err := parseAwsKinesisStreamMetadata(testData.metadata, testAWSKinesisAuthentication, testData.authParams)
Expand All @@ -185,3 +194,19 @@ func TestKinesisParseMetadata(t *testing.T) {
}
}
}

func TestAWSKinesisGetMetricSpecForScaling(t *testing.T) {
for _, testData := range awsKinesisMetricIdentifiers {
meta, err := parseAwsKinesisStreamMetadata(testData.metadataTestData.metadata, testAWSKinesisAuthentication, testData.metadataTestData.authParams)
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
mockAWSKinesisStreamScaler := awsKinesisStreamScaler{meta}

metricSpec := mockAWSKinesisStreamScaler.GetMetricSpecForScaling()
metricName := metricSpec[0].External.Metric.Name
if metricName != testData.name {
t.Error("Wrong External metric source name:", metricName)
}
}
}
2 changes: 1 addition & 1 deletion pkg/scalers/aws_sqs_queue_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s *awsSqsQueueScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec {
targetQueueLengthQty := resource.NewQuantity(int64(s.metadata.targetQueueLength), resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: fmt.Sprintf("%s-%s-%s", "AWS-SQS-Queue", awsSqsQueueMetricName, s.metadata.queueName),
Name: fmt.Sprintf("%s-%s", "AWS-SQS-Queue", s.metadata.queueName),
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
25 changes: 25 additions & 0 deletions pkg/scalers/aws_sqs_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type parseAWSSQSMetadataTestData struct {
comment string
}

type awsSQSMetricIdentifier struct {
metadataTestData *parseAWSSQSMetadataTestData
name string
}

var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
{map[string]string{},
testAWSSQSAuthentication,
Expand Down Expand Up @@ -130,6 +135,10 @@ var testAWSSQSMetadata = []parseAWSSQSMetadataTestData{
"with AWS Role assigned on KEDA operator itself"},
}

var awsSQSMetricIdentifiers = []awsSQSMetricIdentifier{
{&testAWSSQSMetadata[1], "AWS-SQS-Queue-DeleteArtifactQ"},
}

func TestSQSParseMetadata(t *testing.T) {
for _, testData := range testAWSSQSMetadata {
_, err := parseAwsSqsQueueMetadata(testData.metadata, testAWSSQSAuthentication, testData.authParams)
Expand All @@ -141,3 +150,19 @@ func TestSQSParseMetadata(t *testing.T) {
}
}
}

func TestAWSSQSGetMetricSpecForScaling(t *testing.T) {
for _, testData := range awsSQSMetricIdentifiers {
meta, err := parseAwsSqsQueueMetadata(testData.metadataTestData.metadata, testAWSSQSAuthentication, testData.metadataTestData.authParams)
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
mockAWSSQSScaler := awsSqsQueueScaler{meta}

metricSpec := mockAWSSQSScaler.GetMetricSpecForScaling()
metricName := metricSpec[0].External.Metric.Name
if metricName != testData.name {
t.Error("Wrong External metric source name:", metricName)
}
}
}
2 changes: 1 addition & 1 deletion pkg/scalers/azure_blob_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (s *azureBlobScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec {
targetBlobCount := resource.NewQuantity(int64(s.metadata.targetBlobCount), resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: blobCountMetricName,
Name: fmt.Sprintf("%s-%s", "azure-blob", s.metadata.blobContainerName),
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
26 changes: 26 additions & 0 deletions pkg/scalers/azure_blob_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ type parseAzBlobMetadataTestData struct {
podIdentity string
}

type azBlobMetricIdentifier struct {
metadataTestData *parseAzBlobMetadataTestData
name string
}

var testAzBlobMetadata = []parseAzBlobMetadataTestData{
// nothing passed
{map[string]string{}, true, testAzBlobResolvedEnv, map[string]string{}, ""},
Expand All @@ -33,6 +38,11 @@ var testAzBlobMetadata = []parseAzBlobMetadataTestData{
{map[string]string{"blobContainerName": "sample_container", "blobCount": "5"}, false, testAzBlobResolvedEnv, map[string]string{"connection": "value"}, "none"},
}

var azBlobMetricIdentifiers = []azBlobMetricIdentifier{
{&testAzBlobMetadata[1], "azure-blob-sample"},
{&testAzBlobMetadata[4], "azure-blob-sample_container"},
}

func TestAzBlobParseMetadata(t *testing.T) {
for _, testData := range testAzBlobMetadata {
_, podIdentity, err := parseAzureBlobMetadata(testData.metadata, testData.resolvedEnv, testData.authParams, testData.podIdentity)
Expand All @@ -48,3 +58,19 @@ func TestAzBlobParseMetadata(t *testing.T) {
}
}
}

func TestAzBlobGetMetricSpecForScaling(t *testing.T) {
for _, testData := range azBlobMetricIdentifiers {
meta, podIdentity, err := parseAzureBlobMetadata(testData.metadataTestData.metadata, testData.metadataTestData.resolvedEnv, testData.metadataTestData.authParams, testData.metadataTestData.podIdentity)
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
mockAzBlobScaler := azureBlobScaler{meta, podIdentity}

metricSpec := mockAzBlobScaler.GetMetricSpecForScaling()
metricName := metricSpec[0].External.Metric.Name
if metricName != testData.name {
t.Error("Wrong External metric source name:", metricName)
}
}
}
2 changes: 1 addition & 1 deletion pkg/scalers/azure_eventhub_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (scaler *AzureEventHubScaler) GetMetricSpecForScaling() []v2beta2.MetricSpe
targetMetricVal := resource.NewQuantity(scaler.metadata.threshold, resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: thresholdMetricName,
Name: fmt.Sprintf("%s-%s-%s", "azure-eventhub", scaler.metadata.eventHubInfo.EventHubConnection, scaler.metadata.eventHubInfo.EventHubConsumerGroup),
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
25 changes: 25 additions & 0 deletions pkg/scalers/azure_eventhub_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type parseEventHubMetadataTestData struct {
isError bool
}

type eventHubMetricIdentifier struct {
metadataTestData *parseEventHubMetadataTestData
name string
}

type resolvedEnvTestData struct {
resolvedEnv map[string]string
isError bool
Expand All @@ -51,6 +56,10 @@ var parseEventHubMetadataDataset = []parseEventHubMetadataTestData{
{map[string]string{"storageConnection": storageConnectionSetting, "consumerGroup": eventHubConsumerGroup, "connection": eventHubConnectionSetting, "blobContainer": testContainerName}, false},
}

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

var testEventHubScaler = AzureEventHubScaler{
metadata: &EventHubMetadata{
eventHubInfo: azure.EventHubInfo{
Expand Down Expand Up @@ -407,3 +416,19 @@ func DeleteContainerInStorage(ctx context.Context, endpoint *url.URL, credential
}
return nil
}

func TestEventHubGetMetricSpecForScaling(t *testing.T) {
for _, testData := range eventHubMetricIdentifiers {
meta, err := parseAzureEventHubMetadata(testData.metadataTestData.metadata, sampleEventHubResolvedEnv)
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
mockEventHubScaler := AzureEventHubScaler{meta, nil}

metricSpec := mockEventHubScaler.GetMetricSpecForScaling()
metricName := metricSpec[0].External.Metric.Name
if metricName != testData.name {
t.Error("Wrong External metric source name:", metricName)
}
}
}
2 changes: 1 addition & 1 deletion pkg/scalers/azure_monitor_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (s *azureMonitorScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec {
targetMetricVal := resource.NewQuantity(int64(s.metadata.targetValue), resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: azureMonitorMetricName,
Name: fmt.Sprintf("%s-%s-%s-%s", "azure-monitor", s.metadata.azureMonitorInfo.ResourceURI, s.metadata.azureMonitorInfo.ResourceGroupName, s.metadata.azureMonitorInfo.Name),
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
25 changes: 25 additions & 0 deletions pkg/scalers/azure_monitor_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ type parseAzMonitorMetadataTestData struct {
authParams map[string]string
}

type azMonitorMetricIdentifier struct {
metadataTestData *parseAzMonitorMetadataTestData
name string
}

var testAzMonitorResolvedEnv = map[string]string{
"CLIENT_ID": "xxx",
"CLIENT_PASSWORD": "yyy",
Expand Down Expand Up @@ -49,6 +54,10 @@ var testParseAzMonitorMetadata = []parseAzMonitorMetadataTestData{
{map[string]string{"resourceURI": "test/resource/uri", "tenantId": "123", "subscriptionId": "456", "resourceGroupName": "test", "metricName": "metric", "metricAggregationInterval": "0:15:0", "metricAggregationType": "Average", "targetValue": "5"}, false, map[string]string{}, map[string]string{"activeDirectoryClientId": "zzz", "activeDirectoryClientPassword": "password"}},
}

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

func TestAzMonitorParseMetadata(t *testing.T) {
for _, testData := range testParseAzMonitorMetadata {
_, err := parseAzureMonitorMetadata(testData.metadata, testData.resolvedEnv, testData.authParams)
Expand All @@ -60,3 +69,19 @@ func TestAzMonitorParseMetadata(t *testing.T) {
}
}
}

func TestAzMonitorGetMetricSpecForScaling(t *testing.T) {
for _, testData := range azMonitorMetricIdentifiers {
meta, err := parseAzureMonitorMetadata(testData.metadataTestData.metadata, testData.metadataTestData.resolvedEnv, testData.metadataTestData.authParams)
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
mockAzMonitorScaler := azureMonitorScaler{meta}

metricSpec := mockAzMonitorScaler.GetMetricSpecForScaling()
metricName := metricSpec[0].External.Metric.Name
if metricName != testData.name {
t.Error("Wrong External metric source name:", metricName)
}
}
}
2 changes: 1 addition & 1 deletion pkg/scalers/azure_queue_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (s *azureQueueScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec {
targetQueueLengthQty := resource.NewQuantity(int64(s.metadata.targetQueueLength), resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: queueLengthMetricName,
Name: fmt.Sprintf("%s-%s", "azure-queue", s.metadata.queueName),
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
Loading