-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
azure_servicebus_scaler.go
executable file
·407 lines (345 loc) · 12.4 KB
/
azure_servicebus_scaler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package scalers
/*
Copyright 2021 The KEDA Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"context"
"fmt"
"regexp"
"strconv"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus/admin"
az "github.com/Azure/go-autorest/autorest/azure"
"github.com/go-logr/logr"
v2 "k8s.io/api/autoscaling/v2"
"k8s.io/metrics/pkg/apis/external_metrics"
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
"github.com/kedacore/keda/v2/pkg/scalers/azure"
"github.com/kedacore/keda/v2/pkg/scalers/scalersconfig"
kedautil "github.com/kedacore/keda/v2/pkg/util"
)
type entityType int
const (
none entityType = 0
queue entityType = 1
subscription entityType = 2
messageCountMetricName = "messageCount"
activationMessageCountMetricName = "activationMessageCount"
defaultTargetMessageCount = 5
)
type azureServiceBusScaler struct {
ctx context.Context
metricType v2.MetricTargetType
metadata *azureServiceBusMetadata
podIdentity kedav1alpha1.AuthPodIdentity
client *admin.Client
logger logr.Logger
}
type azureServiceBusMetadata struct {
targetLength int64
activationTargetLength int64
queueName string
topicName string
subscriptionName string
connection string
entityType entityType
fullyQualifiedNamespace string
useRegex bool
entityNameRegex *regexp.Regexp
operation string
triggerIndex int
timeout time.Duration
}
// NewAzureServiceBusScaler creates a new AzureServiceBusScaler
func NewAzureServiceBusScaler(ctx context.Context, config *scalersconfig.ScalerConfig) (Scaler, error) {
metricType, err := GetMetricTargetType(config)
if err != nil {
return nil, fmt.Errorf("error getting scaler metric type: %w", err)
}
logger := InitializeLogger(config, "azure_servicebus_scaler")
meta, err := parseAzureServiceBusMetadata(config, logger)
if err != nil {
return nil, fmt.Errorf("error parsing azure service bus metadata: %w", err)
}
return &azureServiceBusScaler{
ctx: ctx,
metricType: metricType,
metadata: meta,
podIdentity: config.PodIdentity,
logger: logger,
}, nil
}
// Creates an azureServiceBusMetadata struct from input metadata/env variables
func parseAzureServiceBusMetadata(config *scalersconfig.ScalerConfig, logger logr.Logger) (*azureServiceBusMetadata, error) {
meta := azureServiceBusMetadata{}
meta.entityType = none
meta.targetLength = defaultTargetMessageCount
meta.timeout = config.GlobalHTTPTimeout
// get target metric value
if val, ok := config.TriggerMetadata[messageCountMetricName]; ok {
messageCount, err := strconv.ParseInt(val, 10, 64)
if err != nil {
logger.Error(err, "Error parsing azure queue metadata", "messageCount", messageCountMetricName)
} else {
meta.targetLength = messageCount
}
}
meta.activationTargetLength = 0
if val, ok := config.TriggerMetadata[activationMessageCountMetricName]; ok {
activationMessageCount, err := strconv.ParseInt(val, 10, 64)
if err != nil {
logger.Error(err, "Error parsing azure queue metadata", activationMessageCountMetricName, activationMessageCountMetricName)
return nil, fmt.Errorf("error parsing azure queue metadata %s", activationMessageCountMetricName)
}
meta.activationTargetLength = activationMessageCount
}
meta.useRegex = false
if val, ok := config.TriggerMetadata["useRegex"]; ok {
useRegex, err := strconv.ParseBool(val)
if err != nil {
return nil, fmt.Errorf("useRegex has invalid value")
}
meta.useRegex = useRegex
}
meta.operation = sumOperation
if meta.useRegex {
if val, ok := config.TriggerMetadata["operation"]; ok {
meta.operation = val
}
switch meta.operation {
case avgOperation, maxOperation, sumOperation:
default:
return nil, fmt.Errorf("operation must be one of avg, max, or sum")
}
}
// get queue name OR topic and subscription name & set entity type accordingly
if val, ok := config.TriggerMetadata["queueName"]; ok {
meta.queueName = val
meta.entityType = queue
if _, ok := config.TriggerMetadata["subscriptionName"]; ok {
return nil, fmt.Errorf("subscription name provided with queue name")
}
if meta.useRegex {
entityNameRegex, err := regexp.Compile(meta.queueName)
if err != nil {
return nil, fmt.Errorf("queueName is not a valid regular expression")
}
entityNameRegex.Longest()
meta.entityNameRegex = entityNameRegex
}
}
if val, ok := config.TriggerMetadata["topicName"]; ok {
if meta.entityType == queue {
return nil, fmt.Errorf("both topic and queue name metadata provided")
}
meta.topicName = val
meta.entityType = subscription
if val, ok := config.TriggerMetadata["subscriptionName"]; ok {
meta.subscriptionName = val
} else {
return nil, fmt.Errorf("no subscription name provided with topic name")
}
if meta.useRegex {
entityNameRegex, err := regexp.Compile(meta.subscriptionName)
if err != nil {
return nil, fmt.Errorf("subscriptionName is not a valid regular expression")
}
entityNameRegex.Longest()
meta.entityNameRegex = entityNameRegex
}
}
if meta.entityType == none {
return nil, fmt.Errorf("no service bus entity type set")
}
switch config.PodIdentity.Provider {
case "", kedav1alpha1.PodIdentityProviderNone:
// get servicebus connection string
if config.AuthParams["connection"] != "" {
meta.connection = config.AuthParams["connection"]
} else if config.TriggerMetadata["connectionFromEnv"] != "" {
meta.connection = config.ResolvedEnv[config.TriggerMetadata["connectionFromEnv"]]
}
if len(meta.connection) == 0 {
return nil, fmt.Errorf("no connection setting given")
}
case kedav1alpha1.PodIdentityProviderAzureWorkload:
if val, ok := config.TriggerMetadata["namespace"]; ok {
envSuffixProvider := func(env az.Environment) (string, error) {
return env.ServiceBusEndpointSuffix, nil
}
endpointSuffix, err := azure.ParseEnvironmentProperty(config.TriggerMetadata, azure.DefaultEndpointSuffixKey, envSuffixProvider)
if err != nil {
return nil, err
}
meta.fullyQualifiedNamespace = fmt.Sprintf("%s.%s", val, endpointSuffix)
} else {
return nil, fmt.Errorf("namespace are required when using pod identity")
}
default:
return nil, fmt.Errorf("azure service bus doesn't support pod identity %s", config.PodIdentity.Provider)
}
meta.triggerIndex = config.TriggerIndex
return &meta, nil
}
// Close - nothing to close for SB
func (s *azureServiceBusScaler) Close(context.Context) error {
return nil
}
// GetMetricSpecForScaling returns the metric spec to be used by the HPA
func (s *azureServiceBusScaler) GetMetricSpecForScaling(context.Context) []v2.MetricSpec {
metricName := ""
var entityType string
if s.metadata.entityType == queue {
metricName = s.metadata.queueName
entityType = "queue"
} else {
metricName = s.metadata.topicName
entityType = "topic"
}
if s.metadata.useRegex {
metricName = fmt.Sprintf("%s-regex", entityType)
}
externalMetric := &v2.ExternalMetricSource{
Metric: v2.MetricIdentifier{
Name: GenerateMetricNameWithIndex(s.metadata.triggerIndex, kedautil.NormalizeString(fmt.Sprintf("azure-servicebus-%s", metricName))),
},
Target: GetMetricTarget(s.metricType, s.metadata.targetLength),
}
metricSpec := v2.MetricSpec{External: externalMetric, Type: externalMetricType}
return []v2.MetricSpec{metricSpec}
}
// GetMetricsAndActivity returns the current metrics to be served to the HPA
func (s *azureServiceBusScaler) GetMetricsAndActivity(ctx context.Context, metricName string) ([]external_metrics.ExternalMetricValue, bool, error) {
queuelen, err := s.getAzureServiceBusLength(ctx)
if err != nil {
s.logger.Error(err, "error getting service bus entity length")
return []external_metrics.ExternalMetricValue{}, false, err
}
metric := GenerateMetricInMili(metricName, float64(queuelen))
return []external_metrics.ExternalMetricValue{metric}, queuelen > s.metadata.activationTargetLength, nil
}
// Returns the length of the queue or subscription
func (s *azureServiceBusScaler) getAzureServiceBusLength(ctx context.Context) (int64, error) {
// get adminClient
adminClient, err := s.getServiceBusAdminClient()
if err != nil {
return -1, err
}
// switch case for queue vs topic here
switch s.metadata.entityType {
case queue:
return getQueueLength(ctx, adminClient, s.metadata)
case subscription:
return getSubscriptionLength(ctx, adminClient, s.metadata)
default:
return -1, fmt.Errorf("no entity type")
}
}
// Returns service bus namespace object
func (s *azureServiceBusScaler) getServiceBusAdminClient() (*admin.Client, error) {
if s.client != nil {
return s.client, nil
}
var err error
var client *admin.Client
opts := &admin.ClientOptions{
ClientOptions: policy.ClientOptions{
Transport: kedautil.CreateHTTPClient(s.metadata.timeout, false),
},
}
switch s.podIdentity.Provider {
case "", kedav1alpha1.PodIdentityProviderNone:
client, err = admin.NewClientFromConnectionString(s.metadata.connection, opts)
case kedav1alpha1.PodIdentityProviderAzureWorkload:
creds, chainedErr := azure.NewChainedCredential(s.logger, s.podIdentity)
if chainedErr != nil {
return nil, chainedErr
}
client, err = admin.NewClient(s.metadata.fullyQualifiedNamespace, creds, opts)
default:
err = fmt.Errorf("incorrect podIdentity type")
}
s.client = client
return client, err
}
func getQueueLength(ctx context.Context, adminClient *admin.Client, meta *azureServiceBusMetadata) (int64, error) {
if !meta.useRegex {
queueEntity, err := adminClient.GetQueueRuntimeProperties(ctx, meta.queueName, &admin.GetQueueRuntimePropertiesOptions{})
if err != nil {
return -1, err
}
if queueEntity == nil {
return -1, fmt.Errorf("queue %s doesn't exist", meta.queueName)
}
return int64(queueEntity.ActiveMessageCount), nil
}
messageCounts := make([]int64, 0)
queuePager := adminClient.NewListQueuesRuntimePropertiesPager(nil)
for queuePager.More() {
page, err := queuePager.NextPage(ctx)
if err != nil {
return -1, err
}
for _, queue := range page.QueueRuntimeProperties {
if meta.entityNameRegex.FindString(queue.QueueName) == queue.QueueName {
messageCounts = append(messageCounts, int64(queue.ActiveMessageCount))
}
}
}
return performOperation(messageCounts, meta.operation), nil
}
func getSubscriptionLength(ctx context.Context, adminClient *admin.Client, meta *azureServiceBusMetadata) (int64, error) {
if !meta.useRegex {
subscriptionEntity, err := adminClient.GetSubscriptionRuntimeProperties(ctx, meta.topicName, meta.subscriptionName,
&admin.GetSubscriptionRuntimePropertiesOptions{})
if err != nil {
return -1, err
}
if subscriptionEntity == nil {
return -1, fmt.Errorf("subscription %s doesn't exist in topic %s", meta.subscriptionName, meta.topicName)
}
return int64(subscriptionEntity.ActiveMessageCount), nil
}
messageCounts := make([]int64, 0)
subscriptionPager := adminClient.NewListSubscriptionsRuntimePropertiesPager(meta.topicName, nil)
for subscriptionPager.More() {
page, err := subscriptionPager.NextPage(ctx)
if err != nil {
return -1, err
}
for _, subscription := range page.SubscriptionRuntimeProperties {
if meta.entityNameRegex.FindString(subscription.SubscriptionName) == subscription.SubscriptionName {
messageCounts = append(messageCounts, int64(subscription.ActiveMessageCount))
}
}
}
return performOperation(messageCounts, meta.operation), nil
}
func performOperation(messageCounts []int64, operation string) int64 {
var result int64
for _, val := range messageCounts {
switch operation {
case avgOperation, sumOperation:
result += val
case maxOperation:
if val > result {
result = val
}
}
}
total := int64(len(messageCounts))
if operation == "avg" && total != 0 {
return result / total
}
return result
}