-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
external_scaler.go
392 lines (331 loc) · 11.3 KB
/
external_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
package scalers
import (
"context"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/go-logr/logr"
"github.com/mitchellh/hashstructure"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
v2 "k8s.io/api/autoscaling/v2"
"k8s.io/metrics/pkg/apis/external_metrics"
pb "github.com/kedacore/keda/v2/pkg/scalers/externalscaler"
"github.com/kedacore/keda/v2/pkg/util"
)
type externalScaler struct {
metricType v2.MetricTargetType
metadata externalScalerMetadata
scaledObjectRef pb.ScaledObjectRef
logger logr.Logger
}
type externalPushScaler struct {
externalScaler
}
type externalScalerMetadata struct {
scalerAddress string
tlsCertFile string
originalMetadata map[string]string
scalerIndex int
caCert string
tlsClientCert string
tlsClientKey string
unsafeSsl bool
}
type connectionGroup struct {
grpcConnection *grpc.ClientConn
}
// a pool of connectionGroup per metadata hash
var connectionPool sync.Map
// NewExternalScaler creates a new external scaler - calls the GRPC interface
// to create a new scaler
func NewExternalScaler(config *ScalerConfig) (Scaler, error) {
metricType, err := GetMetricTargetType(config)
if err != nil {
return nil, fmt.Errorf("error getting external scaler metric type: %w", err)
}
meta, err := parseExternalScalerMetadata(config)
if err != nil {
return nil, fmt.Errorf("error parsing external scaler metadata: %w", err)
}
return &externalScaler{
metricType: metricType,
metadata: meta,
scaledObjectRef: pb.ScaledObjectRef{
Name: config.ScalableObjectName,
Namespace: config.ScalableObjectNamespace,
ScalerMetadata: meta.originalMetadata,
},
logger: InitializeLogger(config, "external_scaler"),
}, nil
}
// NewExternalPushScaler creates a new externalPushScaler push scaler
func NewExternalPushScaler(config *ScalerConfig) (PushScaler, error) {
metricType, err := GetMetricTargetType(config)
if err != nil {
return nil, fmt.Errorf("error getting external scaler metric type: %w", err)
}
meta, err := parseExternalScalerMetadata(config)
if err != nil {
return nil, fmt.Errorf("error parsing external scaler metadata: %w", err)
}
return &externalPushScaler{
externalScaler{
metricType: metricType,
metadata: meta,
scaledObjectRef: pb.ScaledObjectRef{
Name: config.ScalableObjectName,
Namespace: config.ScalableObjectNamespace,
ScalerMetadata: meta.originalMetadata,
},
logger: InitializeLogger(config, "external_push_scaler"),
},
}, nil
}
func parseExternalScalerMetadata(config *ScalerConfig) (externalScalerMetadata, error) {
meta := externalScalerMetadata{
originalMetadata: config.TriggerMetadata,
}
// Check if scalerAddress is present
if val, ok := config.TriggerMetadata["scalerAddress"]; ok && val != "" {
meta.scalerAddress = val
} else {
return meta, fmt.Errorf("scaler Address is a required field")
}
if val, ok := config.TriggerMetadata["tlsCertFile"]; ok && val != "" {
meta.tlsCertFile = val
}
meta.originalMetadata = make(map[string]string)
if val, ok := config.AuthParams["caCert"]; ok {
meta.caCert = val
}
if val, ok := config.AuthParams["tlsClientCert"]; ok {
meta.tlsClientCert = val
}
if val, ok := config.AuthParams["tlsClientKey"]; ok {
meta.tlsClientKey = val
}
meta.unsafeSsl = false
if val, ok := config.TriggerMetadata["unsafeSsl"]; ok && val != "" {
boolVal, err := strconv.ParseBool(val)
if err != nil {
return meta, fmt.Errorf("failed to parse insecureSkipVerify value. Must be either true or false")
}
meta.unsafeSsl = boolVal
}
// Add elements to metadata
for key, value := range config.TriggerMetadata {
// Check if key is in resolved environment and resolve
if strings.HasSuffix(key, "FromEnv") {
if val, ok := config.ResolvedEnv[value]; ok && val != "" {
meta.originalMetadata[key] = val
}
} else {
meta.originalMetadata[key] = value
}
}
meta.scalerIndex = config.ScalerIndex
return meta, nil
}
func (s *externalScaler) Close(context.Context) error {
return nil
}
// GetMetricSpecForScaling returns the metric spec for the HPA
func (s *externalScaler) GetMetricSpecForScaling(ctx context.Context) []v2.MetricSpec {
var result []v2.MetricSpec
grpcClient, err := getClientForConnectionPool(s.metadata, s.logger)
if err != nil {
s.logger.Error(err, "error building grpc connection")
return result
}
response, err := grpcClient.GetMetricSpec(ctx, &s.scaledObjectRef)
if err != nil {
s.logger.Error(err, "error")
return nil
}
for _, spec := range response.MetricSpecs {
externalMetric := &v2.ExternalMetricSource{
Metric: v2.MetricIdentifier{
Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, spec.MetricName),
},
Target: GetMetricTarget(s.metricType, spec.TargetSize),
}
// Create the metric spec for the HPA
metricSpec := v2.MetricSpec{
External: externalMetric,
Type: externalMetricType,
}
result = append(result, metricSpec)
}
return result
}
// GetMetricsAndActivity returns value for a supported metric and an error if there is a problem getting the metric
func (s *externalScaler) GetMetricsAndActivity(ctx context.Context, metricName string) ([]external_metrics.ExternalMetricValue, bool, error) {
var metrics []external_metrics.ExternalMetricValue
grpcClient, err := getClientForConnectionPool(s.metadata, s.logger)
if err != nil {
return []external_metrics.ExternalMetricValue{}, false, err
}
// Remove the sX- prefix as the external scaler shouldn't have to know about it
metricNameWithoutIndex, err := RemoveIndexFromMetricName(s.metadata.scalerIndex, metricName)
if err != nil {
return []external_metrics.ExternalMetricValue{}, false, err
}
request := &pb.GetMetricsRequest{
MetricName: metricNameWithoutIndex,
ScaledObjectRef: &s.scaledObjectRef,
}
metricsResponse, err := grpcClient.GetMetrics(ctx, request)
if err != nil {
s.logger.Error(err, "error")
return []external_metrics.ExternalMetricValue{}, false, err
}
for _, metricResult := range metricsResponse.MetricValues {
metric := GenerateMetricInMili(metricName, float64(metricResult.MetricValue))
metrics = append(metrics, metric)
}
isActiveResponse, err := grpcClient.IsActive(ctx, &s.scaledObjectRef)
if err != nil {
s.logger.Error(err, "error calling IsActive on external scaler")
return []external_metrics.ExternalMetricValue{}, false, err
}
return metrics, isActiveResponse.Result, nil
}
// handleIsActiveStream is the only writer to the active channel and will close it on return.
func (s *externalPushScaler) Run(ctx context.Context, active chan<- bool) {
defer close(active)
// It's possible for the connection to get terminated anytime, we need to run this in a retry loop
runWithLog := func() {
grpcClient, err := getClientForConnectionPool(s.metadata, s.logger)
if err != nil {
s.logger.Error(err, "error running internalRun")
return
}
if err := handleIsActiveStream(ctx, &s.scaledObjectRef, grpcClient, active); err != nil {
s.logger.Error(err, "error running internalRun")
return
}
}
// retry on error from runWithLog() starting by 2 sec backing off * 2 with a max of 2 minute
retryDuration := time.Second * 2
// the caller of this function needs to ensure that they call Stop() on the resulting
// timer, to release background resources.
retryBackoff := func() *time.Timer {
tmr := time.NewTimer(retryDuration)
retryDuration *= 2
if retryDuration > time.Minute*1 {
retryDuration = time.Minute * 1
}
return tmr
}
// start the first run without delay
runWithLog()
for {
backoffTimer := retryBackoff()
select {
case <-ctx.Done():
backoffTimer.Stop()
return
case <-backoffTimer.C:
backoffTimer.Stop()
runWithLog()
}
}
}
// handleIsActiveStream calls blocks on a stream call from the GRPC server. It'll only terminate on error, stream completion, or ctx cancellation.
func handleIsActiveStream(ctx context.Context, scaledObjectRef *pb.ScaledObjectRef, grpcClient pb.ExternalScalerClient, active chan<- bool) error {
stream, err := grpcClient.StreamIsActive(ctx, scaledObjectRef)
if err != nil {
return err
}
for {
resp, err := stream.Recv()
if err != nil {
return err
}
active <- resp.Result
}
}
var connectionPoolMutex sync.Mutex
// getClientForConnectionPool returns a grpcClient and a done() Func. The done() function must be called once the client is no longer
// in use to clean up the shared grpc.ClientConn
func getClientForConnectionPool(metadata externalScalerMetadata, logger logr.Logger) (pb.ExternalScalerClient, error) {
connectionPoolMutex.Lock()
defer connectionPoolMutex.Unlock()
buildGRPCConnection := func(metadata externalScalerMetadata) (*grpc.ClientConn, error) {
// FIXME: DEPRECATED to be removed in v2.13 https://github.com/kedacore/keda/issues/4549
if metadata.tlsCertFile != "" {
logger.V(1).Info("tlsCertFile in ScaleObject metadata will be deprecated in v2.12. Please use" +
"tlsClientCert, tlsClientKey and caCert in TriggerAuthentication instead.")
creds, err := credentials.NewClientTLSFromFile(metadata.tlsCertFile, "")
if err != nil {
return nil, err
}
return grpc.Dial(metadata.scalerAddress, grpc.WithTransportCredentials(creds))
}
tlsConfig, err := util.NewTLSConfig(metadata.tlsClientCert, metadata.tlsClientKey, metadata.caCert, metadata.unsafeSsl)
if err != nil {
return nil, err
}
if len(tlsConfig.Certificates) > 0 || metadata.caCert != "" {
// nosemgrep: go.grpc.ssrf.grpc-tainted-url-host.grpc-tainted-url-host
return grpc.Dial(metadata.scalerAddress, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
}
return grpc.Dial(metadata.scalerAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
// create a unique key per-metadata. If scaledObjects share the same connection properties
// in the metadata, they will share the same grpc.ClientConn
key, err := hashstructure.Hash(metadata.scalerAddress, nil)
if err != nil {
return nil, err
}
if i, ok := connectionPool.Load(key); ok {
if connGroup, ok := i.(*connectionGroup); ok {
return pb.NewExternalScalerClient(connGroup.grpcConnection), nil
}
}
conn, err := buildGRPCConnection(metadata)
if err != nil {
return nil, err
}
connGroup := &connectionGroup{
grpcConnection: conn,
}
connectionPool.Store(key, connGroup)
go func() {
// clean up goroutine.
// once gRPC client is shutdown, remove the connection from the pool and Close() grpc.ClientConn
// nosemgrep: dgryski.semgrep-go.contexttodo.context-todo
<-waitForState(context.TODO(), connGroup.grpcConnection, connectivity.Shutdown)
connectionPoolMutex.Lock()
defer connectionPoolMutex.Unlock()
connectionPool.Delete(key)
connGroup.grpcConnection.Close()
}()
return pb.NewExternalScalerClient(connGroup.grpcConnection), nil
}
func waitForState(ctx context.Context, conn *grpc.ClientConn, states ...connectivity.State) (done chan struct{}) {
done = make(chan struct{})
go func() {
defer close(done)
for {
changeState := conn.WaitForStateChange(ctx, conn.GetState())
if !changeState {
// ctx is done, return
continue
}
nowState := conn.GetState()
for _, state := range states {
if state == nowState {
// match one of the state passed return
return
}
}
}
}()
return done
}