-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathautoscaler_watcher_test.go
More file actions
689 lines (614 loc) · 21.3 KB
/
Copy pathautoscaler_watcher_test.go
File metadata and controls
689 lines (614 loc) · 21.3 KB
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build kubeapiserver
package externalmetrics
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
autoscaler "k8s.io/api/autoscaling/v2beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
dynamic_informer "k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/dynamic/fake"
kube_informer "k8s.io/client-go/informers"
kube_fake "k8s.io/client-go/kubernetes/fake"
"github.com/DataDog/watermarkpodautoscaler/apis/datadoghq/v1alpha1"
"github.com/DataDog/datadog-agent/pkg/clusteragent/autoscaling/externalmetrics/model"
)
const (
autoscalingGroup = "autoscaling"
hpaResource = "horizontalpodautoscalers"
)
func init() {
autoscaler.AddToScheme(scheme)
v1alpha1.AddToScheme(scheme)
}
// Test fixture
type autoscalerFixture struct {
t *testing.T
// Objects to put in the store.
hpaLister []*autoscaler.HorizontalPodAutoscaler
wpaLister []*unstructured.Unstructured
// Objects from here preloaded into fake clients.
kubeObjects []runtime.Object
wpaObjects []runtime.Object
// Local store.
store DatadogMetricsInternalStore
}
func newAutoscalerFixture(t *testing.T) *autoscalerFixture {
return &autoscalerFixture{
t: t,
kubeObjects: []runtime.Object{},
wpaObjects: []runtime.Object{},
store: NewDatadogMetricsInternalStore(),
}
}
func (f *autoscalerFixture) newAutoscalerWatcher(selector labels.Selector) (*AutoscalerWatcher, kube_informer.SharedInformerFactory, dynamic_informer.DynamicSharedInformerFactory) {
for _, hpa := range f.hpaLister {
f.kubeObjects = append(f.kubeObjects, hpa)
}
kubeClient := kube_fake.NewSimpleClientset(f.kubeObjects...)
kubeClient.Resources = []*metav1.APIResourceList{
{
GroupVersion: fmt.Sprintf("%s/%s", autoscalingGroup, "v2beta1"),
APIResources: []metav1.APIResource{
{
Name: hpaResource,
Group: autoscalingGroup,
Version: "v2beta1",
},
},
},
}
kubeInformer := kube_informer.NewSharedInformerFactory(kubeClient, noResyncPeriodFunc())
for _, wpa := range f.wpaLister {
f.wpaObjects = append(f.wpaObjects, wpa)
}
wpaClient := fake.NewSimpleDynamicClient(scheme, f.wpaObjects...)
wpaInformer := dynamic_informer.NewDynamicSharedInformerFactory(wpaClient, noResyncPeriodFunc())
autoscalerWatcher, err := NewAutoscalerWatcher(0, true, 1, "default", selector, kubeClient, kubeInformer, wpaInformer, getIsLeaderFunction(true), &f.store)
if err != nil {
return nil, nil, nil
}
autoscalerWatcher.autoscalerListerSynced = alwaysReady
autoscalerWatcher.wpaListerSynced = alwaysReady
for _, hpa := range f.hpaLister {
kubeInformer.Autoscaling().V2beta1().HorizontalPodAutoscalers().Informer().GetIndexer().Add(hpa)
}
for _, wpa := range f.wpaLister {
wpaInformer.ForResource(gvr).Informer().GetIndexer().Add(wpa)
}
return autoscalerWatcher, kubeInformer, wpaInformer
}
func (f *autoscalerFixture) runWatcherUpdate() {
autoscalerWatcher, kubeInformer, wpaInformer := f.newAutoscalerWatcher(nil)
stopCh := make(chan struct{})
defer close(stopCh)
kubeInformer.Start(stopCh)
wpaInformer.Start(stopCh)
autoscalerWatcher.processAutoscalers()
}
func newFakeHorizontalPodAutoscaler(ns, name string, metrics []autoscaler.MetricSpec) *autoscaler.HorizontalPodAutoscaler {
return newFakeHorizontalPodAutoscalerWithLabels(ns, name, nil, metrics)
}
func newFakeHorizontalPodAutoscalerWithLabels(ns, name string, hpaLabels map[string]string, metrics []autoscaler.MetricSpec) *autoscaler.HorizontalPodAutoscaler {
return &autoscaler.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
Labels: hpaLabels,
},
Spec: autoscaler.HorizontalPodAutoscalerSpec{
Metrics: metrics,
},
}
}
func newFakeWatermarkPodAutoscaler(ns, name string, metrics []interface{}) *unstructured.Unstructured {
return newFakeWatermarkPodAutoscalerWithLabels(ns, name, nil, metrics)
}
func newFakeWatermarkPodAutoscalerWithLabels(ns, name string, wpaLabels map[string]string, metrics []interface{}) *unstructured.Unstructured {
labelsMap := map[string]interface{}{}
for k, v := range wpaLabels {
labelsMap[k] = v
}
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "datadoghq.com/v1alpha1",
"kind": "WatermarkPodAutoscaler",
"metadata": map[string]interface{}{
"name": name,
"namespace": ns,
"labels": labelsMap,
},
"spec": map[string]interface{}{
"metrics": metrics,
},
},
}
}
func TestUpdateAutoscalerReferences(t *testing.T) {
f := newAutoscalerFixture(t)
updateTime := time.Now()
f.hpaLister = []*autoscaler.HorizontalPodAutoscaler{
newFakeHorizontalPodAutoscaler("ns0", "hpa0", []autoscaler.MetricSpec{
{
Type: autoscaler.ExternalMetricSourceType,
External: &autoscaler.ExternalMetricSource{
MetricName: "datadogmetric@dd-metric-0",
},
},
}),
newFakeHorizontalPodAutoscaler("ns1", "hpa1", []autoscaler.MetricSpec{
{
Type: autoscaler.ResourceMetricSourceType,
},
}),
}
f.wpaLister = []*unstructured.Unstructured{
newFakeWatermarkPodAutoscaler("ns0", "wpa0", []interface{}{
map[string]interface{}{
"external": map[string]interface{}{
"metricName": "datadogmetric@dd-metric-1",
},
"type": "External",
},
}),
}
ddm := model.DatadogMetricInternal{
ID: "ns0/dd-metric-0",
Active: false,
Valid: true,
Value: 10.0,
UpdateTime: updateTime,
Error: nil,
}
ddm.SetQueries("metric query0")
f.store.Set("ns0/dd-metric-0", ddm, "utest")
ddm = model.DatadogMetricInternal{
ID: "ns0/dd-metric-1",
Active: true,
Valid: true,
Value: 11.0,
UpdateTime: updateTime,
Error: nil,
}
ddm.SetQueries("metric query1")
f.store.Set("ns0/dd-metric-1", ddm, "utest")
ddm = model.DatadogMetricInternal{
ID: "default/dd-metric-2",
Active: true,
Valid: true,
Value: 12.0,
UpdateTime: updateTime,
AutoscalerReferences: "hpa:ns1/hpa1",
Error: nil,
}
ddm.SetQueries("metric query2")
f.store.Set("default/dd-metric-2", ddm, "utest")
f.runWatcherUpdate()
// Check internal store content
assert.Equal(t, 3, f.store.Count())
ddm = model.DatadogMetricInternal{
ID: "ns0/dd-metric-0",
Active: true,
Valid: true,
Value: 10.0,
UpdateTime: updateTime,
Error: nil,
AutoscalerReferences: "hpa:ns0/hpa0",
}
ddm.SetQueries("metric query0")
compareDatadogMetricInternal(t, &ddm, f.store.Get("ns0/dd-metric-0"))
ddm = model.DatadogMetricInternal{
ID: "ns0/dd-metric-1",
Active: true,
Valid: true,
Value: 11.0,
UpdateTime: updateTime,
Error: nil,
AutoscalerReferences: "wpa:ns0/wpa0",
}
ddm.SetQueries("metric query1")
compareDatadogMetricInternal(t, &ddm, f.store.Get("ns0/dd-metric-1"))
ddm = model.DatadogMetricInternal{
ID: "default/dd-metric-2",
Active: false,
Valid: false,
Value: 12.0,
UpdateTime: updateTime,
Error: nil,
AutoscalerReferences: "",
}
ddm.SetQueries("metric query2")
compareDatadogMetricInternal(t, &ddm, f.store.Get("default/dd-metric-2"))
}
// The namespace part of a `datadogmetric@<namespace>:<name>` reference is ignored, the DatadogMetric
// is always resolved in the namespace of the autoscaler, so a reference to another namespace never
// activates the DatadogMetric it points to.
func TestAutoscalerWatcherIgnoresDatadogMetricReferenceNamespace(t *testing.T) {
f := newAutoscalerFixture(t)
updateTime := time.Now()
f.hpaLister = []*autoscaler.HorizontalPodAutoscaler{
newFakeHorizontalPodAutoscaler("tenant-a", "hpa0", []autoscaler.MetricSpec{
{
Type: autoscaler.ExternalMetricSourceType,
External: &autoscaler.ExternalMetricSource{
MetricName: "datadogmetric@shared:dd-metric-0",
},
},
}),
}
f.wpaLister = []*unstructured.Unstructured{
newFakeWatermarkPodAutoscaler("tenant-b", "wpa0", []interface{}{
map[string]interface{}{
"external": map[string]interface{}{
"metricName": "datadogmetric@shared:dd-metric-0",
},
"type": "External",
},
}),
}
ddm := model.DatadogMetricInternal{
ID: "shared/dd-metric-0",
Active: false,
Valid: true,
Value: 10.0,
UpdateTime: updateTime,
Error: nil,
}
ddm.SetQueries("metric query0")
f.store.Set("shared/dd-metric-0", ddm, "utest")
f.runWatcherUpdate()
assert.Equal(t, 1, f.store.Count())
ddm = model.DatadogMetricInternal{
ID: "shared/dd-metric-0",
Active: false,
Valid: true,
Value: 10.0,
UpdateTime: updateTime,
Error: nil,
AutoscalerReferences: "",
}
ddm.SetQueries("metric query0")
compareDatadogMetricInternal(t, &ddm, f.store.Get("shared/dd-metric-0"))
}
func TestCreateAutogenDatadogMetrics(t *testing.T) {
f := newAutoscalerFixture(t)
updateTime := time.Now()
f.hpaLister = []*autoscaler.HorizontalPodAutoscaler{
newFakeHorizontalPodAutoscaler("ns0", "hpa0", []autoscaler.MetricSpec{
{
Type: autoscaler.ExternalMetricSourceType,
External: &autoscaler.ExternalMetricSource{
MetricName: "docker.cpu.usage",
MetricSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"foo": "bar",
},
},
},
},
}),
newFakeHorizontalPodAutoscaler("ns0", "hpa1", []autoscaler.MetricSpec{
{
Type: autoscaler.ExternalMetricSourceType,
External: &autoscaler.ExternalMetricSource{
MetricName: "datadogmetric@ns0:donotexist",
},
},
}),
}
f.wpaLister = []*unstructured.Unstructured{
newFakeWatermarkPodAutoscaler("ns0", "wpa0", []interface{}{
map[string]interface{}{
"external": map[string]interface{}{
"metricName": "docker.cpu.usage",
"metricSelector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"bar": "foo",
},
},
},
"type": "External",
},
}),
}
ddm := model.DatadogMetricInternal{
ID: "default/dd-metric-0",
Active: true,
Valid: true,
Value: 10.0,
UpdateTime: updateTime,
Error: nil,
}
ddm.SetQuery("metric query0")
f.store.Set("default/dd-metric-0", ddm, "utest")
f.runWatcherUpdate()
// Check internal store content
assert.Equal(t, 3, f.store.Count())
ddm = model.DatadogMetricInternal{
ID: "default/dd-metric-0",
Active: false,
Valid: false,
Value: 10.0,
UpdateTime: updateTime,
Error: nil,
}
ddm.SetQuery("metric query0")
compareDatadogMetricInternal(t, &ddm, f.store.Get("default/dd-metric-0"))
ddm = model.DatadogMetricInternal{
ID: "default/dcaautogen-f311ac1e6b29e3723d1445645c43afd4340d22",
Active: true,
Valid: false,
Autogen: true,
ExternalMetricName: "docker.cpu.usage",
Value: 0.0,
UpdateTime: updateTime,
Error: nil,
AutoscalerReferences: "hpa:ns0/hpa0",
}
ddm.SetQuery("avg:docker.cpu.usage{foo:bar}.rollup(30)")
compareDatadogMetricInternal(t, &ddm, f.store.Get("default/dcaautogen-f311ac1e6b29e3723d1445645c43afd4340d22"))
ddm = model.DatadogMetricInternal{
ID: "default/dcaautogen-b6ea72b610c00aba6791b5eca1912e68dc7412",
Active: true,
Valid: false,
Autogen: true,
ExternalMetricName: "docker.cpu.usage",
Value: 0.0,
UpdateTime: updateTime,
Error: nil,
AutoscalerReferences: "wpa:ns0/wpa0",
}
ddm.SetQuery("avg:docker.cpu.usage{bar:foo}.rollup(30)")
compareDatadogMetricInternal(t, &ddm, f.store.Get("default/dcaautogen-b6ea72b610c00aba6791b5eca1912e68dc7412"))
}
func TestDisableDatadogMetricAutogen(t *testing.T) {
f := newAutoscalerFixture(t)
f.hpaLister = []*autoscaler.HorizontalPodAutoscaler{
newFakeHorizontalPodAutoscaler("ns0", "hpa0", []autoscaler.MetricSpec{
{
Type: autoscaler.ExternalMetricSourceType,
External: &autoscaler.ExternalMetricSource{
MetricName: "docker.cpu.usage",
MetricSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"foo": "bar",
},
},
},
},
}),
}
f.wpaLister = []*unstructured.Unstructured{
newFakeWatermarkPodAutoscaler("ns0", "wpa0", []interface{}{
map[string]interface{}{
"external": map[string]interface{}{
"metricName": "docker.cpu.usage",
"metricSelector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"bar": "foo",
},
},
},
"type": "External",
},
}),
}
autoscalerWatcher, kubeInformer, wpaInformer := f.newAutoscalerWatcher(nil)
autoscalerWatcher.autogenEnabled = false
stopCh := make(chan struct{})
defer close(stopCh)
kubeInformer.Start(stopCh)
wpaInformer.Start(stopCh)
autoscalerWatcher.processAutoscalers()
// The non-DatadogMetric HPA and WPA should not create any autogenerated
// DatadogMetrics; they should be ignored.
assert.Equal(t, 0, f.store.Count())
}
func TestCleanUpAutogenDatadogMetrics(t *testing.T) {
f := newAutoscalerFixture(t)
// AutogenExpirationPeriod is set to 1 hour in our unit tests
oldUpdateTime := time.Now().Add(time.Duration(-30) * time.Minute)
expiredUpdateTime := time.Now().Add(time.Duration(-90) * time.Minute)
// This DatadogMetric is expired, but it's not an autogen one - should not touch it
ddm := model.DatadogMetricInternal{
ID: "default/dd-metric-0",
Active: true,
Valid: true,
Value: 10.0,
UpdateTime: expiredUpdateTime,
Error: nil,
}
ddm.SetQueries("metric query0")
f.store.Set("default/dd-metric-0", ddm, "utest")
// HPA has been deleted but last update time was 30 minutes ago, we should keep it
ddm = model.DatadogMetricInternal{
ID: "default/dcaautogen-f311ac1e6b29e3723d1445645c43afd4340d22",
Active: true,
Valid: false,
Autogen: true,
ExternalMetricName: "docker.cpu.usage",
Deleted: false,
Value: 0.0,
UpdateTime: oldUpdateTime,
Error: nil,
}
ddm.SetQueries("avg:docker.cpu.usage{foo:bar}.rollup(30)")
f.store.Set("default/dcaautogen-f311ac1e6b29e3723d1445645c43afd4340d22", ddm, "utest")
// WPA has been deleted for 90 minutes, we should flag this as deleted
ddm = model.DatadogMetricInternal{
ID: "default/dcaautogen-b6ea72b610c00aba6791b5eca1912e68dc7412",
Active: true,
Valid: false,
Autogen: true,
ExternalMetricName: "docker.cpu.usage",
Deleted: true,
Value: 0.0,
UpdateTime: expiredUpdateTime,
Error: nil,
}
ddm.SetQueries("avg:docker.cpu.usage{bar:foo}.rollup(30)")
f.store.Set("default/dcaautogen-b6ea72b610c00aba6791b5eca1912e68dc7412", ddm, "utest")
f.runWatcherUpdate()
// Check internal store content
assert.Equal(t, 3, f.store.Count())
ddm = model.DatadogMetricInternal{
ID: "default/dd-metric-0",
Active: false,
Valid: false,
Deleted: false,
Value: 10.0,
UpdateTime: expiredUpdateTime,
Error: nil,
}
ddm.SetQueries("metric query0")
compareDatadogMetricInternal(t, &ddm, f.store.Get("default/dd-metric-0"))
ddm = model.DatadogMetricInternal{
ID: "default/dcaautogen-f311ac1e6b29e3723d1445645c43afd4340d22",
Active: false,
Valid: false,
Autogen: true,
ExternalMetricName: "docker.cpu.usage",
Deleted: false,
Value: 0.0,
UpdateTime: oldUpdateTime,
Error: nil,
}
ddm.SetQueries("avg:docker.cpu.usage{foo:bar}.rollup(30)")
compareDatadogMetricInternal(t, &ddm, f.store.Get("default/dcaautogen-f311ac1e6b29e3723d1445645c43afd4340d22"))
ddm = model.DatadogMetricInternal{
ID: "default/dcaautogen-b6ea72b610c00aba6791b5eca1912e68dc7412",
Active: false,
Valid: false,
Autogen: true,
ExternalMetricName: "docker.cpu.usage",
Deleted: true,
Value: 0.0,
UpdateTime: expiredUpdateTime,
Error: nil,
}
ddm.SetQueries("avg:docker.cpu.usage{bar:foo}.rollup(30)")
compareDatadogMetricInternal(t, &ddm, f.store.Get("default/dcaautogen-b6ea72b610c00aba6791b5eca1912e68dc7412"))
}
func TestAutoscalerAutogenLabelSelectorFiltering(t *testing.T) {
f := newAutoscalerFixture(t)
f.hpaLister = []*autoscaler.HorizontalPodAutoscaler{
// hpa0 matches label selector, no datadogmetric@ reference — included via label match
newFakeHorizontalPodAutoscalerWithLabels("ns0", "hpa0", map[string]string{"team": "infra"}, []autoscaler.MetricSpec{
{
Type: autoscaler.ExternalMetricSourceType,
External: &autoscaler.ExternalMetricSource{
MetricName: "requests_per_s",
MetricSelector: &metav1.LabelSelector{MatchLabels: map[string]string{"kube_container_name": "app"}},
},
},
}),
// hpa1 does NOT match label selector, but has datadogmetric@ reference — included via direct reference
newFakeHorizontalPodAutoscalerWithLabels("ns0", "hpa1", map[string]string{"app.kubernetes.io/managed-by": "keda-operator"}, []autoscaler.MetricSpec{
{
Type: autoscaler.ExternalMetricSourceType,
External: &autoscaler.ExternalMetricSource{
MetricName: "datadogmetric@ns0:dd-metric-ref",
},
},
}),
// hpa2 does NOT match label selector and has no datadogmetric@ reference — excluded
newFakeHorizontalPodAutoscalerWithLabels("ns0", "hpa2", map[string]string{"app.kubernetes.io/managed-by": "keda-operator"}, []autoscaler.MetricSpec{
{
Type: autoscaler.ExternalMetricSourceType,
External: &autoscaler.ExternalMetricSource{
MetricName: "keda_metric",
MetricSelector: &metav1.LabelSelector{MatchLabels: map[string]string{"queue": "jobs"}},
},
},
}),
}
f.wpaLister = []*unstructured.Unstructured{
// wpa0 matches label selector, no datadogmetric@ reference — included via label match
newFakeWatermarkPodAutoscalerWithLabels("ns0", "wpa0", map[string]string{"team": "infra"}, []interface{}{
map[string]interface{}{
"external": map[string]interface{}{
"metricName": "docker.cpu.usage",
"metricSelector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"bar": "foo",
},
},
},
"type": "External",
},
}),
// wpa1 does NOT match label selector and has no datadogmetric@ reference — excluded
newFakeWatermarkPodAutoscalerWithLabels("ns0", "wpa1", map[string]string{"app.kubernetes.io/managed-by": "keda-operator"}, []interface{}{
map[string]interface{}{
"external": map[string]interface{}{
"metricName": "keda_wpa_metric",
"metricSelector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"queue": "jobs",
},
},
},
"type": "External",
},
}),
}
ddm := model.DatadogMetricInternal{
ID: "ns0/dd-metric-ref",
Active: false,
Valid: true,
Value: 20.0,
UpdateTime: time.Now(),
Error: nil,
}
ddm.SetQueries("metric query ref")
f.store.Set("ns0/dd-metric-ref", ddm, "utest")
// Parse a selector that excludes autoscalers with app.kubernetes.io/managed-by=keda-operator
selector, err := labels.Parse("app.kubernetes.io/managed-by!=keda-operator")
assert.NoError(t, err)
autoscalerWatcher, kubeInformer, wpaInformer := f.newAutoscalerWatcher(selector)
stopCh := make(chan struct{})
defer close(stopCh)
kubeInformer.Start(stopCh)
wpaInformer.Start(stopCh)
autoscalerWatcher.processAutoscalers()
// Check all store entries for autoscaler references
foundHpa0Ref := false
foundHpa2Ref := false
foundWpa0Ref := false
foundWpa1Ref := false
for _, m := range f.store.GetAll() {
if m.AutoscalerReferences == "hpa:ns0/hpa0" {
foundHpa0Ref = true
}
if m.AutoscalerReferences == "hpa:ns0/hpa2" {
foundHpa2Ref = true
}
if m.AutoscalerReferences == "wpa:ns0/wpa0" {
foundWpa0Ref = true
}
if m.AutoscalerReferences == "wpa:ns0/wpa1" {
foundWpa1Ref = true
}
}
// hpa0 matched label selector — should be included and create autogen metric
assert.True(t, foundHpa0Ref, "hpa0 should be included (matches label selector)")
// hpa1 has datadogmetric@ reference — dd-metric-ref should be active despite failing label selector
refMetric := f.store.Get("ns0/dd-metric-ref")
assert.NotNil(t, refMetric)
assert.True(t, refMetric.Active)
assert.Equal(t, "hpa:ns0/hpa1", refMetric.AutoscalerReferences)
// hpa2 should be excluded — no label match, no datadogmetric@ reference
assert.False(t, foundHpa2Ref, "hpa2 should be excluded (no label match and no datadogmetric@ reference)")
// wpa0 matched label selector — should be included and create autogen metric
assert.True(t, foundWpa0Ref, "wpa0 should be included (matches label selector)")
// wpa1 should be excluded — no label match, no datadogmetric@ reference
assert.False(t, foundWpa1Ref, "wpa1 should be excluded (no label match and no datadogmetric@ reference)")
}