@@ -19,7 +19,6 @@ package scalers
19
19
import (
20
20
"context"
21
21
"fmt"
22
- "strconv"
23
22
"strings"
24
23
25
24
"github.com/Azure/azure-sdk-for-go/sdk/storage/azqueue"
@@ -34,37 +33,34 @@ import (
34
33
)
35
34
36
35
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"
43
38
)
44
39
45
- var (
46
- maxPeekMessages int32 = 32
47
- )
40
+ var maxPeekMessages int32 = 32
48
41
49
42
type azureQueueScaler struct {
50
43
metricType v2.MetricTargetType
51
- metadata * azureQueueMetadata
44
+ metadata azureQueueMetadata
52
45
queueClient * azqueue.QueueClient
53
46
logger logr.Logger
54
47
}
55
48
56
49
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
58
+ }
59
+
60
+ func (m * azureQueueMetadata ) Validate () error {
61
+ return nil
65
62
}
66
63
67
- // NewAzureQueueScaler creates a new scaler for queue
68
64
func NewAzureQueueScaler (config * scalersconfig.ScalerConfig ) (Scaler , error ) {
69
65
metricType , err := GetMetricTargetType (config )
70
66
if err != nil {
@@ -73,14 +69,14 @@ func NewAzureQueueScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {
73
69
74
70
logger := InitializeLogger (config , "azure_queue_scaler" )
75
71
76
- meta , podIdentity , err := parseAzureQueueMetadata (config , logger )
72
+ meta , podIdentity , err := parseAzureQueueMetadata (config )
77
73
if err != nil {
78
74
return nil , fmt .Errorf ("error parsing azure queue metadata: %w" , err )
79
75
}
80
76
81
- queueClient , err := azure .GetStorageQueueClient (logger , podIdentity , meta .connection , meta .accountName , meta .endpointSuffix , meta .queueName , config .GlobalHTTPTimeout )
77
+ queueClient , err := azure .GetStorageQueueClient (logger , podIdentity , meta .Connection , meta .AccountName , meta .EndpointSuffix , meta .QueueName , config .GlobalHTTPTimeout )
82
78
if err != nil {
83
- return nil , fmt .Errorf ("error creating azure blob client: %w" , err )
79
+ return nil , fmt .Errorf ("error creating azure queue client: %w" , err )
84
80
}
85
81
86
82
return & azureQueueScaler {
@@ -91,105 +87,63 @@ func NewAzureQueueScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {
91
87
}, nil
92
88
}
93
89
94
- func parseAzureQueueMetadata (config * scalersconfig.ScalerConfig , logger logr. Logger ) (* azureQueueMetadata , kedav1alpha1.AuthPodIdentity , error ) {
90
+ func parseAzureQueueMetadata (config * scalersconfig.ScalerConfig ) (azureQueueMetadata , kedav1alpha1.AuthPodIdentity , error ) {
95
91
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
92
+ err := config .TypedConfig (& meta )
93
+ if err != nil {
94
+ return meta , kedav1alpha1.AuthPodIdentity {}, fmt .Errorf ("error parsing azure queue metadata: %w" , err )
107
95
}
108
96
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
97
+ err = meta .Validate ()
98
+ if err != nil {
99
+ return meta , kedav1alpha1.AuthPodIdentity {}, err
119
100
}
120
101
121
102
endpointSuffix , err := azure .ParseAzureStorageEndpointSuffix (config .TriggerMetadata , azure .QueueEndpoint )
122
103
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
104
+ return meta , kedav1alpha1.AuthPodIdentity {}, err
143
105
}
106
+ meta .EndpointSuffix = endpointSuffix
144
107
145
108
// If the Use AAD Pod Identity is not present, or set to "none"
146
109
// then check for connection string
147
110
switch config .PodIdentity .Provider {
148
111
case "" , kedav1alpha1 .PodIdentityProviderNone :
149
112
// Azure Queue Scaler expects a "connection" parameter in the metadata
150
113
// 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" )
114
+ if meta .Connection == "" {
115
+ return meta , kedav1alpha1.AuthPodIdentity {}, fmt .Errorf ("no connection setting given" )
160
116
}
161
117
case kedav1alpha1 .PodIdentityProviderAzureWorkload :
162
118
// 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" )
119
+ if meta .AccountName == "" {
120
+ return meta , kedav1alpha1.AuthPodIdentity {}, fmt .Errorf ("no accountName given" )
167
121
}
168
122
default :
169
- return nil , kedav1alpha1.AuthPodIdentity {}, fmt .Errorf ("pod identity %s not supported for azure storage queues" , config .PodIdentity .Provider )
123
+ return meta , kedav1alpha1.AuthPodIdentity {}, fmt .Errorf ("pod identity %s not supported for azure storage queues" , config .PodIdentity .Provider )
170
124
}
171
125
172
- meta .triggerIndex = config .TriggerIndex
173
-
174
- return & meta , config .PodIdentity , nil
126
+ meta .TriggerIndex = config .TriggerIndex
127
+ return meta , config .PodIdentity , nil
175
128
}
176
129
177
130
func (s * azureQueueScaler ) Close (context.Context ) error {
178
131
return nil
179
132
}
180
133
134
+ // GetMetricsAndActivity returns value for a supported metric and an error if there is a problem getting the metric
181
135
func (s * azureQueueScaler ) GetMetricSpecForScaling (context.Context ) []v2.MetricSpec {
136
+ metricName := kedautil .NormalizeString (fmt .Sprintf ("azure-queue-%s" , s .metadata .QueueName ))
182
137
externalMetric := & v2.ExternalMetricSource {
183
138
Metric : v2.MetricIdentifier {
184
- Name : GenerateMetricNameWithIndex (s .metadata .triggerIndex , kedautil . NormalizeString ( fmt . Sprintf ( "azure-queue-%s" , s . metadata . queueName )) ),
139
+ Name : GenerateMetricNameWithIndex (s .metadata .TriggerIndex , metricName ),
185
140
},
186
- Target : GetMetricTarget (s .metricType , s .metadata .targetQueueLength ),
141
+ Target : GetMetricTarget (s .metricType , s .metadata .QueueLength ),
187
142
}
188
143
metricSpec := v2.MetricSpec {External : externalMetric , Type : externalMetricType }
189
144
return []v2.MetricSpec {metricSpec }
190
145
}
191
146
192
- // GetMetricsAndActivity returns value for a supported metric and an error if there is a problem getting the metric
193
147
func (s * azureQueueScaler ) GetMetricsAndActivity (ctx context.Context , metricName string ) ([]external_metrics.ExternalMetricValue , bool , error ) {
194
148
queuelen , err := s .getMessageCount (ctx )
195
149
if err != nil {
@@ -198,12 +152,11 @@ func (s *azureQueueScaler) GetMetricsAndActivity(ctx context.Context, metricName
198
152
}
199
153
200
154
metric := GenerateMetricInMili (metricName , float64 (queuelen ))
201
- return []external_metrics.ExternalMetricValue {metric }, queuelen > s .metadata .activationTargetQueueLength , nil
155
+ return []external_metrics.ExternalMetricValue {metric }, queuelen > s .metadata .ActivationQueueLength , nil
202
156
}
203
157
204
158
func (s * azureQueueScaler ) getMessageCount (ctx context.Context ) (int64 , error ) {
205
- strategy := strings .ToLower (s .metadata .queueLengthStrategy )
206
- if strategy == QueueLengthStrategyVisibleOnly {
159
+ if strings .ToLower (s .metadata .QueueLengthStrategy ) == queueLengthStrategyVisibleOnly {
207
160
queue , err := s .queueClient .PeekMessages (ctx , & azqueue.PeekMessagesOptions {NumberOfMessages : & maxPeekMessages })
208
161
if err != nil {
209
162
return 0 , err
0 commit comments