forked from kedacore/keda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscaledobject_webhook_test.go
More file actions
1495 lines (1242 loc) · 48.1 KB
/
Copy pathscaledobject_webhook_test.go
File metadata and controls
1495 lines (1242 loc) · 48.1 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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2023 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 v1alpha1
import (
"context"
"strings"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
v2 "k8s.io/api/autoscaling/v2"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var _ = It("should validate the so creation when there isn't any hpa", func() {
namespaceName := "valid"
namespace := createNamespace(namespaceName)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("should validate the so creation when there are other SO for other workloads", func() {
namespaceName := "valid-multiple-so"
namespace := createNamespace(namespaceName)
so1 := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
so2 := createScaledObject("other-so-name", namespaceName, "other-workload", "apps/v1", "Deployment", false, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), so1)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so2)
}).ShouldNot(HaveOccurred())
})
var _ = It("should validate the so creation when there are other HPA for other workloads", func() {
namespaceName := "valid-other-hpa"
namespace := createNamespace(namespaceName)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
hpa := createHpa("other-hpa-name", namespaceName, "other-workload", "apps/v1", "Deployment", nil)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), hpa)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("should validate the so creation when it's own hpa is already generated", func() {
hpaName := "test-so-hpa"
namespaceName := "own-hpa"
namespace := createNamespace(namespaceName)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
hpa := createHpa(hpaName, namespaceName, workloadName, "apps/v1", "Deployment", so)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), hpa)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("should validate the so update when it's own hpa is already generated", func() {
hpaName := "test-so-hpa"
namespaceName := "update-so"
namespace := createNamespace(namespaceName)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
hpa := createHpa(hpaName, namespaceName, workloadName, "apps/v1", "Deployment", so)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), hpa)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), so)
Expect(err).ToNot(HaveOccurred())
so.Spec.MaxReplicaCount = ptr.To[int32](7)
Eventually(func() error {
return k8sClient.Update(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("shouldn't validate the so creation when there is another unmanaged hpa", func() {
hpaName := "test-unmanaged-hpa"
namespaceName := "unmanaged-hpa"
namespace := createNamespace(namespaceName)
hpa := createHpa(hpaName, namespaceName, workloadName, "apps/v1", "Deployment", nil)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), hpa)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("shouldn't validate the so creation when the replica counts are wrong", func() {
namespaceName := "wrong-replica-count"
namespace := createNamespace(namespaceName)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
so.Spec.MinReplicaCount = ptr.To[int32](10)
so.Spec.MaxReplicaCount = ptr.To[int32](5)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("shouldn't validate the so creation when the fallback is wrong", func() {
namespaceName := "wrong-fallback"
namespace := createNamespace(namespaceName)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
so.Spec.Fallback = &Fallback{
FailureThreshold: -1,
Replicas: -3,
}
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("shouldn't validate the so creation when the fallback is configured and only cpu/memory triggers are used.", func() {
namespaceName := "wrong-fallback-cpu-memory"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, true, true)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", true, map[string]string{}, "")
so.Spec.Fallback = &Fallback{
FailureThreshold: 3,
Replicas: 6,
}
for index := range so.Spec.Triggers {
so.Spec.Triggers[index].MetricType = "AverageValue"
}
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("should validate the so creation when the fallback is configured, and at least one trigger (besides cpu/memory) is configured.", func() {
namespaceName := "right-fallback-at-least-one-averagevalue"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, true, true)
// Create ScaledObject with cpu and memory triggers.
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", true, map[string]string{}, "")
triggers := []ScaleTriggers{
{
Type: "kubernetes-workload",
Name: "workload_trig_1",
Metadata: map[string]string{
"podSelector": "pod=workload-test",
"value": "1",
},
MetricType: v2.ValueMetricType,
},
{
Type: "kubernetes-workload",
Name: "workload_trig_2",
Metadata: map[string]string{
"podSelector": "pod=workload-test",
"value": "1",
},
MetricType: v2.AverageValueMetricType,
},
}
// Append other triggers to the SO, one of them with metricType=AverageValue.
so.Spec.Triggers = append(so.Spec.Triggers, triggers...)
so.Spec.Fallback = &Fallback{
FailureThreshold: 3,
Replicas: 6,
}
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("should validate the so creation when the fallback is configured, and so uses ScalingModifiers.", func() {
namespaceName := "right-fallback-scalingmodifier"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, true, true)
// Create ScaledObject with cpu and memory triggers.
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", true, map[string]string{}, "")
triggers := []ScaleTriggers{
{
Type: "kubernetes-workload",
Name: "workload_trig_1",
Metadata: map[string]string{
"podSelector": "pod=workload-test",
"value": "1",
},
MetricType: v2.ValueMetricType,
},
{
Type: "kubernetes-workload",
Name: "workload_trig_2",
Metadata: map[string]string{
"podSelector": "pod=workload-test",
"value": "1",
},
MetricType: v2.ValueMetricType,
},
}
// Append other triggers to the SO, none of them with metricType=AverageValue.
so.Spec.Triggers = append(so.Spec.Triggers, triggers...)
so.Spec.Fallback = &Fallback{
FailureThreshold: 3,
Replicas: 6,
}
so.Spec.Advanced.ScalingModifiers = ScalingModifiers{Target: "2", Formula: "workload_trig_1 + workload_trig_2", MetricType: v2.ValueMetricType}
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("should validate the so creation when there is another unmanaged hpa and so has transfer-hpa-ownership activated and the name is specified", func() {
hpaName := "test-unmanaged-hpa-ownership-with-name"
namespaceName := "unmanaged-hpa-ownership-with-name"
namespace := createNamespace(namespaceName)
hpa := createHpa(hpaName, namespaceName, workloadName, "apps/v1", "Deployment", nil)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{ScaledObjectTransferHpaOwnershipAnnotation: "true"}, hpaName)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), hpa)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("shouldn't validate the so creation when there is another unmanaged hpa and so has transfer-hpa-ownership activated but no name specified", func() {
hpaName := "test-unmanaged-hpa-ownership-without-name"
namespaceName := "unmanaged-hpa-ownership-without-name"
namespace := createNamespace(namespaceName)
hpa := createHpa(hpaName, namespaceName, workloadName, "apps/v1", "Deployment", nil)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{ScaledObjectTransferHpaOwnershipAnnotation: "true"}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), hpa)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("shouldn't validate the so creation when hpa has shared-ownership unactivated", func() {
hpaName := "test-hpa-disabled-validation-by-hpa-ownership"
namespaceName := "hpa-ownership"
namespace := createNamespace(namespaceName)
hpa := createHpa(hpaName, namespaceName, workloadName, "apps/v1", "Deployment", nil)
hpa.Annotations = map[string]string{ValidationsHpaOwnershipAnnotation: "false"}
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{ScaledObjectTransferHpaOwnershipAnnotation: "false"}, hpaName)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), hpa)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("shouldn't validate the so creation when there is another so", func() {
so2Name := "test-so2"
namespaceName := "managed-hpa"
namespace := createNamespace(namespaceName)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
so2 := createScaledObject(so2Name, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), so2)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("shouldn't validate the so creation when there is another hpa with custom apis", func() {
hpaName := "test-custom-hpa"
namespaceName := "custom-apis"
namespace := createNamespace(namespaceName)
so := createScaledObject(soName, namespaceName, workloadName, "custom-api/v1", "custom-kind", false, map[string]string{}, "")
hpa := createHpa(hpaName, namespaceName, workloadName, "custom-api/v1", "custom-kind", nil)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), hpa)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("should validate the so creation with cpu and memory when deployment has requests", func() {
namespaceName := "deployment-has-requests"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, true, true)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", true, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("shouldn't validate the creation with cpu and memory when deployment is missing", func() {
namespaceName := "deployment-missing"
namespace := createNamespace(namespaceName)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", true, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("should validate the creation with cpu and memory when deployment is missing and dry-run is true", func() {
namespaceName := "deployment-missing-dry-run"
namespace := createNamespace(namespaceName)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", true, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so, client.DryRunAll)
}).ShouldNot(HaveOccurred())
})
var _ = It("shouldn't validate the so creation with cpu and memory when deployment hasn't got memory request", func() {
namespaceName := "deployment-no-memory-request"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, true, false)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", true, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
// This test checks whether the validation fails when the CPU and memory resource limits are missing in pod spec (in
// deployment) and there are CPU and memory triggers in ScaledObject. This is a test for already existing behavior.
// See github.com/kedacore/keda/issues/5348
var _ = It("shouldn't validate the SO creation with CPU and memory when deployment doesn't have CPU and memory", func() {
namespaceName := "resource-default-limits-missing"
namespace := createNamespace(namespaceName)
deployment := createDeployment(namespaceName, false, false)
scaledObject := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", true, map[string]string{}, "")
// Create namespace
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
// Create deployment
err = k8sClient.Create(context.Background(), deployment)
Expect(err).ToNot(HaveOccurred())
// Create scaled object, asynchronously
Eventually(func() error {
return k8sClient.Create(context.Background(), scaledObject)
}).Should(HaveOccurred())
})
// This test checks whether the validation fails when the CPU and memory resource limits are missing in pod spec (in
// deployment), but there are default limits specified in LimitRange in the same namespace, and there are CPU and
// memory triggers in ScaledObject. This a test for newly added behavior after fixing the following issue.
// See github.com/kedacore/keda/issues/5348
var _ = It("should validate the SO creation with CPU and memory when deployment doesn't have CPU and memory, but LimitRange has the limits specified", func() {
namespaceName := "resource-default-limits-in-limitrange"
limitRangeName := "test-limit-range"
cpuLimit := resource.NewMilliQuantity(100, resource.DecimalSI)
memoryLimit := resource.NewMilliQuantity(100, resource.DecimalSI)
namespace := createNamespace(namespaceName)
deployment := createDeployment(namespaceName, false, false)
limitRange := createLimitRange(limitRangeName, namespaceName, v1.LimitTypeContainer, cpuLimit, memoryLimit)
scaledObject := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", true, map[string]string{}, "")
// Create namespace
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
// Create limit range
err = k8sClient.Create(context.Background(), limitRange)
Expect(err).ToNot(HaveOccurred())
// Create deployment
err = k8sClient.Create(context.Background(), deployment)
Expect(err).ToNot(HaveOccurred())
// Create scaled object, asynchronously
Eventually(func() error {
return k8sClient.Create(context.Background(), scaledObject)
}).ShouldNot(HaveOccurred())
})
var _ = It("shouldn't validate the so creation with cpu and memory when deployment hasn't got cpu request", func() {
namespaceName := "deployment-no-cpu-request"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, false, true)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", true, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("should validate the so creation with cpu and memory when statefulset has requests", func() {
namespaceName := "statefulset-has-requests"
namespace := createNamespace(namespaceName)
workload := createStatefulSet(namespaceName, true, true)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "StatefulSet", true, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("shouldn't validate the so creation with cpu and memory when statefulset hasn't got memory request", func() {
namespaceName := "statefulset-no-memory-request"
namespace := createNamespace(namespaceName)
workload := createStatefulSet(namespaceName, true, false)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "StatefulSet", true, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("shouldn't validate the so creation with cpu and memory when statefulset hasn't got cpu request", func() {
namespaceName := "statefulset-no-cpu-request"
namespace := createNamespace(namespaceName)
workload := createStatefulSet(namespaceName, false, true)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "StatefulSet", true, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("should validate the so creation without cpu and memory when custom resources", func() {
namespaceName := "crd-not-resources"
namespace := createNamespace(namespaceName)
so := createScaledObject(soName, namespaceName, workloadName, "custom-api", "StatefulSet", true, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("should validate so creation when all requirements are met for scaling to zero with cpu scaler", func() {
namespaceName := "scale-to-zero-good"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, true, false)
so := createScaledObjectSTZ(soName, namespaceName, workloadName, 0, 5, true)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("shouldn't validate so creation with cpu scaler requirements not being met for scaling to 0", func() {
namespaceName := "scale-to-zero-min-replicas-bad"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, true, false)
so := createScaledObjectSTZ(soName, namespaceName, workloadName, 0, 5, false)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("should validate so creation when min replicas is > 0 with only cpu scaler given", func() {
namespaceName := "scale-to-zero-no-external-trigger-good"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, true, false)
so := createScaledObjectSTZ(soName, namespaceName, workloadName, 1, 5, false)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("should not validate ScaledObject creation when deployment only provides cpu resource limits", func() {
namespaceName := "only-cpu-resource-limits-set"
namespace := createNamespace(namespaceName)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
workload := createDeployment(namespaceName, false, false)
workload.Spec.Template.Spec.Containers[0].Resources.Limits = v1.ResourceList{
v1.ResourceCPU: *resource.NewMilliQuantity(100, resource.DecimalSI),
}
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
so.Spec.Triggers = []ScaleTriggers{
{
Type: "cpu",
Metadata: map[string]string{
"value": "10",
},
},
}
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("should not validate ScaledObject creation when deployment only provides memory resource limits", func() {
namespaceName := "only-memory-resource-limits-set"
namespace := createNamespace(namespaceName)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
workload := createDeployment(namespaceName, false, false)
workload.Spec.Template.Spec.Containers[0].Resources.Limits = v1.ResourceList{
v1.ResourceMemory: *resource.NewMilliQuantity(1024, resource.DecimalSI),
}
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
so.Spec.Triggers = []ScaleTriggers{
{
Type: "memory",
Metadata: map[string]string{
"value": "512Mi",
},
},
}
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("should validate the so update if it's removing the finalizer even if it's invalid", func() {
namespaceName := "removing-finalizers"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, true, true)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", true, map[string]string{}, "")
so.Finalizers = append(so.Finalizers, "finalizer")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
workload.Spec.Template.Spec.Containers[0].Resources.Requests = nil
err = k8sClient.Update(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
so.Finalizers = []string{}
Eventually(func() error {
return k8sClient.Update(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("shouldn't validate the so creation when the name exceeds 54 characters", func() {
namespaceName := "so-name-too-long"
namespace := createNamespace(namespaceName)
longName := strings.Repeat("a", maxScaledObjectNameLength+1)
so := createScaledObject(longName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("should validate the so creation when the name is exactly 54 characters", func() {
namespaceName := "so-name-max-length"
namespace := createNamespace(namespaceName)
maxName := strings.Repeat("a", maxScaledObjectNameLength)
so := createScaledObject(maxName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("shouldn't create so when stabilizationWindowSeconds exceeds 3600", func() {
namespaceName := "fail-so-creation"
namespace := createNamespace(namespaceName)
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
so.Spec.Advanced.HorizontalPodAutoscalerConfig = &HorizontalPodAutoscalerConfig{
Behavior: &v2.HorizontalPodAutoscalerBehavior{
ScaleDown: &v2.HPAScalingRules{
StabilizationWindowSeconds: ptr.To[int32](3700),
},
},
}
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).
WithTimeout(5 * time.Second).
Should(HaveOccurred())
})
var _ = It("should validate empty triggers in ScaledObject", func() {
namespaceName := "empty-triggers-set"
namespace := createNamespace(namespaceName)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
workload := createDeployment(namespaceName, false, false)
workload.Spec.Template.Spec.Containers[0].Resources.Limits = v1.ResourceList{
v1.ResourceCPU: *resource.NewMilliQuantity(100, resource.DecimalSI),
}
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
so := createScaledObject(soName, namespaceName, workloadName, "apps/v1", "Deployment", false, map[string]string{}, "")
so.Spec.Triggers = []ScaleTriggers{}
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
// ============================ SCALING MODIFIERS ============================ \\
// =========================================================================== \\
var _ = It("should validate the so creation with ScalingModifiers.Formula", func() {
namespaceName := "scaling-modifiers-formula-good"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, false, false)
sm := ScalingModifiers{Target: "2", Formula: "workload_trig + cron_trig"}
triggers := []ScaleTriggers{
{
Type: "cron",
Name: "cron_trig",
Metadata: map[string]string{
"timezone": "UTC",
"start": "0 * * * *",
"end": "1 * * * *",
"desiredReplicas": "1",
},
},
{
Type: "kubernetes-workload",
Name: "workload_trig",
Metadata: map[string]string{
"podSelector": "pod=workload-test",
"value": "1",
},
},
}
so := createScaledObjectScalingModifiers(namespaceName, sm, triggers)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("shouldnt validate the so creation with scalingModifiers.Formula but no target", func() {
namespaceName := "scaling-modifiers-formula-no-target-bad"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, false, false)
sm := ScalingModifiers{Formula: "workload_trig + cron_trig"}
triggers := []ScaleTriggers{
{
Type: "cron",
Name: "cron_trig",
Metadata: map[string]string{
"timezone": "UTC",
"start": "0 * * * *",
"end": "1 * * * *",
"desiredReplicas": "1",
},
},
{
Type: "kubernetes-workload",
Name: "workload_trig",
Metadata: map[string]string{
"podSelector": "pod=workload-test",
"value": "1",
},
},
}
so := createScaledObjectScalingModifiers(namespaceName, sm, triggers)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("shouldnt validate the so creation with ScalingModifiers when triggers dont have names", func() {
namespaceName := "scaling-modifiers-triggers-no-names-bad"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, true, true)
sm := ScalingModifiers{Formula: "workload_trig + cron_trig"}
triggers := []ScaleTriggers{
{
Type: "cron",
Metadata: map[string]string{
"timezone": "UTC",
"start": "0 * * * *",
"end": "1 * * * *",
"desiredReplicas": "1",
},
},
{
Type: "kubernetes-workload",
Metadata: map[string]string{
"podSelector": "pod=workload-test",
"value": "1",
},
},
}
so := createScaledObjectScalingModifiers(namespaceName, sm, triggers)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).Should(HaveOccurred())
})
var _ = It("should validate the so creation with ScalingModifiers when formula triggers do have names but not all triggers", func() {
namespaceName := "scaling-modifiers-specific-triggers-good"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, true, true)
sm := ScalingModifiers{Target: "2", Formula: "workload_trig + 1"}
triggers := []ScaleTriggers{
{
Type: "cron",
Metadata: map[string]string{
"timezone": "UTC",
"start": "0 * * * *",
"end": "1 * * * *",
"desiredReplicas": "1",
},
},
{
Type: "kubernetes-workload",
Name: "workload_trig",
Metadata: map[string]string{
"podSelector": "pod=workload-test",
"value": "1",
},
},
}
so := createScaledObjectScalingModifiers(namespaceName, sm, triggers)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("should validate the so creation with ScalingModifiers when formula casts to float already", func() {
namespaceName := "scaling-modifiers-cast-to-float-good"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, true, true)
sm := ScalingModifiers{Target: "2", Formula: "float(workload_trig + 1)"}
triggers := []ScaleTriggers{
{
Type: "kubernetes-workload",
Name: "workload_trig",
Metadata: map[string]string{
"podSelector": "pod=workload-test",
"value": "1",
},
},
}
so := createScaledObjectScalingModifiers(namespaceName, sm, triggers)
err := k8sClient.Create(context.Background(), namespace)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), workload)
Expect(err).ToNot(HaveOccurred())
Eventually(func() error {
return k8sClient.Create(context.Background(), so)
}).ShouldNot(HaveOccurred())
})
var _ = It("should validate the so creation with ScalingModifiers.Formula - casting float from ternary operator", func() {
namespaceName := "scaling-modifiers-formula-casting-float-good"
namespace := createNamespace(namespaceName)
workload := createDeployment(namespaceName, false, false)