Skip to content

Commit 54c315d

Browse files
Refactor azure queue scaler (#6269)
* Refactor azure queue scaler Signed-off-by: rickbrouwer <[email protected]> * update after feedback Signed-off-by: Rick Brouwer <[email protected]> --------- Signed-off-by: rickbrouwer <[email protected]> Signed-off-by: Rick Brouwer <[email protected]> Co-authored-by: Jorge Turrado Ferrero <[email protected]>
1 parent c1a1b3d commit 54c315d

File tree

3 files changed

+296
-149
lines changed

3 files changed

+296
-149
lines changed

pkg/scalers/aws_sqs_queue_scaler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
)
2121

2222
const (
23+
defaultTargetQueueLength = 5
2324
targetQueueLengthDefault = 5
2425
activationTargetQueueLengthDefault = 0
2526
defaultScaleOnInFlight = true

pkg/scalers/azure_queue_scaler.go

Lines changed: 34 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package scalers
1919
import (
2020
"context"
2121
"fmt"
22-
"strconv"
2322
"strings"
2423

2524
"github.com/Azure/azure-sdk-for-go/sdk/storage/azqueue"
@@ -34,37 +33,30 @@ import (
3433
)
3534

3635
const (
37-
queueLengthMetricName = "queueLength"
38-
activationQueueLengthMetricName = "activationQueueLength"
39-
defaultTargetQueueLength = 5
40-
externalMetricType = "External"
41-
QueueLengthStrategyAll string = "all"
42-
QueueLengthStrategyVisibleOnly string = "visibleonly"
36+
externalMetricType = "External"
37+
queueLengthStrategyVisibleOnly = "visibleonly"
4338
)
4439

45-
var (
46-
maxPeekMessages int32 = 32
47-
)
40+
var maxPeekMessages int32 = 32
4841

4942
type azureQueueScaler struct {
5043
metricType v2.MetricTargetType
51-
metadata *azureQueueMetadata
44+
metadata azureQueueMetadata
5245
queueClient *azqueue.QueueClient
5346
logger logr.Logger
5447
}
5548

5649
type azureQueueMetadata struct {
57-
targetQueueLength int64
58-
activationTargetQueueLength int64
59-
queueName string
60-
connection string
61-
accountName string
62-
endpointSuffix string
63-
queueLengthStrategy string
64-
triggerIndex int
50+
ActivationQueueLength int64 `keda:"name=activationQueueLength, order=triggerMetadata, default=0"`
51+
QueueName string `keda:"name=queueName, order=triggerMetadata"`
52+
QueueLength int64 `keda:"name=queueLength, order=triggerMetadata, default=5"`
53+
Connection string `keda:"name=connection, order=authParams;triggerMetadata;resolvedEnv, optional"`
54+
AccountName string `keda:"name=accountName, order=triggerMetadata, optional"`
55+
EndpointSuffix string `keda:"name=endpointSuffix, order=triggerMetadata, optional"`
56+
QueueLengthStrategy string `keda:"name=queueLengthStrategy, order=triggerMetadata, enum=all;visibleonly, default=all"`
57+
TriggerIndex int
6558
}
6659

67-
// NewAzureQueueScaler creates a new scaler for queue
6860
func NewAzureQueueScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {
6961
metricType, err := GetMetricTargetType(config)
7062
if err != nil {
@@ -73,14 +65,14 @@ func NewAzureQueueScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {
7365

7466
logger := InitializeLogger(config, "azure_queue_scaler")
7567

76-
meta, podIdentity, err := parseAzureQueueMetadata(config, logger)
68+
meta, podIdentity, err := parseAzureQueueMetadata(config)
7769
if err != nil {
7870
return nil, fmt.Errorf("error parsing azure queue metadata: %w", err)
7971
}
8072

81-
queueClient, err := azure.GetStorageQueueClient(logger, podIdentity, meta.connection, meta.accountName, meta.endpointSuffix, meta.queueName, config.GlobalHTTPTimeout)
73+
queueClient, err := azure.GetStorageQueueClient(logger, podIdentity, meta.Connection, meta.AccountName, meta.EndpointSuffix, meta.QueueName, config.GlobalHTTPTimeout)
8274
if err != nil {
83-
return nil, fmt.Errorf("error creating azure blob client: %w", err)
75+
return nil, fmt.Errorf("error creating azure queue client: %w", err)
8476
}
8577

8678
return &azureQueueScaler{
@@ -91,105 +83,58 @@ func NewAzureQueueScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {
9183
}, nil
9284
}
9385

94-
func parseAzureQueueMetadata(config *scalersconfig.ScalerConfig, logger logr.Logger) (*azureQueueMetadata, kedav1alpha1.AuthPodIdentity, error) {
86+
func parseAzureQueueMetadata(config *scalersconfig.ScalerConfig) (azureQueueMetadata, kedav1alpha1.AuthPodIdentity, error) {
9587
meta := azureQueueMetadata{}
96-
meta.targetQueueLength = defaultTargetQueueLength
97-
98-
if val, ok := config.TriggerMetadata[queueLengthMetricName]; ok {
99-
queueLength, err := strconv.ParseInt(val, 10, 64)
100-
if err != nil {
101-
logger.Error(err, "Error parsing azure queue metadata", "queueLengthMetricName", queueLengthMetricName)
102-
return nil, kedav1alpha1.AuthPodIdentity{},
103-
fmt.Errorf("error parsing azure queue metadata %s: %w", queueLengthMetricName, err)
104-
}
105-
106-
meta.targetQueueLength = queueLength
107-
}
108-
109-
meta.activationTargetQueueLength = 0
110-
if val, ok := config.TriggerMetadata[activationQueueLengthMetricName]; ok {
111-
activationQueueLength, err := strconv.ParseInt(val, 10, 64)
112-
if err != nil {
113-
logger.Error(err, "Error parsing azure queue metadata", activationQueueLengthMetricName, activationQueueLengthMetricName)
114-
return nil, kedav1alpha1.AuthPodIdentity{},
115-
fmt.Errorf("error parsing azure queue metadata %s: %w", activationQueueLengthMetricName, err)
116-
}
117-
118-
meta.activationTargetQueueLength = activationQueueLength
88+
err := config.TypedConfig(&meta)
89+
if err != nil {
90+
return meta, kedav1alpha1.AuthPodIdentity{}, fmt.Errorf("error parsing azure queue metadata: %w", err)
11991
}
12092

12193
endpointSuffix, err := azure.ParseAzureStorageEndpointSuffix(config.TriggerMetadata, azure.QueueEndpoint)
12294
if err != nil {
123-
return nil, kedav1alpha1.AuthPodIdentity{}, err
124-
}
125-
126-
meta.endpointSuffix = endpointSuffix
127-
128-
if val, ok := config.TriggerMetadata["queueName"]; ok && val != "" {
129-
meta.queueName = val
130-
} else {
131-
return nil, kedav1alpha1.AuthPodIdentity{}, fmt.Errorf("no queueName given")
132-
}
133-
134-
if val, ok := config.TriggerMetadata["queueLengthStrategy"]; ok && val != "" {
135-
strategy := strings.ToLower(val)
136-
if strategy == QueueLengthStrategyAll || strategy == QueueLengthStrategyVisibleOnly {
137-
meta.queueLengthStrategy = strategy
138-
} else {
139-
return nil, kedav1alpha1.AuthPodIdentity{}, fmt.Errorf("invalid queueLengthStrategy %s given", val)
140-
}
141-
} else {
142-
meta.queueLengthStrategy = QueueLengthStrategyAll
95+
return meta, kedav1alpha1.AuthPodIdentity{}, err
14396
}
97+
meta.EndpointSuffix = endpointSuffix
14498

14599
// If the Use AAD Pod Identity is not present, or set to "none"
146100
// then check for connection string
147101
switch config.PodIdentity.Provider {
148102
case "", kedav1alpha1.PodIdentityProviderNone:
149103
// Azure Queue Scaler expects a "connection" parameter in the metadata
150104
// of the scaler or in a TriggerAuthentication object
151-
if config.AuthParams["connection"] != "" {
152-
// Found the connection in a parameter from TriggerAuthentication
153-
meta.connection = config.AuthParams["connection"]
154-
} else if config.TriggerMetadata["connectionFromEnv"] != "" {
155-
meta.connection = config.ResolvedEnv[config.TriggerMetadata["connectionFromEnv"]]
156-
}
157-
158-
if len(meta.connection) == 0 {
159-
return nil, kedav1alpha1.AuthPodIdentity{}, fmt.Errorf("no connection setting given")
105+
if meta.Connection == "" {
106+
return meta, kedav1alpha1.AuthPodIdentity{}, fmt.Errorf("no connection setting given")
160107
}
161108
case kedav1alpha1.PodIdentityProviderAzureWorkload:
162109
// If the Use AAD Pod Identity is present then check account name
163-
if val, ok := config.TriggerMetadata["accountName"]; ok && val != "" {
164-
meta.accountName = val
165-
} else {
166-
return nil, kedav1alpha1.AuthPodIdentity{}, fmt.Errorf("no accountName given")
110+
if meta.AccountName == "" {
111+
return meta, kedav1alpha1.AuthPodIdentity{}, fmt.Errorf("no accountName given")
167112
}
168113
default:
169-
return nil, kedav1alpha1.AuthPodIdentity{}, fmt.Errorf("pod identity %s not supported for azure storage queues", config.PodIdentity.Provider)
114+
return meta, kedav1alpha1.AuthPodIdentity{}, fmt.Errorf("pod identity %s not supported for azure storage queues", config.PodIdentity.Provider)
170115
}
171116

172-
meta.triggerIndex = config.TriggerIndex
173-
174-
return &meta, config.PodIdentity, nil
117+
meta.TriggerIndex = config.TriggerIndex
118+
return meta, config.PodIdentity, nil
175119
}
176120

177121
func (s *azureQueueScaler) Close(context.Context) error {
178122
return nil
179123
}
180124

125+
// GetMetricsAndActivity returns value for a supported metric and an error if there is a problem getting the metric
181126
func (s *azureQueueScaler) GetMetricSpecForScaling(context.Context) []v2.MetricSpec {
127+
metricName := kedautil.NormalizeString(fmt.Sprintf("azure-queue-%s", s.metadata.QueueName))
182128
externalMetric := &v2.ExternalMetricSource{
183129
Metric: v2.MetricIdentifier{
184-
Name: GenerateMetricNameWithIndex(s.metadata.triggerIndex, kedautil.NormalizeString(fmt.Sprintf("azure-queue-%s", s.metadata.queueName))),
130+
Name: GenerateMetricNameWithIndex(s.metadata.TriggerIndex, metricName),
185131
},
186-
Target: GetMetricTarget(s.metricType, s.metadata.targetQueueLength),
132+
Target: GetMetricTarget(s.metricType, s.metadata.QueueLength),
187133
}
188134
metricSpec := v2.MetricSpec{External: externalMetric, Type: externalMetricType}
189135
return []v2.MetricSpec{metricSpec}
190136
}
191137

192-
// GetMetricsAndActivity returns value for a supported metric and an error if there is a problem getting the metric
193138
func (s *azureQueueScaler) GetMetricsAndActivity(ctx context.Context, metricName string) ([]external_metrics.ExternalMetricValue, bool, error) {
194139
queuelen, err := s.getMessageCount(ctx)
195140
if err != nil {
@@ -198,12 +143,11 @@ func (s *azureQueueScaler) GetMetricsAndActivity(ctx context.Context, metricName
198143
}
199144

200145
metric := GenerateMetricInMili(metricName, float64(queuelen))
201-
return []external_metrics.ExternalMetricValue{metric}, queuelen > s.metadata.activationTargetQueueLength, nil
146+
return []external_metrics.ExternalMetricValue{metric}, queuelen > s.metadata.ActivationQueueLength, nil
202147
}
203148

204149
func (s *azureQueueScaler) getMessageCount(ctx context.Context) (int64, error) {
205-
strategy := strings.ToLower(s.metadata.queueLengthStrategy)
206-
if strategy == QueueLengthStrategyVisibleOnly {
150+
if strings.ToLower(s.metadata.QueueLengthStrategy) == queueLengthStrategyVisibleOnly {
207151
queue, err := s.queueClient.PeekMessages(ctx, &azqueue.PeekMessagesOptions{NumberOfMessages: &maxPeekMessages})
208152
if err != nil {
209153
return 0, err

0 commit comments

Comments
 (0)