-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
aws_sqs_queue_scaler.go
212 lines (174 loc) · 5.93 KB
/
aws_sqs_queue_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
package scalers
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"
v2beta2 "k8s.io/api/autoscaling/v2beta2"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/metrics/pkg/apis/external_metrics"
logf "sigs.k8s.io/controller-runtime/pkg/log"
kedautil "github.com/kedacore/keda/v2/pkg/util"
)
const (
targetQueueLengthDefault = 5
)
var (
awsSqsQueueMetricNames = []string{
"ApproximateNumberOfMessages",
"ApproximateNumberOfMessagesNotVisible",
}
sqsQueueLog = logf.Log.WithName("aws_sqs_queue_scaler")
)
type awsSqsQueueScaler struct {
metadata *awsSqsQueueMetadata
sqsClient sqsiface.SQSAPI
}
type awsSqsQueueMetadata struct {
targetQueueLength int
queueURL string
queueName string
awsRegion string
awsAuthorization awsAuthorizationMetadata
scalerIndex int
}
// NewAwsSqsQueueScaler creates a new awsSqsQueueScaler
func NewAwsSqsQueueScaler(config *ScalerConfig) (Scaler, error) {
meta, err := parseAwsSqsQueueMetadata(config)
if err != nil {
return nil, fmt.Errorf("error parsing SQS queue metadata: %s", err)
}
return &awsSqsQueueScaler{
metadata: meta,
sqsClient: createSqsClient(meta),
}, nil
}
func parseAwsSqsQueueMetadata(config *ScalerConfig) (*awsSqsQueueMetadata, error) {
meta := awsSqsQueueMetadata{}
meta.targetQueueLength = defaultTargetQueueLength
if val, ok := config.TriggerMetadata["queueLength"]; ok && val != "" {
queueLength, err := strconv.Atoi(val)
if err != nil {
meta.targetQueueLength = targetQueueLengthDefault
sqsQueueLog.Error(err, "Error parsing SQS queue metadata queueLength, using default %n", targetQueueLengthDefault)
} else {
meta.targetQueueLength = queueLength
}
}
if val, ok := config.TriggerMetadata["queueURL"]; ok && val != "" {
meta.queueURL = val
} else {
return nil, fmt.Errorf("no queueURL given")
}
queueURL, err := url.ParseRequestURI(meta.queueURL)
if err != nil {
return nil, fmt.Errorf("queueURL is not a valid URL")
}
queueURLPath := queueURL.Path
queueURLPathParts := strings.Split(queueURLPath, "/")
if len(queueURLPathParts) != 3 || len(queueURLPathParts[2]) == 0 {
return nil, fmt.Errorf("cannot get queueName from queueURL")
}
meta.queueName = queueURLPathParts[2]
if val, ok := config.TriggerMetadata["awsRegion"]; ok && val != "" {
meta.awsRegion = val
} else {
return nil, fmt.Errorf("no awsRegion given")
}
auth, err := getAwsAuthorization(config.AuthParams, config.TriggerMetadata, config.ResolvedEnv)
if err != nil {
return nil, err
}
meta.awsAuthorization = auth
meta.scalerIndex = config.ScalerIndex
return &meta, nil
}
func createSqsClient(metadata *awsSqsQueueMetadata) *sqs.SQS {
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String(metadata.awsRegion),
}))
var sqsClient *sqs.SQS
if metadata.awsAuthorization.podIdentityOwner {
creds := credentials.NewStaticCredentials(metadata.awsAuthorization.awsAccessKeyID, metadata.awsAuthorization.awsSecretAccessKey, "")
if metadata.awsAuthorization.awsRoleArn != "" {
creds = stscreds.NewCredentials(sess, metadata.awsAuthorization.awsRoleArn)
}
sqsClient = sqs.New(sess, &aws.Config{
Region: aws.String(metadata.awsRegion),
Credentials: creds,
})
} else {
sqsClient = sqs.New(sess, &aws.Config{
Region: aws.String(metadata.awsRegion),
})
}
return sqsClient
}
// IsActive determines if we need to scale from zero
func (s *awsSqsQueueScaler) IsActive(ctx context.Context) (bool, error) {
length, err := s.GetAwsSqsQueueLength()
if err != nil {
return false, err
}
return length > 0, nil
}
func (s *awsSqsQueueScaler) Close(context.Context) error {
return nil
}
func (s *awsSqsQueueScaler) GetMetricSpecForScaling(context.Context) []v2beta2.MetricSpec {
targetQueueLengthQty := resource.NewQuantity(int64(s.metadata.targetQueueLength), resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, kedautil.NormalizeString(fmt.Sprintf("aws-sqs-%s", s.metadata.queueName))),
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
AverageValue: targetQueueLengthQty,
},
}
metricSpec := v2beta2.MetricSpec{External: externalMetric, Type: externalMetricType}
return []v2beta2.MetricSpec{metricSpec}
}
// GetMetrics returns value for a supported metric and an error if there is a problem getting the metric
func (s *awsSqsQueueScaler) GetMetrics(ctx context.Context, metricName string, metricSelector labels.Selector) ([]external_metrics.ExternalMetricValue, error) {
queuelen, err := s.GetAwsSqsQueueLength()
if err != nil {
sqsQueueLog.Error(err, "Error getting queue length")
return []external_metrics.ExternalMetricValue{}, err
}
metric := external_metrics.ExternalMetricValue{
MetricName: metricName,
Value: *resource.NewQuantity(int64(queuelen), resource.DecimalSI),
Timestamp: metav1.Now(),
}
return append([]external_metrics.ExternalMetricValue{}, metric), nil
}
// Get SQS Queue Length
func (s *awsSqsQueueScaler) GetAwsSqsQueueLength() (int32, error) {
input := &sqs.GetQueueAttributesInput{
AttributeNames: aws.StringSlice(awsSqsQueueMetricNames),
QueueUrl: aws.String(s.metadata.queueURL),
}
output, err := s.sqsClient.GetQueueAttributes(input)
if err != nil {
return -1, err
}
var approximateNumberOfMessages int64
for _, awsSqsQueueMetric := range awsSqsQueueMetricNames {
metricValue, err := strconv.ParseInt(*output.Attributes[awsSqsQueueMetric], 10, 32)
if err != nil {
return -1, err
}
approximateNumberOfMessages += metricValue
}
return int32(approximateNumberOfMessages), nil
}