-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
gcp_pubsub_scaler.go
328 lines (272 loc) · 9.59 KB
/
gcp_pubsub_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
package scalers
import (
"context"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/go-logr/logr"
v2 "k8s.io/api/autoscaling/v2"
"k8s.io/metrics/pkg/apis/external_metrics"
"github.com/kedacore/keda/v2/pkg/scalers/gcp"
"github.com/kedacore/keda/v2/pkg/scalers/scalersconfig"
kedautil "github.com/kedacore/keda/v2/pkg/util"
)
const (
compositeSubscriptionIDPrefix = "projects/[a-z][a-zA-Z0-9-]*[a-zA-Z0-9]/(subscriptions|topics)/[a-zA-Z][a-zA-Z0-9-_~%\\+\\.]*"
prefixPubSubResource = "pubsub.googleapis.com/"
resourceTypePubSubSubscription = "subscription"
resourceTypePubSubTopic = "topic"
pubSubModeSubscriptionSize = "SubscriptionSize"
pubSubDefaultValue = 10
)
var regexpCompositeSubscriptionIDPrefix = regexp.MustCompile(compositeSubscriptionIDPrefix)
type pubsubScaler struct {
client *gcp.StackDriverClient
metricType v2.MetricTargetType
metadata *pubsubMetadata
logger logr.Logger
}
type pubsubMetadata struct {
mode string
value float64
activationValue float64
// a resource is one of subscription or topic
resourceType string
resourceName string
gcpAuthorization *gcp.AuthorizationMetadata
triggerIndex int
aggregation string
timeHorizon string
valueIfNull *float64
}
// NewPubSubScaler creates a new pubsubScaler
func NewPubSubScaler(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, "gcp_pub_sub_scaler")
meta, err := parsePubSubMetadata(config, logger)
if err != nil {
return nil, fmt.Errorf("error parsing PubSub metadata: %w", err)
}
return &pubsubScaler{
metricType: metricType,
metadata: meta,
logger: logger,
}, nil
}
func parsePubSubResourceConfig(config *scalersconfig.ScalerConfig, meta *pubsubMetadata) error {
sub, subPresent := config.TriggerMetadata["subscriptionName"]
subFromEnv, subFromEnvPresent := config.TriggerMetadata["subscriptionNameFromEnv"]
if subPresent && subFromEnvPresent {
return fmt.Errorf("exactly one of subscriptionName or subscriptionNameFromEnv is allowed")
}
hasSub := subPresent || subFromEnvPresent
topic, topicPresent := config.TriggerMetadata["topicName"]
topicFromEnv, topicFromEnvPresent := config.TriggerMetadata["topicNameFromEnv"]
if topicPresent && topicFromEnvPresent {
return fmt.Errorf("exactly one of topicName or topicNameFromEnv is allowed")
}
hasTopic := topicPresent || topicFromEnvPresent
if (!hasSub && !hasTopic) || (hasSub && hasTopic) {
return fmt.Errorf("exactly one of subscription or topic name must be given")
}
if hasSub {
if subPresent {
if sub == "" {
return fmt.Errorf("no subscription name given")
}
meta.resourceName = sub
} else {
if subFromEnv == "" {
return fmt.Errorf("no environment variable name given for resolving subscription name")
}
resolvedSub, ok := config.ResolvedEnv[subFromEnv]
if !ok {
return fmt.Errorf("resolved environment doesn't contain name '%s'", subFromEnv)
}
if resolvedSub == "" {
return fmt.Errorf("resolved environment subscription name is empty")
}
meta.resourceName = config.ResolvedEnv[subFromEnv]
}
meta.resourceType = resourceTypePubSubSubscription
} else {
if topicPresent {
if topic == "" {
return fmt.Errorf("no topic name given")
}
meta.resourceName = topic
} else {
if topicFromEnv == "" {
return fmt.Errorf("no environment variable name given for resolving topic name")
}
resolvedTopic, ok := config.ResolvedEnv[topicFromEnv]
if !ok {
return fmt.Errorf("resolved environment doesn't contain name '%s'", topicFromEnv)
}
if resolvedTopic == "" {
return fmt.Errorf("resolved environment topic name is empty")
}
meta.resourceName = config.ResolvedEnv[topicFromEnv]
}
meta.resourceType = resourceTypePubSubTopic
}
return nil
}
func parsePubSubMetadata(config *scalersconfig.ScalerConfig, logger logr.Logger) (*pubsubMetadata, error) {
meta := pubsubMetadata{mode: pubSubModeSubscriptionSize, value: pubSubDefaultValue}
mode, modePresent := config.TriggerMetadata["mode"]
value, valuePresent := config.TriggerMetadata["value"]
if subSize, subSizePresent := config.TriggerMetadata["subscriptionSize"]; subSizePresent {
if modePresent || valuePresent {
return nil, errors.New("you can use either mode and value fields or subscriptionSize field")
}
if _, topicPresent := config.TriggerMetadata["topicName"]; topicPresent {
return nil, errors.New("you cannot use subscriptionSize field together with topicName field. Use subscriptionName field instead")
}
logger.Info("subscriptionSize field is deprecated. Use mode and value fields instead")
subSizeValue, err := strconv.ParseFloat(subSize, 64)
if err != nil {
return nil, fmt.Errorf("value parsing error %w", err)
}
meta.value = subSizeValue
} else {
if modePresent {
meta.mode = mode
}
if valuePresent {
triggerValue, err := strconv.ParseFloat(value, 64)
if err != nil {
return nil, fmt.Errorf("value parsing error %w", err)
}
meta.value = triggerValue
}
}
if val, ok := config.TriggerMetadata["valueIfNull"]; ok && val != "" {
valueIfNull, err := strconv.ParseFloat(val, 64)
if err != nil {
return nil, fmt.Errorf("valueIfNull parsing error %w", err)
}
meta.valueIfNull = &valueIfNull
}
meta.aggregation = config.TriggerMetadata["aggregation"]
meta.timeHorizon = config.TriggerMetadata["timeHorizon"]
err := parsePubSubResourceConfig(config, &meta)
if err != nil {
return nil, err
}
meta.activationValue = 0
if val, ok := config.TriggerMetadata["activationValue"]; ok {
activationValue, err := strconv.ParseFloat(val, 64)
if err != nil {
return nil, fmt.Errorf("activationValue parsing error %w", err)
}
meta.activationValue = activationValue
}
auth, err := gcp.GetGCPAuthorization(config)
if err != nil {
return nil, err
}
meta.gcpAuthorization = auth
meta.triggerIndex = config.TriggerIndex
return &meta, nil
}
func (s *pubsubScaler) Close(context.Context) error {
if s.client != nil {
err := s.client.Close()
s.client = nil
if err != nil {
s.logger.Error(err, "error closing StackDriver client")
}
}
return nil
}
// GetMetricSpecForScaling returns the metric spec for the HPA
func (s *pubsubScaler) GetMetricSpecForScaling(context.Context) []v2.MetricSpec {
externalMetric := &v2.ExternalMetricSource{
Metric: v2.MetricIdentifier{
Name: GenerateMetricNameWithIndex(s.metadata.triggerIndex, kedautil.NormalizeString(fmt.Sprintf("gcp-ps-%s", s.metadata.resourceName))),
},
Target: GetMetricTargetMili(s.metricType, s.metadata.value),
}
// Create the metric spec for the HPA
metricSpec := v2.MetricSpec{
External: externalMetric,
Type: externalMetricType,
}
return []v2.MetricSpec{metricSpec}
}
// GetMetricsAndActivity connects to Stack Driver and finds the size of the pub sub subscription
func (s *pubsubScaler) GetMetricsAndActivity(ctx context.Context, metricName string) ([]external_metrics.ExternalMetricValue, bool, error) {
mode := s.metadata.mode
// SubscriptionSize is actually NumUndeliveredMessages in GCP PubSub.
// Considering backward compatibility, fallback "SubscriptionSize" to "NumUndeliveredMessages"
if mode == pubSubModeSubscriptionSize {
mode = "NumUndeliveredMessages"
}
prefix := prefixPubSubResource + s.metadata.resourceType + "/"
metricType := prefix + snakeCase(mode)
value, err := s.getMetrics(ctx, metricType)
if err != nil {
s.logger.Error(err, "error getting metric", "metricType", metricType)
return []external_metrics.ExternalMetricValue{}, false, err
}
metric := GenerateMetricInMili(metricName, value)
return []external_metrics.ExternalMetricValue{metric}, value > s.metadata.activationValue, nil
}
func (s *pubsubScaler) setStackdriverClient(ctx context.Context) error {
var client *gcp.StackDriverClient
var err error
if s.metadata.gcpAuthorization.PodIdentityProviderEnabled {
client, err = gcp.NewStackDriverClientPodIdentity(ctx)
} else {
client, err = gcp.NewStackDriverClient(ctx, s.metadata.gcpAuthorization.GoogleApplicationCredentials)
}
if err != nil {
return err
}
s.client = client
return nil
}
// getMetrics gets metric type value from stackdriver api
func (s *pubsubScaler) getMetrics(ctx context.Context, metricType string) (float64, error) {
if s.client == nil {
if err := s.setStackdriverClient(ctx); err != nil {
return -1, err
}
}
resourceID, projectID := getResourceData(s)
query, err := s.client.BuildMQLQuery(
projectID, s.metadata.resourceType, metricType, resourceID, s.metadata.aggregation, s.metadata.timeHorizon,
)
if err != nil {
return -1, err
}
// Pubsub metrics are collected every 60 seconds so no need to aggregate them.
// See: https://cloud.google.com/monitoring/api/metrics_gcp#gcp-pubsub
return s.client.QueryMetrics(ctx, projectID, query, s.metadata.valueIfNull)
}
func getResourceData(s *pubsubScaler) (string, string) {
var resourceID string
var projectID string
if regexpCompositeSubscriptionIDPrefix.MatchString(s.metadata.resourceName) {
resourceID = strings.Split(s.metadata.resourceName, "/")[3]
projectID = strings.Split(s.metadata.resourceName, "/")[1]
} else {
resourceID = s.metadata.resourceName
}
return resourceID, projectID
}
var (
regexpFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
regexpAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
)
func snakeCase(camelCase string) string {
snake := regexpFirstCap.ReplaceAllString(camelCase, "${1}_${2}")
snake = regexpAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}