@@ -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,30 @@ 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
65
58
}
66
59
67
- // NewAzureQueueScaler creates a new scaler for queue
68
60
func NewAzureQueueScaler (config * scalersconfig.ScalerConfig ) (Scaler , error ) {
69
61
metricType , err := GetMetricTargetType (config )
70
62
if err != nil {
@@ -73,14 +65,14 @@ func NewAzureQueueScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {
73
65
74
66
logger := InitializeLogger (config , "azure_queue_scaler" )
75
67
76
- meta , podIdentity , err := parseAzureQueueMetadata (config , logger )
68
+ meta , podIdentity , err := parseAzureQueueMetadata (config )
77
69
if err != nil {
78
70
return nil , fmt .Errorf ("error parsing azure queue metadata: %w" , err )
79
71
}
80
72
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 )
82
74
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 )
84
76
}
85
77
86
78
return & azureQueueScaler {
@@ -91,105 +83,58 @@ func NewAzureQueueScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {
91
83
}, nil
92
84
}
93
85
94
- func parseAzureQueueMetadata (config * scalersconfig.ScalerConfig , logger logr. Logger ) (* azureQueueMetadata , kedav1alpha1.AuthPodIdentity , error ) {
86
+ func parseAzureQueueMetadata (config * scalersconfig.ScalerConfig ) (azureQueueMetadata , kedav1alpha1.AuthPodIdentity , error ) {
95
87
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 )
119
91
}
120
92
121
93
endpointSuffix , err := azure .ParseAzureStorageEndpointSuffix (config .TriggerMetadata , azure .QueueEndpoint )
122
94
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
143
96
}
97
+ meta .EndpointSuffix = endpointSuffix
144
98
145
99
// If the Use AAD Pod Identity is not present, or set to "none"
146
100
// then check for connection string
147
101
switch config .PodIdentity .Provider {
148
102
case "" , kedav1alpha1 .PodIdentityProviderNone :
149
103
// Azure Queue Scaler expects a "connection" parameter in the metadata
150
104
// 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" )
160
107
}
161
108
case kedav1alpha1 .PodIdentityProviderAzureWorkload :
162
109
// 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" )
167
112
}
168
113
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 )
170
115
}
171
116
172
- meta .triggerIndex = config .TriggerIndex
173
-
174
- return & meta , config .PodIdentity , nil
117
+ meta .TriggerIndex = config .TriggerIndex
118
+ return meta , config .PodIdentity , nil
175
119
}
176
120
177
121
func (s * azureQueueScaler ) Close (context.Context ) error {
178
122
return nil
179
123
}
180
124
125
+ // GetMetricsAndActivity returns value for a supported metric and an error if there is a problem getting the metric
181
126
func (s * azureQueueScaler ) GetMetricSpecForScaling (context.Context ) []v2.MetricSpec {
127
+ metricName := kedautil .NormalizeString (fmt .Sprintf ("azure-queue-%s" , s .metadata .QueueName ))
182
128
externalMetric := & v2.ExternalMetricSource {
183
129
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 ),
185
131
},
186
- Target : GetMetricTarget (s .metricType , s .metadata .targetQueueLength ),
132
+ Target : GetMetricTarget (s .metricType , s .metadata .QueueLength ),
187
133
}
188
134
metricSpec := v2.MetricSpec {External : externalMetric , Type : externalMetricType }
189
135
return []v2.MetricSpec {metricSpec }
190
136
}
191
137
192
- // GetMetricsAndActivity returns value for a supported metric and an error if there is a problem getting the metric
193
138
func (s * azureQueueScaler ) GetMetricsAndActivity (ctx context.Context , metricName string ) ([]external_metrics.ExternalMetricValue , bool , error ) {
194
139
queuelen , err := s .getMessageCount (ctx )
195
140
if err != nil {
@@ -198,12 +143,11 @@ func (s *azureQueueScaler) GetMetricsAndActivity(ctx context.Context, metricName
198
143
}
199
144
200
145
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
202
147
}
203
148
204
149
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 {
207
151
queue , err := s .queueClient .PeekMessages (ctx , & azqueue.PeekMessagesOptions {NumberOfMessages : & maxPeekMessages })
208
152
if err != nil {
209
153
return 0 , err
0 commit comments