forked from kedacore/keda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure_queue.go
59 lines (45 loc) · 1.51 KB
/
azure_queue.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
package scalers
import (
"context"
"fmt"
"net/url"
"github.com/Azure/azure-storage-queue-go/azqueue"
)
// GetAzureQueueLength returns the length of a queue in int
func GetAzureQueueLength(ctx context.Context, podIdentity string, connectionString, queueName string, accountName string) (int32, error) {
var credential azqueue.Credential
var err error
if podIdentity == "" || podIdentity == "none" {
var accountKey string
_, accountName, accountKey, _, err = ParseAzureStorageConnectionString(connectionString)
if err != nil {
return -1, err
}
credential, err = azqueue.NewSharedKeyCredential(accountName, accountKey)
if err != nil {
return -1, err
}
} else if podIdentity == "azure" {
token, err := getAzureADPodIdentityToken("https://storage.azure.com/")
if err != nil {
azureQueueLog.Error(err, "Error fetching token cannot determine queue size")
return -1, nil
}
credential = azqueue.NewTokenCredential(token.AccessToken, nil)
} else {
return -1, fmt.Errorf("Azure queues doesn't support %s pod identity type", podIdentity)
}
p := azqueue.NewPipeline(credential, azqueue.PipelineOptions{})
u, _ := url.Parse(fmt.Sprintf("https://%s.queue.core.windows.net", accountName))
serviceURL := azqueue.NewServiceURL(*u, p)
queueURL := serviceURL.NewQueueURL(queueName)
_, err = queueURL.Create(ctx, azqueue.Metadata{})
if err != nil {
return -1, err
}
props, err := queueURL.GetProperties(ctx)
if err != nil {
return -1, err
}
return props.ApproximateMessagesCount(), nil
}