-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
gcp_storage_scaler.go
223 lines (185 loc) · 6.44 KB
/
gcp_storage_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
package scalers
import (
"context"
"errors"
"fmt"
"strconv"
"cloud.google.com/go/storage"
"github.com/go-logr/logr"
"google.golang.org/api/iterator"
option "google.golang.org/api/option"
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 (
// Default for how many objects per a single scaled processor
defaultTargetObjectCount = 100
// A limit on iterating bucket objects
defaultMaxBucketItemsToScan = 1000
)
type gcsScaler struct {
client *storage.Client
bucket *storage.BucketHandle
metricType v2.MetricTargetType
metadata *gcsMetadata
logger logr.Logger
}
type gcsMetadata struct {
bucketName string
gcpAuthorization *gcp.AuthorizationMetadata
maxBucketItemsToScan int64
metricName string
targetObjectCount int64
activationTargetObjectCount int64
blobDelimiter string
blobPrefix string
}
// NewGcsScaler creates a new gcsScaler
func NewGcsScaler(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_storage_scaler")
meta, err := parseGcsMetadata(config, logger)
if err != nil {
return nil, fmt.Errorf("error parsing GCP storage metadata: %w", err)
}
ctx := context.Background()
var client *storage.Client
switch {
case meta.gcpAuthorization.PodIdentityProviderEnabled:
client, err = storage.NewClient(ctx)
case meta.gcpAuthorization.GoogleApplicationCredentialsFile != "":
client, err = storage.NewClient(
ctx, option.WithCredentialsFile(meta.gcpAuthorization.GoogleApplicationCredentialsFile))
default:
client, err = storage.NewClient(
ctx, option.WithCredentialsJSON([]byte(meta.gcpAuthorization.GoogleApplicationCredentials)))
}
if err != nil {
return nil, fmt.Errorf("storage.NewClient: %w", err)
}
bucket := client.Bucket(meta.bucketName)
if bucket == nil {
return nil, fmt.Errorf("failed to create a handle to bucket %s", meta.bucketName)
}
logger.Info(fmt.Sprintf("Metadata %v", meta))
return &gcsScaler{
client: client,
bucket: bucket,
metricType: metricType,
metadata: meta,
logger: logger,
}, nil
}
func parseGcsMetadata(config *scalersconfig.ScalerConfig, logger logr.Logger) (*gcsMetadata, error) {
meta := gcsMetadata{}
meta.targetObjectCount = defaultTargetObjectCount
meta.maxBucketItemsToScan = defaultMaxBucketItemsToScan
if val, ok := config.TriggerMetadata["bucketName"]; ok {
if val == "" {
logger.Error(nil, "no bucket name given")
return nil, fmt.Errorf("no bucket name given")
}
meta.bucketName = val
} else {
logger.Error(nil, "no bucket name given")
return nil, fmt.Errorf("no bucket name given")
}
if val, ok := config.TriggerMetadata["targetObjectCount"]; ok {
targetObjectCount, err := strconv.ParseInt(val, 10, 64)
if err != nil {
logger.Error(err, "Error parsing targetObjectCount")
return nil, fmt.Errorf("error parsing targetObjectCount: %w", err)
}
meta.targetObjectCount = targetObjectCount
}
meta.activationTargetObjectCount = 0
if val, ok := config.TriggerMetadata["activationTargetObjectCount"]; ok {
activationTargetObjectCount, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return nil, fmt.Errorf("activationTargetObjectCount parsing error %w", err)
}
meta.activationTargetObjectCount = activationTargetObjectCount
}
if val, ok := config.TriggerMetadata["maxBucketItemsToScan"]; ok {
maxBucketItemsToScan, err := strconv.ParseInt(val, 10, 64)
if err != nil {
logger.Error(err, "Error parsing maxBucketItemsToScan")
return nil, fmt.Errorf("error parsing maxBucketItemsToScan: %w", err)
}
meta.maxBucketItemsToScan = maxBucketItemsToScan
}
if val, ok := config.TriggerMetadata["blobDelimiter"]; ok {
meta.blobDelimiter = val
}
if val, ok := config.TriggerMetadata["blobPrefix"]; ok {
meta.blobPrefix = val
}
auth, err := gcp.GetGCPAuthorization(config)
if err != nil {
return nil, err
}
meta.gcpAuthorization = auth
var metricName = kedautil.NormalizeString(fmt.Sprintf("gcp-storage-%s", meta.bucketName))
meta.metricName = GenerateMetricNameWithIndex(config.TriggerIndex, metricName)
return &meta, nil
}
func (s *gcsScaler) Close(context.Context) error {
if s.client != nil {
return s.client.Close()
}
return nil
}
// GetMetricSpecForScaling returns the metric spec for the HPA
func (s *gcsScaler) GetMetricSpecForScaling(context.Context) []v2.MetricSpec {
externalMetric := &v2.ExternalMetricSource{
Metric: v2.MetricIdentifier{
Name: s.metadata.metricName,
},
Target: GetMetricTarget(s.metricType, s.metadata.targetObjectCount),
}
metricSpec := v2.MetricSpec{External: externalMetric, Type: externalMetricType}
return []v2.MetricSpec{metricSpec}
}
// GetMetricsAndActivity returns the number of items in the bucket (up to s.metadata.maxBucketItemsToScan)
func (s *gcsScaler) GetMetricsAndActivity(ctx context.Context, metricName string) ([]external_metrics.ExternalMetricValue, bool, error) {
items, err := s.getItemCount(ctx, s.metadata.maxBucketItemsToScan)
if err != nil {
return []external_metrics.ExternalMetricValue{}, false, err
}
metric := GenerateMetricInMili(metricName, float64(items))
return []external_metrics.ExternalMetricValue{metric}, items > s.metadata.activationTargetObjectCount, nil
}
// getItemCount gets the number of items in the bucket, up to maxCount
func (s *gcsScaler) getItemCount(ctx context.Context, maxCount int64) (int64, error) {
query := &storage.Query{Delimiter: s.metadata.blobDelimiter, Prefix: s.metadata.blobPrefix}
err := query.SetAttrSelection([]string{"Name"})
if err != nil {
s.logger.Error(err, "failed to set attribute selection")
return 0, err
}
it := s.bucket.Objects(ctx, query)
var count int64
for count < maxCount {
_, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
if errors.Is(err, storage.ErrBucketNotExist) {
s.logger.Info("Bucket " + s.metadata.bucketName + " doesn't exist")
return 0, nil
}
s.logger.Error(err, "failed to enumerate items in bucket "+s.metadata.bucketName)
return count, err
}
count++
}
s.logger.V(1).Info(fmt.Sprintf("Counted %d items with a limit of %d", count, maxCount))
return count, nil
}