-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
scale_handler.go
547 lines (487 loc) · 18 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
package scaling
import (
"context"
"fmt"
"sync"
"time"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/autoscaling/v2beta2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/scale"
"knative.dev/pkg/apis/duck"
duckv1 "knative.dev/pkg/apis/duck/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
kedav1alpha1 "github.com/kedacore/keda/v2/api/v1alpha1"
"github.com/kedacore/keda/v2/pkg/scalers"
"github.com/kedacore/keda/v2/pkg/scaling/executor"
"github.com/kedacore/keda/v2/pkg/scaling/resolver"
)
const (
// Default polling interval for a ScaledObject triggers if no pollingInterval is defined.
defaultPollingInterval = 30
)
// ScaleHandler encapsulates the logic of calling the right scalers for
// each ScaledObject and making the final scale decision and operation
type ScaleHandler interface {
HandleScalableObject(scalableObject interface{}) error
DeleteScalableObject(scalableObject interface{}) error
GetScalers(scalableObject interface{}) ([]scalers.Scaler, error)
}
type scaleHandler struct {
client client.Client
logger logr.Logger
scaleLoopContexts *sync.Map
scaleExecutor executor.ScaleExecutor
globalHTTPTimeout time.Duration
}
// NewScaleHandler creates a ScaleHandler object
func NewScaleHandler(client client.Client, scaleClient *scale.ScalesGetter, reconcilerScheme *runtime.Scheme, globalHTTPTimeout time.Duration) ScaleHandler {
return &scaleHandler{
client: client,
logger: logf.Log.WithName("scalehandler"),
scaleLoopContexts: &sync.Map{},
scaleExecutor: executor.NewScaleExecutor(client, scaleClient, reconcilerScheme),
globalHTTPTimeout: globalHTTPTimeout,
}
}
func (h *scaleHandler) GetScalers(scalableObject interface{}) ([]scalers.Scaler, error) {
withTriggers, err := asDuckWithTriggers(scalableObject)
if err != nil {
return nil, err
}
podTemplateSpec, containerName, err := h.getPods(scalableObject)
if err != nil {
return nil, err
}
return h.buildScalers(withTriggers, podTemplateSpec, containerName)
}
func (h *scaleHandler) HandleScalableObject(scalableObject interface{}) error {
withTriggers, err := asDuckWithTriggers(scalableObject)
if err != nil {
h.logger.Error(err, "error duck typing object into withTrigger")
return err
}
key := generateKey(withTriggers)
ctx, cancel := context.WithCancel(context.TODO())
// 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)
}
// a mutex is used to synchronize scale requests per scalableObject
scalingMutex := &sync.Mutex{}
go h.startPushScalers(ctx, withTriggers, scalableObject, scalingMutex)
go h.startScaleLoop(ctx, withTriggers, scalableObject, scalingMutex)
return nil
}
func (h *scaleHandler) DeleteScalableObject(scalableObject interface{}) error {
withTriggers, err := asDuckWithTriggers(scalableObject)
if err != nil {
h.logger.Error(err, "error duck typing object into withTrigger")
return err
}
key := generateKey(withTriggers)
result, ok := h.scaleLoopContexts.Load(key)
if ok {
cancel, ok := result.(context.CancelFunc)
if ok {
cancel()
}
h.scaleLoopContexts.Delete(key)
} else {
h.logger.V(1).Info("ScaleObject 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)
// kick off one check to the scalers now
h.checkScalers(ctx, scalableObject, scalingMutex)
pollingInterval := getPollingInterval(withTriggers)
logger.V(1).Info("Watching with pollingInterval", "PollingInterval", pollingInterval)
for {
select {
case <-time.After(pollingInterval):
h.checkScalers(ctx, scalableObject, scalingMutex)
case <-ctx.Done():
logger.V(1).Info("Context canceled")
return
}
}
}
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)
ss, err := h.GetScalers(scalableObject)
if err != nil {
logger.Error(err, "Error getting scalers", "object", scalableObject)
return
}
for _, s := range ss {
scaler, ok := s.(scalers.PushScaler)
if !ok {
continue
}
go func() {
activeCh := make(chan bool)
go scaler.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)
case *kedav1alpha1.ScaledJob:
h.logger.Info("Warning: External Push Scaler does not support ScaledJob", "object", scalableObject)
}
scalingMutex.Unlock()
}
}
}()
}
}
// 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) {
scalers, err := h.GetScalers(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:
h.scaleExecutor.RequestScale(ctx, obj, h.checkScaledObjectScalers(ctx, scalers))
case *kedav1alpha1.ScaledJob:
scaledJob := scalableObject.(*kedav1alpha1.ScaledJob)
isActive, scaleTo, maxScale := h.checkScaledJobScalers(ctx, scalers, scaledJob)
h.scaleExecutor.RequestJobScale(ctx, obj, isActive, scaleTo, maxScale)
}
}
func (h *scaleHandler) checkScaledObjectScalers(ctx context.Context, scalers []scalers.Scaler) bool {
isActive := false
for i, scaler := range scalers {
isTriggerActive, err := scaler.IsActive(ctx)
scaler.Close()
if err != nil {
h.logger.V(1).Info("Error getting scale decision", "Error", err)
continue
} else if isTriggerActive {
isActive = true
if scaler.GetMetricSpecForScaling()[0].External != nil {
h.logger.V(1).Info("Scaler for scaledObject is active", "Metrics Name", scaler.GetMetricSpecForScaling()[0].External.Metric.Name)
}
if scaler.GetMetricSpecForScaling()[0].Resource != nil {
h.logger.V(1).Info("Scaler for scaledObject is active", "Metrics Name", scaler.GetMetricSpecForScaling()[0].Resource.Name)
}
closeScalers(scalers[i+1:])
break
}
}
return isActive
}
func (h *scaleHandler) checkScaledJobScalers(ctx context.Context, scalers []scalers.Scaler, scaledJob *kedav1alpha1.ScaledJob) (bool, int64, int64) {
var queueLength int64
var targetAverageValue int64
var maxValue int64
isActive := false
for _, scaler := range scalers {
scalerLogger := h.logger.WithValues("Scaler", scaler)
metricSpecs := scaler.GetMetricSpecForScaling()
// skip cpu/memory resource scaler
if metricSpecs[0].External == nil {
continue
}
isTriggerActive, err := scaler.IsActive(ctx)
scalerLogger.Info("Active trigger", "isTriggerActive", isTriggerActive)
targetAverageValue = getTargetAverageValue(metricSpecs)
scalerLogger.Info("Scaler targetAverageValue", "targetAverageValue", targetAverageValue)
metrics, _ := scaler.GetMetrics(ctx, "queueLength", nil)
var metricValue int64
for _, m := range metrics {
if m.MetricName == "queueLength" {
metricValue, _ = m.Value.AsInt64()
queueLength += metricValue
}
}
scalerLogger.Info("QueueLength Metric value", "queueLength", queueLength)
scaler.Close()
if err != nil {
scalerLogger.V(1).Info("Error getting scale decision, but continue", "Error", err)
continue
} else if isTriggerActive {
isActive = true
scalerLogger.Info("Scaler is active")
}
}
if targetAverageValue != 0 {
maxValue = min(scaledJob.MaxReplicaCount(), devideWithCeil(queueLength, targetAverageValue))
}
h.logger.Info("Scaler maxValue", "maxValue", maxValue)
return isActive, queueLength, maxValue
}
func getTargetAverageValue(metricSpecs []v2beta2.MetricSpec) int64 {
var targetAverageValue int64
var metricValue int64
var flag bool
for _, metric := range metricSpecs {
if metric.External.Target.AverageValue == nil {
metricValue = 0
} else {
metricValue, flag = metric.External.Target.AverageValue.AsInt64()
if !flag {
metricValue = 0
}
}
targetAverageValue += metricValue
}
count := int64(len(metricSpecs))
if count != 0 {
return targetAverageValue / count
}
return 0
}
func devideWithCeil(x, y int64) int64 {
ans := x / y
reminder := x % y
if reminder != 0 {
return ans + 1
}
return ans
}
// Min function for int64
func min(x, y int64) int64 {
if x > y {
return y
}
return x
}
// buildScalers returns list of Scalers for the specified triggers
func (h *scaleHandler) buildScalers(withTriggers *kedav1alpha1.WithTriggers, podTemplateSpec *corev1.PodTemplateSpec, containerName string) ([]scalers.Scaler, error) {
logger := h.logger.WithValues("type", withTriggers.Kind, "namespace", withTriggers.Namespace, "name", withTriggers.Name)
var scalersRes []scalers.Scaler
var err error
resolvedEnv := make(map[string]string)
if podTemplateSpec != nil {
resolvedEnv, err = resolver.ResolveContainerEnv(h.client, logger, &podTemplateSpec.Spec, containerName, withTriggers.Namespace)
if err != nil {
return scalersRes, fmt.Errorf("error resolving secrets for ScaleTarget: %s", err)
}
}
for i, trigger := range withTriggers.Spec.Triggers {
config := &scalers.ScalerConfig{
Name: withTriggers.Name,
Namespace: withTriggers.Namespace,
TriggerMetadata: trigger.Metadata,
ResolvedEnv: resolvedEnv,
AuthParams: make(map[string]string),
GlobalHTTPTimeout: h.globalHTTPTimeout,
}
if podTemplateSpec != nil {
authParams, podIdentity := resolver.ResolveAuthRef(h.client, logger, trigger.AuthenticationRef, &podTemplateSpec.Spec, withTriggers.Namespace)
if podIdentity == kedav1alpha1.PodIdentityProviderAwsEKS {
serviceAccountName := podTemplateSpec.Spec.ServiceAccountName
serviceAccount := &corev1.ServiceAccount{}
err = h.client.Get(context.TODO(), types.NamespacedName{Name: serviceAccountName, Namespace: withTriggers.Namespace}, serviceAccount)
if err != nil {
closeScalers(scalersRes)
return []scalers.Scaler{}, fmt.Errorf("error getting service account: %s", err)
}
authParams["awsRoleArn"] = serviceAccount.Annotations[kedav1alpha1.PodIdentityAnnotationEKS]
} else if podIdentity == kedav1alpha1.PodIdentityProviderAwsKiam {
authParams["awsRoleArn"] = podTemplateSpec.ObjectMeta.Annotations[kedav1alpha1.PodIdentityAnnotationKiam]
}
config.AuthParams = authParams
config.PodIdentity = podIdentity
} else {
authParams, _ := resolver.ResolveAuthRef(h.client, logger, trigger.AuthenticationRef, nil, withTriggers.Namespace)
config.AuthParams = authParams
}
scaler, err := buildScaler(trigger.Type, config)
if err != nil {
closeScalers(scalersRes)
return []scalers.Scaler{}, fmt.Errorf("error getting scaler for trigger #%d: %s", i, err)
}
scalersRes = append(scalersRes, scaler)
}
return scalersRes, nil
}
func (h *scaleHandler) getPods(scalableObject interface{}) (*corev1.PodTemplateSpec, string, error) {
switch obj := scalableObject.(type) {
case *kedav1alpha1.ScaledObject:
// Try to get a real object instance for better cache usage, but fall back to an Unstructured if needed.
podTemplateSpec := corev1.PodTemplateSpec{}
gvk := obj.Status.ScaleTargetGVKR.GroupVersionKind()
objKey := client.ObjectKey{Namespace: obj.Namespace, Name: obj.Spec.ScaleTargetRef.Name}
switch {
// For core types, use a typed client so we get an informer-cache-backed Get to reduce API load.
case gvk.Group == "apps" && gvk.Kind == "Deployment":
deployment := &appsv1.Deployment{}
if err := h.client.Get(context.TODO(), objKey, deployment); err != nil {
// resource doesn't exist
h.logger.Error(err, "Target deployment doesn't exist", "resource", gvk.String(), "name", objKey.Name)
return nil, "", err
}
podTemplateSpec.ObjectMeta = deployment.ObjectMeta
podTemplateSpec.Spec = deployment.Spec.Template.Spec
case gvk.Group == "apps" && gvk.Kind == "StatefulSet":
statefulSet := &appsv1.StatefulSet{}
if err := h.client.Get(context.TODO(), objKey, statefulSet); err != nil {
// resource doesn't exist
h.logger.Error(err, "Target deployment doesn't exist", "resource", gvk.String(), "name", objKey.Name)
return nil, "", err
}
podTemplateSpec.ObjectMeta = statefulSet.ObjectMeta
podTemplateSpec.Spec = statefulSet.Spec.Template.Spec
default:
unstruct := &unstructured.Unstructured{}
unstruct.SetGroupVersionKind(gvk)
if err := h.client.Get(context.TODO(), objKey, unstruct); err != nil {
// resource doesn't exist
h.logger.Error(err, "Target resource doesn't exist", "resource", gvk.String(), "name", objKey.Name)
return nil, "", err
}
withPods := &duckv1.WithPod{}
if err := duck.FromUnstructured(unstruct, withPods); err != nil {
h.logger.Error(err, "Cannot convert Unstructured into PodSpecable Duck-type", "object", unstruct)
}
podTemplateSpec.ObjectMeta = withPods.ObjectMeta
podTemplateSpec.Spec = withPods.Spec.Template.Spec
}
if podTemplateSpec.Spec.Containers == nil || len(podTemplateSpec.Spec.Containers) == 0 {
h.logger.V(1).Info("There aren't any containers found in the ScaleTarget, therefore it is no possible to inject environment properties", "resource", gvk.String(), "name", obj.Spec.ScaleTargetRef.Name)
return nil, "", nil
}
return &podTemplateSpec, obj.Spec.ScaleTargetRef.EnvSourceContainerName, nil
case *kedav1alpha1.ScaledJob:
return &obj.Spec.JobTargetRef.Template, obj.Spec.EnvSourceContainerName, nil
default:
return nil, "", fmt.Errorf("unknown scalable object type %v", scalableObject)
}
}
func buildScaler(triggerType string, config *scalers.ScalerConfig) (scalers.Scaler, error) {
// TRIGGERS-START
switch triggerType {
case "artemis-queue":
return scalers.NewArtemisQueueScaler(config)
case "aws-cloudwatch":
return scalers.NewAwsCloudwatchScaler(config)
case "aws-kinesis-stream":
return scalers.NewAwsKinesisStreamScaler(config)
case "aws-sqs-queue":
return scalers.NewAwsSqsQueueScaler(config)
case "azure-blob":
return scalers.NewAzureBlobScaler(config)
case "azure-eventhub":
return scalers.NewAzureEventHubScaler(config)
case "azure-log-analytics":
return scalers.NewAzureLogAnalyticsScaler(config)
case "azure-monitor":
return scalers.NewAzureMonitorScaler(config)
case "azure-queue":
return scalers.NewAzureQueueScaler(config)
case "azure-servicebus":
return scalers.NewAzureServiceBusScaler(config)
case "cpu":
return scalers.NewCPUMemoryScaler(corev1.ResourceCPU, config)
case "cron":
return scalers.NewCronScaler(config)
case "external":
return scalers.NewExternalScaler(config)
case "external-push":
return scalers.NewExternalPushScaler(config)
case "gcp-pubsub":
return scalers.NewPubSubScaler(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 "liiklus":
return scalers.NewLiiklusScaler(config)
case "memory":
return scalers.NewCPUMemoryScaler(corev1.ResourceMemory, config)
case "metrics-api":
return scalers.NewMetricsAPIScaler(config)
case "mongodb":
return scalers.NewMongoDBScaler(config)
case "mysql":
return scalers.NewMySQLScaler(config)
case "openstack-swift":
return scalers.NewOpenstackSwiftScaler(config)
case "postgresql":
return scalers.NewPostgreSQLScaler(config)
case "prometheus":
return scalers.NewPrometheusScaler(config)
case "rabbitmq":
return scalers.NewRabbitMQScaler(config)
case "redis":
return scalers.NewRedisScaler(false, config)
case "redis-cluster":
return scalers.NewRedisScaler(true, config)
case "redis-cluster-streams":
return scalers.NewRedisStreamsScaler(true, config)
case "redis-streams":
return scalers.NewRedisStreamsScaler(false, config)
case "stan":
return scalers.NewStanScaler(config)
default:
return nil, fmt.Errorf("no scaler found for type: %s", triggerType)
}
// TRIGGERS-END
}
func asDuckWithTriggers(scalableObject interface{}) (*kedav1alpha1.WithTriggers, error) {
switch obj := scalableObject.(type) {
case *kedav1alpha1.ScaledObject:
return &kedav1alpha1.WithTriggers{
TypeMeta: obj.TypeMeta,
ObjectMeta: obj.ObjectMeta,
Spec: kedav1alpha1.WithTriggersSpec{
PollingInterval: obj.Spec.PollingInterval,
Triggers: obj.Spec.Triggers,
},
}, nil
case *kedav1alpha1.ScaledJob:
return &kedav1alpha1.WithTriggers{
TypeMeta: obj.TypeMeta,
ObjectMeta: obj.ObjectMeta,
Spec: kedav1alpha1.WithTriggersSpec{
PollingInterval: obj.Spec.PollingInterval,
Triggers: obj.Spec.Triggers,
},
}, nil
default:
// here could be the conversion from unknown Duck type potentially in the future
return nil, fmt.Errorf("unknown scalable object type %v", scalableObject)
}
}
func closeScalers(scalers []scalers.Scaler) {
for _, scaler := range scalers {
defer scaler.Close()
}
}
func getPollingInterval(withTriggers *kedav1alpha1.WithTriggers) time.Duration {
if withTriggers.Spec.PollingInterval != nil {
return time.Second * time.Duration(*withTriggers.Spec.PollingInterval)
}
return time.Second * time.Duration(defaultPollingInterval)
}
func generateKey(scalableObject *kedav1alpha1.WithTriggers) string {
return fmt.Sprintf("%s.%s.%s", scalableObject.Kind, scalableObject.Namespace, scalableObject.Name)
}