@@ -154,8 +154,9 @@ var _ = It("should validate the so creation when there isn't any hpa", func() {
154
154
err := k8sClient .Create (context .Background (), namespace )
155
155
Expect (err ).ToNot (HaveOccurred ())
156
156
157
- err = k8sClient .Create (context .Background (), so )
158
- Expect (err ).ToNot (HaveOccurred ())
157
+ Eventually (func () error {
158
+ return k8sClient .Create (context .Background (), so )
159
+ }).ShouldNot (HaveOccurred ())
159
160
})
160
161
161
162
var _ = It ("should validate the so creation when there are other SO for other workloads" , func () {
@@ -171,8 +172,9 @@ var _ = It("should validate the so creation when there are other SO for other wo
171
172
err = k8sClient .Create (context .Background (), so1 )
172
173
Expect (err ).ToNot (HaveOccurred ())
173
174
174
- err = k8sClient .Create (context .Background (), so2 )
175
- Expect (err ).ToNot (HaveOccurred ())
175
+ Eventually (func () error {
176
+ return k8sClient .Create (context .Background (), so2 )
177
+ }).ShouldNot (HaveOccurred ())
176
178
})
177
179
178
180
var _ = It ("should validate the so creation when there are other HPA for other workloads" , func () {
@@ -188,8 +190,9 @@ var _ = It("should validate the so creation when there are other HPA for other w
188
190
err = k8sClient .Create (context .Background (), hpa )
189
191
Expect (err ).ToNot (HaveOccurred ())
190
192
191
- err = k8sClient .Create (context .Background (), so )
192
- Expect (err ).ToNot (HaveOccurred ())
193
+ Eventually (func () error {
194
+ return k8sClient .Create (context .Background (), so )
195
+ }).ShouldNot (HaveOccurred ())
193
196
})
194
197
195
198
var _ = It ("should validate the so creation when it's own hpa is already generated" , func () {
@@ -206,8 +209,9 @@ var _ = It("should validate the so creation when it's own hpa is already generat
206
209
err = k8sClient .Create (context .Background (), hpa )
207
210
Expect (err ).ToNot (HaveOccurred ())
208
211
209
- err = k8sClient .Create (context .Background (), so )
210
- Expect (err ).ToNot (HaveOccurred ())
212
+ Eventually (func () error {
213
+ return k8sClient .Create (context .Background (), so )
214
+ }).ShouldNot (HaveOccurred ())
211
215
})
212
216
213
217
var _ = It ("should validate the so update when it's own hpa is already generated" , func () {
@@ -228,8 +232,9 @@ var _ = It("should validate the so update when it's own hpa is already generated
228
232
Expect (err ).ToNot (HaveOccurred ())
229
233
230
234
so .Spec .MaxReplicaCount = pointer .Int32 (7 )
231
- err = k8sClient .Update (context .Background (), so )
232
- Expect (err ).ToNot (HaveOccurred ())
235
+ Eventually (func () error {
236
+ return k8sClient .Update (context .Background (), so )
237
+ }).ShouldNot (HaveOccurred ())
233
238
})
234
239
235
240
var _ = It ("shouldn't validate the so creation when there is another unmanaged hpa" , func () {
@@ -246,8 +251,9 @@ var _ = It("shouldn't validate the so creation when there is another unmanaged h
246
251
err = k8sClient .Create (context .Background (), hpa )
247
252
Expect (err ).ToNot (HaveOccurred ())
248
253
249
- err = k8sClient .Create (context .Background (), so )
250
- Expect (err ).To (HaveOccurred ())
254
+ Eventually (func () error {
255
+ return k8sClient .Create (context .Background (), so )
256
+ }).Should (HaveOccurred ())
251
257
})
252
258
253
259
var _ = It ("shouldn't validate the so creation when there is another so" , func () {
@@ -264,8 +270,9 @@ var _ = It("shouldn't validate the so creation when there is another so", func()
264
270
err = k8sClient .Create (context .Background (), so2 )
265
271
Expect (err ).ToNot (HaveOccurred ())
266
272
267
- err = k8sClient .Create (context .Background (), so )
268
- Expect (err ).To (HaveOccurred ())
273
+ Eventually (func () error {
274
+ return k8sClient .Create (context .Background (), so )
275
+ }).Should (HaveOccurred ())
269
276
})
270
277
271
278
var _ = It ("shouldn't validate the so creation when there is another hpa with custom apis" , func () {
@@ -282,8 +289,9 @@ var _ = It("shouldn't validate the so creation when there is another hpa with cu
282
289
err = k8sClient .Create (context .Background (), hpa )
283
290
Expect (err ).ToNot (HaveOccurred ())
284
291
285
- err = k8sClient .Create (context .Background (), so )
286
- Expect (err ).To (HaveOccurred ())
292
+ Eventually (func () error {
293
+ return k8sClient .Create (context .Background (), so )
294
+ }).Should (HaveOccurred ())
287
295
})
288
296
289
297
var _ = It ("should validate the so creation with cpu and memory when deployment has requests" , func () {
@@ -299,8 +307,9 @@ var _ = It("should validate the so creation with cpu and memory when deployment
299
307
err = k8sClient .Create (context .Background (), workload )
300
308
Expect (err ).ToNot (HaveOccurred ())
301
309
302
- err = k8sClient .Create (context .Background (), so )
303
- Expect (err ).ToNot (HaveOccurred ())
310
+ Eventually (func () error {
311
+ return k8sClient .Create (context .Background (), so )
312
+ }).ShouldNot (HaveOccurred ())
304
313
})
305
314
306
315
var _ = It ("shouldn't validate the so creation with cpu and memory when deployment hasn't got memory request" , func () {
@@ -316,8 +325,9 @@ var _ = It("shouldn't validate the so creation with cpu and memory when deployme
316
325
err = k8sClient .Create (context .Background (), workload )
317
326
Expect (err ).ToNot (HaveOccurred ())
318
327
319
- err = k8sClient .Create (context .Background (), so )
320
- Expect (err ).To (HaveOccurred ())
328
+ Eventually (func () error {
329
+ return k8sClient .Create (context .Background (), so )
330
+ }).Should (HaveOccurred ())
321
331
})
322
332
323
333
var _ = It ("shouldn't validate the so creation with cpu and memory when deployment hasn't got cpu request" , func () {
@@ -333,8 +343,9 @@ var _ = It("shouldn't validate the so creation with cpu and memory when deployme
333
343
err = k8sClient .Create (context .Background (), workload )
334
344
Expect (err ).ToNot (HaveOccurred ())
335
345
336
- err = k8sClient .Create (context .Background (), so )
337
- Expect (err ).To (HaveOccurred ())
346
+ Eventually (func () error {
347
+ return k8sClient .Create (context .Background (), so )
348
+ }).Should (HaveOccurred ())
338
349
})
339
350
340
351
var _ = It ("should validate the so creation with cpu and memory when statefulset has requests" , func () {
@@ -350,8 +361,9 @@ var _ = It("should validate the so creation with cpu and memory when statefulset
350
361
err = k8sClient .Create (context .Background (), workload )
351
362
Expect (err ).ToNot (HaveOccurred ())
352
363
353
- err = k8sClient .Create (context .Background (), so )
354
- Expect (err ).ToNot (HaveOccurred ())
364
+ Eventually (func () error {
365
+ return k8sClient .Create (context .Background (), so )
366
+ }).ShouldNot (HaveOccurred ())
355
367
})
356
368
357
369
var _ = It ("shouldn't validate the so creation with cpu and memory when statefulset hasn't got memory request" , func () {
@@ -367,8 +379,9 @@ var _ = It("shouldn't validate the so creation with cpu and memory when stateful
367
379
err = k8sClient .Create (context .Background (), workload )
368
380
Expect (err ).ToNot (HaveOccurred ())
369
381
370
- err = k8sClient .Create (context .Background (), so )
371
- Expect (err ).To (HaveOccurred ())
382
+ Eventually (func () error {
383
+ return k8sClient .Create (context .Background (), so )
384
+ }).Should (HaveOccurred ())
372
385
})
373
386
374
387
var _ = It ("shouldn't validate the so creation with cpu and memory when statefulset hasn't got cpu request" , func () {
@@ -384,8 +397,9 @@ var _ = It("shouldn't validate the so creation with cpu and memory when stateful
384
397
err = k8sClient .Create (context .Background (), workload )
385
398
Expect (err ).ToNot (HaveOccurred ())
386
399
387
- err = k8sClient .Create (context .Background (), so )
388
- Expect (err ).To (HaveOccurred ())
400
+ Eventually (func () error {
401
+ return k8sClient .Create (context .Background (), so )
402
+ }).Should (HaveOccurred ())
389
403
})
390
404
391
405
var _ = It ("should validate the so creation without cpu and memory when custom resources" , func () {
@@ -397,8 +411,88 @@ var _ = It("should validate the so creation without cpu and memory when custom r
397
411
err := k8sClient .Create (context .Background (), namespace )
398
412
Expect (err ).ToNot (HaveOccurred ())
399
413
400
- err = k8sClient .Create (context .Background (), so )
414
+ Eventually (func () error {
415
+ return k8sClient .Create (context .Background (), so )
416
+ }).ShouldNot (HaveOccurred ())
417
+ })
418
+
419
+ var _ = It ("should validate so creation when all requirements are met for scaling to zero with cpu scaler" , func () {
420
+ namespaceName := "scale-to-zero-good"
421
+ namespace := createNamespace (namespaceName )
422
+ workload := createDeployment (namespaceName , true , false )
423
+
424
+ so := createScaledObjectSTZ (soName , namespaceName , workloadName , 0 , 5 , true )
425
+
426
+ err := k8sClient .Create (context .Background (), namespace )
427
+ Expect (err ).ToNot (HaveOccurred ())
428
+
429
+ err = k8sClient .Create (context .Background (), workload )
430
+ Expect (err ).ToNot (HaveOccurred ())
431
+
432
+ Eventually (func () error {
433
+ return k8sClient .Create (context .Background (), so )
434
+ }).ShouldNot (HaveOccurred ())
435
+ })
436
+
437
+ var _ = It ("shouldn't validate so creation with cpu scaler requirements not being met for scaling to 0" , func () {
438
+ namespaceName := "scale-to-zero-min-replicas-bad"
439
+ namespace := createNamespace (namespaceName )
440
+ workload := createDeployment (namespaceName , true , false )
441
+
442
+ so := createScaledObjectSTZ (soName , namespaceName , workloadName , 0 , 5 , false )
443
+
444
+ err := k8sClient .Create (context .Background (), namespace )
445
+ Expect (err ).ToNot (HaveOccurred ())
446
+ err = k8sClient .Create (context .Background (), workload )
447
+ Expect (err ).ToNot (HaveOccurred ())
448
+ Eventually (func () error {
449
+ return k8sClient .Create (context .Background (), so )
450
+ }).Should (HaveOccurred ())
451
+ })
452
+
453
+ var _ = It ("should validate so creation when min replicas is > 0 with only cpu scaler given" , func () {
454
+ namespaceName := "scale-to-zero-no-external-trigger-good"
455
+ namespace := createNamespace (namespaceName )
456
+ workload := createDeployment (namespaceName , true , false )
457
+
458
+ so := createScaledObjectSTZ (soName , namespaceName , workloadName , 1 , 5 , false )
459
+
460
+ err := k8sClient .Create (context .Background (), namespace )
461
+ Expect (err ).ToNot (HaveOccurred ())
462
+ err = k8sClient .Create (context .Background (), workload )
463
+ Expect (err ).ToNot (HaveOccurred ())
464
+ Eventually (func () error {
465
+ return k8sClient .Create (context .Background (), so )
466
+ }).ShouldNot (HaveOccurred ())
467
+
468
+ })
469
+
470
+ var _ = It ("should validate the so update if it's removing the finalizer even if it's invalid" , func () {
471
+
472
+ namespaceName := "removing-finalizers"
473
+ namespace := createNamespace (namespaceName )
474
+ workload := createDeployment (namespaceName , true , true )
475
+ so := createScaledObject (soName , namespaceName , workloadName , "apps/v1" , "Deployment" , true )
476
+ so .ObjectMeta .Finalizers = append (so .ObjectMeta .Finalizers , "finalizer" )
477
+
478
+ err := k8sClient .Create (context .Background (), namespace )
401
479
Expect (err ).ToNot (HaveOccurred ())
480
+
481
+ err = k8sClient .Create (context .Background (), workload )
482
+ Expect (err ).ToNot (HaveOccurred ())
483
+
484
+ Eventually (func () error {
485
+ return k8sClient .Create (context .Background (), so )
486
+ }).ShouldNot (HaveOccurred ())
487
+
488
+ workload .Spec .Template .Spec .Containers [0 ].Resources .Requests = nil
489
+ err = k8sClient .Update (context .Background (), workload )
490
+ Expect (err ).ToNot (HaveOccurred ())
491
+
492
+ so .ObjectMeta .Finalizers = []string {}
493
+ Eventually (func () error {
494
+ return k8sClient .Update (context .Background (), so )
495
+ }).ShouldNot (HaveOccurred ())
402
496
})
403
497
404
498
var _ = AfterSuite (func () {
@@ -595,3 +689,44 @@ func createStatefulSet(namespace string, hasCPU, hasMemory bool) *appsv1.Statefu
595
689
},
596
690
}
597
691
}
692
+
693
+ func createScaledObjectSTZ (name string , namespace string , targetName string , minReplicas int32 , maxReplicas int32 , hasExternalTrigger bool ) * ScaledObject {
694
+ triggers := []ScaleTriggers {
695
+ {
696
+ Type : "cpu" ,
697
+ Metadata : map [string ]string {
698
+ "value" : "10" ,
699
+ },
700
+ },
701
+ }
702
+ if hasExternalTrigger {
703
+ kubeWorkloadTrigger := ScaleTriggers {
704
+ Type : "kubernetes-workload" ,
705
+ Metadata : map [string ]string {
706
+ "podSelector" : "pod=workload-test" ,
707
+ "value" : "1" ,
708
+ },
709
+ }
710
+ triggers = append (triggers , kubeWorkloadTrigger )
711
+ }
712
+ return & ScaledObject {
713
+ ObjectMeta : metav1.ObjectMeta {
714
+ Name : name ,
715
+ Namespace : namespace ,
716
+ UID : types .UID (name ),
717
+ },
718
+ TypeMeta : metav1.TypeMeta {
719
+ Kind : "ScaledObject" ,
720
+ APIVersion : "keda.sh" ,
721
+ },
722
+ Spec : ScaledObjectSpec {
723
+ ScaleTargetRef : & ScaleTarget {
724
+ Name : targetName ,
725
+ },
726
+ MinReplicaCount : pointer .Int32 (minReplicas ),
727
+ MaxReplicaCount : pointer .Int32 (maxReplicas ),
728
+ CooldownPeriod : pointer .Int32 (1 ),
729
+ Triggers : triggers ,
730
+ },
731
+ }
732
+ }
0 commit comments