-
Notifications
You must be signed in to change notification settings - Fork 1k
/
scale_handler.go
691 lines (620 loc) · 26.4 KB
/
scale_handler.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/*
Copyright 2021 The KEDA Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scaling
import (
"context"
"fmt"
"strings"
"sync"
"time"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
corev1listers "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/scale"
"k8s.io/client-go/tools/record"
"k8s.io/metrics/pkg/apis/external_metrics"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
"github.com/kedacore/keda/v2/pkg/eventreason"
"github.com/kedacore/keda/v2/pkg/fallback"
metricsserviceapi "github.com/kedacore/keda/v2/pkg/metricsservice/api"
"github.com/kedacore/keda/v2/pkg/prommetrics"
"github.com/kedacore/keda/v2/pkg/scalers"
"github.com/kedacore/keda/v2/pkg/scaling/cache"
"github.com/kedacore/keda/v2/pkg/scaling/cache/metricscache"
"github.com/kedacore/keda/v2/pkg/scaling/executor"
"github.com/kedacore/keda/v2/pkg/scaling/resolver"
)
// ScaleHandler encapsulates the logic of calling the right scalers for
// each ScaledObject and making the final scale decision and operation
type ScaleHandler interface {
HandleScalableObject(ctx context.Context, scalableObject interface{}) error
DeleteScalableObject(ctx context.Context, scalableObject interface{}) error
GetScalersCache(ctx context.Context, scalableObject interface{}) (*cache.ScalersCache, error)
ClearScalersCache(ctx context.Context, scalableObject interface{}) error
GetScaledObjectMetrics(ctx context.Context, scaledObjectName, scaledObjectNamespace, metricName string) (*external_metrics.ExternalMetricValueList, *metricsserviceapi.PromMetricsMsg, error)
}
type scaleHandler struct {
client client.Client
logger logr.Logger
scaleLoopContexts *sync.Map
scaleExecutor executor.ScaleExecutor
globalHTTPTimeout time.Duration
recorder record.EventRecorder
scalerCaches map[string]*cache.ScalersCache
scalerCachesLock *sync.RWMutex
scaledObjectsMetricCache metricscache.MetricsCache
secretsLister corev1listers.SecretLister
}
// NewScaleHandler creates a ScaleHandler object
func NewScaleHandler(client client.Client, scaleClient scale.ScalesGetter, reconcilerScheme *runtime.Scheme, globalHTTPTimeout time.Duration, recorder record.EventRecorder, secretsLister corev1listers.SecretLister) ScaleHandler {
return &scaleHandler{
client: client,
logger: logf.Log.WithName("scalehandler"),
scaleLoopContexts: &sync.Map{},
scaleExecutor: executor.NewScaleExecutor(client, scaleClient, reconcilerScheme, recorder),
globalHTTPTimeout: globalHTTPTimeout,
recorder: recorder,
scalerCaches: map[string]*cache.ScalersCache{},
scalerCachesLock: &sync.RWMutex{},
scaledObjectsMetricCache: metricscache.NewMetricsCache(),
secretsLister: secretsLister,
}
}
func (h *scaleHandler) HandleScalableObject(ctx context.Context, scalableObject interface{}) error {
withTriggers, err := kedav1alpha1.AsDuckWithTriggers(scalableObject)
if err != nil {
h.logger.Error(err, "error duck typing object into withTrigger")
return err
}
key := withTriggers.GenerateIdentifier()
ctx, cancel := context.WithCancel(ctx)
// cancel the outdated ScaleLoop for the same ScaledObject (if exists)
value, loaded := h.scaleLoopContexts.LoadOrStore(key, cancel)
if loaded {
cancelValue, ok := value.(context.CancelFunc)
if ok {
cancelValue()
}
h.scaleLoopContexts.Store(key, cancel)
} else {
h.recorder.Event(withTriggers, corev1.EventTypeNormal, eventreason.KEDAScalersStarted, "Started scalers watch")
}
// a mutex is used to synchronize scale requests per scalableObject
scalingMutex := &sync.Mutex{}
// passing deep copy of ScaledObject/ScaledJob to the scaleLoop go routines, it's a precaution to not have global objects shared between threads
switch obj := scalableObject.(type) {
case *kedav1alpha1.ScaledObject:
go h.startPushScalers(ctx, withTriggers, obj.DeepCopy(), scalingMutex)
go h.startScaleLoop(ctx, withTriggers, obj.DeepCopy(), scalingMutex)
case *kedav1alpha1.ScaledJob:
go h.startPushScalers(ctx, withTriggers, obj.DeepCopy(), scalingMutex)
go h.startScaleLoop(ctx, withTriggers, obj.DeepCopy(), scalingMutex)
}
return nil
}
func (h *scaleHandler) DeleteScalableObject(ctx context.Context, scalableObject interface{}) error {
withTriggers, err := kedav1alpha1.AsDuckWithTriggers(scalableObject)
if err != nil {
h.logger.Error(err, "error duck typing object into withTrigger")
return err
}
key := withTriggers.GenerateIdentifier()
result, ok := h.scaleLoopContexts.Load(key)
if ok {
cancel, ok := result.(context.CancelFunc)
if ok {
cancel()
}
h.scaleLoopContexts.Delete(key)
err := h.ClearScalersCache(ctx, scalableObject)
if err != nil {
h.logger.Error(err, "error clearing scalers cache")
}
h.recorder.Event(withTriggers, corev1.EventTypeNormal, eventreason.KEDAScalersStopped, "Stopped scalers watch")
} else {
h.logger.V(1).Info("ScaledObject was not found in controller cache", "key", key)
}
return nil
}
// startScaleLoop blocks forever and checks the scaledObject based on its pollingInterval
func (h *scaleHandler) startScaleLoop(ctx context.Context, withTriggers *kedav1alpha1.WithTriggers, scalableObject interface{}, scalingMutex sync.Locker) {
logger := h.logger.WithValues("type", withTriggers.Kind, "namespace", withTriggers.Namespace, "name", withTriggers.Name)
pollingInterval := withTriggers.GetPollingInterval()
logger.V(1).Info("Watching with pollingInterval", "PollingInterval", pollingInterval)
for {
tmr := time.NewTimer(pollingInterval)
h.checkScalers(ctx, scalableObject, scalingMutex)
select {
case <-tmr.C:
tmr.Stop()
case <-ctx.Done():
logger.V(1).Info("Context canceled")
err := h.ClearScalersCache(ctx, scalableObject)
if err != nil {
logger.Error(err, "error clearing scalers cache")
}
tmr.Stop()
return
}
}
}
// GetScalersCache returns cache for input scalableObject, if the object is not found in the cache, it returns a new one
// if the input object is ScaledObject, it also compares the Generation of the input of object with the one stored in the cache,
// this is needed for out of scalerLoop invocations of this method (in package `controllers/keda`).
func (h *scaleHandler) GetScalersCache(ctx context.Context, scalableObject interface{}) (*cache.ScalersCache, error) {
withTriggers, err := kedav1alpha1.AsDuckWithTriggers(scalableObject)
if err != nil {
return nil, err
}
key := withTriggers.GenerateIdentifier()
generation := withTriggers.Generation
return h.performGetScalersCache(ctx, key, scalableObject, &generation, "", "", "")
}
// getScalersCacheForScaledObject returns cache for input ScaledObject, referenced by name and namespace
// we don't need to compare the Generation, because this method should be called only inside scale loop, where we have up to date object.
func (h *scaleHandler) getScalersCacheForScaledObject(ctx context.Context, scaledObjectName, scaledObjectNamespace string) (*cache.ScalersCache, error) {
key := kedav1alpha1.GenerateIdentifier("ScaledObject", scaledObjectNamespace, scaledObjectName)
return h.performGetScalersCache(ctx, key, nil, nil, "ScaledObject", scaledObjectNamespace, scaledObjectName)
}
// performGetScalersCache returns cache for input scalableObject, it is common code used by GetScalersCache() and getScalersCacheForScaledObject() methods
func (h *scaleHandler) performGetScalersCache(ctx context.Context, key string, scalableObject interface{}, scalableObjectGeneration *int64, scalableObjectKind, scalableObjectNamespace, scalableObjectName string) (*cache.ScalersCache, error) {
h.scalerCachesLock.RLock()
if cache, ok := h.scalerCaches[key]; ok {
// generation was specified -> let's include it in the check as well
if scalableObjectGeneration != nil {
if cache.ScalableObjectGeneration == *scalableObjectGeneration {
h.scalerCachesLock.RUnlock()
return cache, nil
}
} else {
h.scalerCachesLock.RUnlock()
return cache, nil
}
}
h.scalerCachesLock.RUnlock()
h.scalerCachesLock.Lock()
defer h.scalerCachesLock.Unlock()
if cache, ok := h.scalerCaches[key]; ok {
// generation was specified -> let's include it in the check as well
if scalableObjectGeneration != nil {
if cache.ScalableObjectGeneration == *scalableObjectGeneration {
return cache, nil
}
// object was found in cache, but the generation is not correct,
// let's close scalers in the cache and proceed further to recreate the cache
cache.Close(ctx)
} else {
return cache, nil
}
}
if scalableObject == nil {
switch scalableObjectKind {
case "ScaledObject":
scaledObject := &kedav1alpha1.ScaledObject{}
err := h.client.Get(ctx, types.NamespacedName{Name: scalableObjectName, Namespace: scalableObjectNamespace}, scaledObject)
if err != nil {
h.logger.Error(err, "failed to get ScaledObject", "name", scalableObjectName, "namespace", scalableObjectNamespace)
return nil, err
}
scalableObject = scaledObject
case "ScaledJob":
scaledJob := &kedav1alpha1.ScaledJob{}
err := h.client.Get(ctx, types.NamespacedName{Name: scalableObjectName, Namespace: scalableObjectNamespace}, scaledJob)
if err != nil {
h.logger.Error(err, "failed to get ScaledJob", "name", scalableObjectName, "namespace", scalableObjectNamespace)
return nil, err
}
scalableObject = scaledJob
default:
err := fmt.Errorf("unknown ScalableObjectKind, got=%q", scalableObjectKind)
h.logger.Error(err, "unknown kind", "name", scalableObjectName, "namespace", scalableObjectNamespace)
return nil, err
}
}
withTriggers, err := kedav1alpha1.AsDuckWithTriggers(scalableObject)
if err != nil {
return nil, err
}
podTemplateSpec, containerName, err := resolver.ResolveScaleTargetPodSpec(ctx, h.client, h.logger, scalableObject)
if err != nil {
return nil, err
}
scalers, err := h.buildScalers(ctx, withTriggers, podTemplateSpec, containerName)
if err != nil {
return nil, err
}
newCache := &cache.ScalersCache{
Scalers: scalers,
ScalableObjectGeneration: withTriggers.Generation,
Recorder: h.recorder,
}
switch obj := scalableObject.(type) {
case *kedav1alpha1.ScaledObject:
newCache.ScaledObject = obj
default:
}
h.scalerCaches[key] = newCache
return h.scalerCaches[key], nil
}
func (h *scaleHandler) ClearScalersCache(ctx context.Context, scalableObject interface{}) error {
withTriggers, err := kedav1alpha1.AsDuckWithTriggers(scalableObject)
if err != nil {
return err
}
key := withTriggers.GenerateIdentifier()
go h.scaledObjectsMetricCache.Delete(key)
h.scalerCachesLock.Lock()
defer h.scalerCachesLock.Unlock()
if cache, ok := h.scalerCaches[key]; ok {
h.logger.V(1).WithValues("key", key).Info("Removing entry from ScalersCache")
cache.Close(ctx)
delete(h.scalerCaches, key)
}
return nil
}
func (h *scaleHandler) startPushScalers(ctx context.Context, withTriggers *kedav1alpha1.WithTriggers, scalableObject interface{}, scalingMutex sync.Locker) {
logger := h.logger.WithValues("type", withTriggers.Kind, "namespace", withTriggers.Namespace, "name", withTriggers.Name)
cache, err := h.GetScalersCache(ctx, scalableObject)
if err != nil {
logger.Error(err, "Error getting scalers", "object", scalableObject)
return
}
for _, ps := range cache.GetPushScalers() {
go func(s scalers.PushScaler) {
activeCh := make(chan bool)
go s.Run(ctx, activeCh)
for {
select {
case <-ctx.Done():
return
case active := <-activeCh:
scalingMutex.Lock()
switch obj := scalableObject.(type) {
case *kedav1alpha1.ScaledObject:
h.scaleExecutor.RequestScale(ctx, obj, active, false)
case *kedav1alpha1.ScaledJob:
h.logger.Info("Warning: External Push Scaler does not support ScaledJob", "object", scalableObject)
}
scalingMutex.Unlock()
}
}
}(ps)
}
}
// checkScalers contains the main logic for the ScaleHandler scaling logic.
// It'll check each trigger active status then call RequestScale
func (h *scaleHandler) checkScalers(ctx context.Context, scalableObject interface{}, scalingMutex sync.Locker) {
cache, err := h.GetScalersCache(ctx, scalableObject)
if err != nil {
h.logger.Error(err, "Error getting scalers", "object", scalableObject)
return
}
scalingMutex.Lock()
defer scalingMutex.Unlock()
switch obj := scalableObject.(type) {
case *kedav1alpha1.ScaledObject:
err = h.client.Get(ctx, types.NamespacedName{Name: obj.Name, Namespace: obj.Namespace}, obj)
if err != nil {
h.logger.Error(err, "Error getting scaledObject", "object", scalableObject)
return
}
isActive, isError, metricsRecords := cache.GetScaledObjectState(ctx, obj)
h.scaleExecutor.RequestScale(ctx, obj, isActive, isError)
if len(metricsRecords) > 0 {
h.logger.V(1).Info("Storing metrics to cache", "scaledObject.Namespace", obj.Namespace, "scaledObject.Name", obj.Name, "metricsRecords", metricsRecords)
h.scaledObjectsMetricCache.StoreRecords(obj.GenerateIdentifier(), metricsRecords)
}
case *kedav1alpha1.ScaledJob:
err = h.client.Get(ctx, types.NamespacedName{Name: obj.Name, Namespace: obj.Namespace}, obj)
if err != nil {
h.logger.Error(err, "Error getting scaledJob", "object", scalableObject)
return
}
isActive, scaleTo, maxScale := cache.IsScaledJobActive(ctx, obj)
h.scaleExecutor.RequestJobScale(ctx, obj, isActive, scaleTo, maxScale)
}
}
// GetScaledObjectMetrics returns metrics for specified metric name for a ScaledObject identified by it's name and namespace.
// The second return value are Prometheus metrics that needed to be exposed (used by DEPRECATED Prometheus Server on KEDA Metrics Server)
// It could either query the metric value directly from the scaler or from a cache, that's being stored for the scaler.
func (h *scaleHandler) GetScaledObjectMetrics(ctx context.Context, scaledObjectName, scaledObjectNamespace, metricName string) (*external_metrics.ExternalMetricValueList, *metricsserviceapi.PromMetricsMsg, error) {
var matchingMetrics []external_metrics.ExternalMetricValue
exportedPromMetrics := metricsserviceapi.PromMetricsMsg{
ScaledObjectErr: false,
ScalerMetric: []*metricsserviceapi.ScalerMetricMsg{},
ScalerError: []*metricsserviceapi.ScalerErrorMsg{},
}
cache, err := h.getScalersCacheForScaledObject(ctx, scaledObjectName, scaledObjectNamespace)
prommetrics.RecordScaledObjectError(scaledObjectNamespace, scaledObjectName, err)
// [DEPRECATED] handle exporting Prometheus metrics from Operator to Metrics Server
exportedPromMetrics.ScaledObjectErr = (err != nil)
if err != nil {
return nil, &exportedPromMetrics, fmt.Errorf("error when getting scalers %s", err)
}
var scaledObject *kedav1alpha1.ScaledObject
if cache.ScaledObject != nil {
scaledObject = cache.ScaledObject
} else {
err := fmt.Errorf("scaledObject not found in the cache")
h.logger.Error(err, "scaledObject not found in the cache", "name", scaledObjectName, "namespace", scaledObjectNamespace)
return nil, &exportedPromMetrics, err
}
scalerError := false
scaledObjectIdentifier := scaledObject.GenerateIdentifier()
// let's check metrics for all scalers in a ScaledObject
scalers, scalerConfigs := cache.GetScalers()
for scalerIndex := 0; scalerIndex < len(scalers); scalerIndex++ {
metricSpecs := scalers[scalerIndex].GetMetricSpecForScaling(ctx)
scalerName := strings.Replace(fmt.Sprintf("%T", scalers[scalerIndex]), "*scalers.", "", 1)
if scalerConfigs[scalerIndex].TriggerName != "" {
scalerName = scalerConfigs[scalerIndex].TriggerName
}
for _, metricSpec := range metricSpecs {
// skip cpu/memory resource scaler
if metricSpec.External == nil {
continue
}
// Filter only the desired metric
if strings.EqualFold(metricSpec.External.Metric.Name, metricName) {
var metrics []external_metrics.ExternalMetricValue
// if cache is defined for this scaler/metric, let's try to hit it first
metricsFoundInCache := false
if scalerConfigs[scalerIndex].TriggerUseCachedMetrics {
var metricsRecord metricscache.MetricsRecord
if metricsRecord, metricsFoundInCache = h.scaledObjectsMetricCache.ReadRecord(scaledObjectIdentifier, metricSpec.External.Metric.Name); metricsFoundInCache {
h.logger.V(1).Info("Reading metrics from cache", "scaledObject.Namespace", scaledObjectNamespace, "scaledObject.Name", scaledObjectName, "scaler", scalerName, "metricName", metricSpec.External.Metric.Name, "metricsRecord", metricsRecord)
metrics = metricsRecord.Metric
err = metricsRecord.ScalerError
}
}
if !metricsFoundInCache {
metrics, err = cache.GetMetricsForScaler(ctx, scalerIndex, metricName)
h.logger.V(1).Info("Getting metrics from scaler", "scaledObject.Namespace", scaledObjectNamespace, "scaledObject.Name", scaledObjectName, "scaler", scalerName, "metricName", metricSpec.External.Metric.Name, "metrics", metrics, "scalerError", err)
}
metrics, err = fallback.GetMetricsWithFallback(ctx, h.client, h.logger, metrics, err, metricName, scaledObject, metricSpec)
if err != nil {
scalerError = true
h.logger.Error(err, "error getting metric for scaler", "scaledObject.Namespace", scaledObjectNamespace, "scaledObject.Name", scaledObjectName, "scaler", scalerName)
} else {
for _, metric := range metrics {
metricValue := metric.Value.AsApproximateFloat64()
prommetrics.RecordScalerMetric(scaledObjectNamespace, scaledObjectName, scalerName, scalerIndex, metric.MetricName, metricValue)
// [DEPRECATED] handle exporting Prometheus metrics from Operator to Metrics Server
scalerMetricMsg := metricsserviceapi.ScalerMetricMsg{
ScalerName: scalerName,
ScalerIndex: int32(scalerIndex),
MetricName: metricName,
MetricValue: float32(metricValue),
}
exportedPromMetrics.ScalerMetric = append(exportedPromMetrics.ScalerMetric, &scalerMetricMsg)
}
matchingMetrics = append(matchingMetrics, metrics...)
}
prommetrics.RecordScalerError(scaledObjectNamespace, scaledObjectName, scalerName, scalerIndex, metricName, err)
// [DEPRECATED] handle exporting Prometheus metrics from Operator to Metrics Server
scalerErrMsg := metricsserviceapi.ScalerErrorMsg{
ScalerName: scalerName,
ScalerIndex: int32(scalerIndex),
MetricName: metricName,
Error: (err != nil),
}
exportedPromMetrics.ScalerError = append(exportedPromMetrics.ScalerError, &scalerErrMsg)
}
}
}
// invalidate the cache for the ScaledObject, if we hit an error in any scaler
// in this case we try to build all scalers (and resolve all secrets/creds) again in the next call
if scalerError {
err := h.ClearScalersCache(ctx, scaledObject)
if err != nil {
h.logger.Error(err, "error clearing scalers cache")
}
h.logger.V(1).Info("scaler error encountered, clearing scaler cache")
}
if len(matchingMetrics) == 0 {
return nil, &exportedPromMetrics, fmt.Errorf("no matching metrics found for " + metricName)
}
return &external_metrics.ExternalMetricValueList{
Items: matchingMetrics,
}, &exportedPromMetrics, nil
}
// buildScalers returns list of Scalers for the specified triggers
func (h *scaleHandler) buildScalers(ctx context.Context, withTriggers *kedav1alpha1.WithTriggers, podTemplateSpec *corev1.PodTemplateSpec, containerName string) ([]cache.ScalerBuilder, error) {
logger := h.logger.WithValues("type", withTriggers.Kind, "namespace", withTriggers.Namespace, "name", withTriggers.Name)
var err error
resolvedEnv := make(map[string]string)
result := make([]cache.ScalerBuilder, 0, len(withTriggers.Spec.Triggers))
for i, t := range withTriggers.Spec.Triggers {
triggerIndex, trigger := i, t
factory := func() (scalers.Scaler, *scalers.ScalerConfig, error) {
if podTemplateSpec != nil {
resolvedEnv, err = resolver.ResolveContainerEnv(ctx, h.client, logger, &podTemplateSpec.Spec, containerName, withTriggers.Namespace, h.secretsLister)
if err != nil {
return nil, nil, fmt.Errorf("error resolving secrets for ScaleTarget: %s", err)
}
}
config := &scalers.ScalerConfig{
ScalableObjectName: withTriggers.Name,
ScalableObjectNamespace: withTriggers.Namespace,
ScalableObjectType: withTriggers.Kind,
TriggerName: trigger.Name,
TriggerMetadata: trigger.Metadata,
TriggerUseCachedMetrics: trigger.UseCachedMetrics,
ResolvedEnv: resolvedEnv,
AuthParams: make(map[string]string),
GlobalHTTPTimeout: h.globalHTTPTimeout,
ScalerIndex: triggerIndex,
MetricType: trigger.MetricType,
}
authParams, podIdentity, err := resolver.ResolveAuthRefAndPodIdentity(ctx, h.client, logger, trigger.AuthenticationRef, podTemplateSpec, withTriggers.Namespace, h.secretsLister)
if err != nil {
return nil, nil, err
}
config.AuthParams = authParams
config.PodIdentity = podIdentity
scaler, err := buildScaler(ctx, h.client, trigger.Type, config)
return scaler, config, err
}
scaler, config, err := factory()
if err != nil {
h.recorder.Event(withTriggers, corev1.EventTypeWarning, eventreason.KEDAScalerFailed, err.Error())
h.logger.Error(err, "error resolving auth params", "scalerIndex", triggerIndex, "object", withTriggers)
if scaler != nil {
scaler.Close(ctx)
}
for _, builder := range result {
builder.Scaler.Close(ctx)
}
return nil, err
}
result = append(result, cache.ScalerBuilder{
Scaler: scaler,
ScalerConfig: *config,
Factory: factory,
})
}
return result, nil
}
func buildScaler(ctx context.Context, client client.Client, triggerType string, config *scalers.ScalerConfig) (scalers.Scaler, error) {
// TRIGGERS-START
switch triggerType {
case "activemq":
return scalers.NewActiveMQScaler(config)
case "artemis-queue":
return scalers.NewArtemisQueueScaler(config)
case "aws-cloudwatch":
return scalers.NewAwsCloudwatchScaler(config)
case "aws-dynamodb":
return scalers.NewAwsDynamoDBScaler(config)
case "aws-dynamodb-streams":
return scalers.NewAwsDynamoDBStreamsScaler(ctx, config)
case "aws-kinesis-stream":
return scalers.NewAwsKinesisStreamScaler(config)
case "aws-sqs-queue":
return scalers.NewAwsSqsQueueScaler(config)
case "azure-app-insights":
return scalers.NewAzureAppInsightsScaler(config)
case "azure-blob":
return scalers.NewAzureBlobScaler(config)
case "azure-data-explorer":
return scalers.NewAzureDataExplorerScaler(ctx, config)
case "azure-eventhub":
return scalers.NewAzureEventHubScaler(ctx, config)
case "azure-log-analytics":
return scalers.NewAzureLogAnalyticsScaler(config)
case "azure-monitor":
return scalers.NewAzureMonitorScaler(config)
case "azure-pipelines":
return scalers.NewAzurePipelinesScaler(ctx, config)
case "azure-queue":
return scalers.NewAzureQueueScaler(config)
case "azure-servicebus":
return scalers.NewAzureServiceBusScaler(ctx, config)
case "cassandra":
return scalers.NewCassandraScaler(config)
case "couchdb":
return scalers.NewCouchDBScaler(ctx, config)
case "cpu":
return scalers.NewCPUMemoryScaler(corev1.ResourceCPU, config)
case "cron":
return scalers.NewCronScaler(config)
case "datadog":
return scalers.NewDatadogScaler(ctx, config)
case "elasticsearch":
return scalers.NewElasticsearchScaler(config)
case "etcd":
return scalers.NewEtcdScaler(config)
case "external":
return scalers.NewExternalScaler(config)
// TODO: use other way for test.
case "external-mock":
return scalers.NewExternalMockScaler(config)
case "external-push":
return scalers.NewExternalPushScaler(config)
case "gcp-pubsub":
return scalers.NewPubSubScaler(config)
case "gcp-stackdriver":
return scalers.NewStackdriverScaler(ctx, config)
case "gcp-storage":
return scalers.NewGcsScaler(config)
case "graphite":
return scalers.NewGraphiteScaler(config)
case "huawei-cloudeye":
return scalers.NewHuaweiCloudeyeScaler(config)
case "ibmmq":
return scalers.NewIBMMQScaler(config)
case "influxdb":
return scalers.NewInfluxDBScaler(config)
case "kafka":
return scalers.NewKafkaScaler(config)
case "kubernetes-workload":
return scalers.NewKubernetesWorkloadScaler(client, config)
case "liiklus":
return scalers.NewLiiklusScaler(config)
case "loki":
return scalers.NewLokiScaler(config)
case "memory":
return scalers.NewCPUMemoryScaler(corev1.ResourceMemory, config)
case "metrics-api":
return scalers.NewMetricsAPIScaler(config)
case "mongodb":
return scalers.NewMongoDBScaler(ctx, config)
case "mssql":
return scalers.NewMSSQLScaler(config)
case "mysql":
return scalers.NewMySQLScaler(config)
case "nats-jetstream":
return scalers.NewNATSJetStreamScaler(config)
case "new-relic":
return scalers.NewNewRelicScaler(config)
case "openstack-metric":
return scalers.NewOpenstackMetricScaler(ctx, config)
case "openstack-swift":
return scalers.NewOpenstackSwiftScaler(ctx, config)
case "postgresql":
return scalers.NewPostgreSQLScaler(config)
case "predictkube":
return scalers.NewPredictKubeScaler(ctx, config)
case "prometheus":
return scalers.NewPrometheusScaler(config)
case "pulsar":
return scalers.NewPulsarScaler(config)
case "rabbitmq":
return scalers.NewRabbitMQScaler(config)
case "redis":
return scalers.NewRedisScaler(ctx, false, false, config)
case "redis-cluster":
return scalers.NewRedisScaler(ctx, true, false, config)
case "redis-cluster-streams":
return scalers.NewRedisStreamsScaler(ctx, true, false, config)
case "redis-sentinel":
return scalers.NewRedisScaler(ctx, false, true, config)
case "redis-sentinel-streams":
return scalers.NewRedisStreamsScaler(ctx, false, true, config)
case "redis-streams":
return scalers.NewRedisStreamsScaler(ctx, false, false, config)
case "selenium-grid":
return scalers.NewSeleniumGridScaler(config)
case "solace-event-queue":
return scalers.NewSolaceScaler(config)
case "stan":
return scalers.NewStanScaler(config)
default:
return nil, fmt.Errorf("no scaler found for type: %s", triggerType)
}
// TRIGGERS-END
}