@@ -20,8 +20,12 @@ import (
20
20
"reflect"
21
21
"testing"
22
22
23
+ "k8s.io/apimachinery/pkg/api/resource"
23
24
"k8s.io/apimachinery/pkg/runtime"
25
+ utilfeature "k8s.io/apiserver/pkg/util/feature"
26
+ featuregatetesting "k8s.io/component-base/featuregate/testing"
24
27
"k8s.io/kubernetes/pkg/api/legacyscheme"
28
+ "k8s.io/kubernetes/pkg/features"
25
29
26
30
"github.com/google/go-cmp/cmp"
27
31
"github.com/stretchr/testify/assert"
@@ -132,14 +136,17 @@ func TestGenerateScaleUpRules(t *testing.T) {
132
136
rateUpPercentPeriodSeconds int32
133
137
stabilizationSeconds * int32
134
138
selectPolicy * autoscalingv2.ScalingPolicySelect
139
+ tolerance * resource.Quantity
135
140
136
141
expectedPolicies []autoscalingv2.HPAScalingPolicy
137
142
expectedStabilization * int32
138
143
expectedSelectPolicy string
144
+ expectedTolerance * resource.Quantity
139
145
annotation string
140
146
}
141
147
maxPolicy := autoscalingv2 .MaxChangePolicySelect
142
148
minPolicy := autoscalingv2 .MinChangePolicySelect
149
+ sampleTolerance := resource .MustParse ("0.5" )
143
150
tests := []TestCase {
144
151
{
145
152
annotation : "Default values" ,
@@ -208,12 +215,25 @@ func TestGenerateScaleUpRules(t *testing.T) {
208
215
expectedStabilization : utilpointer .Int32 (25 ),
209
216
expectedSelectPolicy : string (autoscalingv2 .MaxChangePolicySelect ),
210
217
},
218
+ {
219
+ annotation : "Percent policy and tolerance is specified" ,
220
+ rateUpPercent : 7 ,
221
+ rateUpPercentPeriodSeconds : 10 ,
222
+ tolerance : & sampleTolerance ,
223
+ expectedPolicies : []autoscalingv2.HPAScalingPolicy {
224
+ {Type : autoscalingv2 .PercentScalingPolicy , Value : 7 , PeriodSeconds : 10 },
225
+ },
226
+ expectedStabilization : utilpointer .Int32 (0 ),
227
+ expectedSelectPolicy : string (autoscalingv2 .MaxChangePolicySelect ),
228
+ expectedTolerance : & sampleTolerance ,
229
+ },
211
230
}
212
231
for _ , tc := range tests {
213
232
t .Run (tc .annotation , func (t * testing.T ) {
214
233
scaleUpRules := & autoscalingv2.HPAScalingRules {
215
234
StabilizationWindowSeconds : tc .stabilizationSeconds ,
216
235
SelectPolicy : tc .selectPolicy ,
236
+ Tolerance : tc .tolerance ,
217
237
}
218
238
if tc .rateUpPods != 0 || tc .rateUpPodsPeriodSeconds != 0 {
219
239
scaleUpRules .Policies = append (scaleUpRules .Policies , autoscalingv2.HPAScalingPolicy {
@@ -234,10 +254,138 @@ func TestGenerateScaleUpRules(t *testing.T) {
234
254
}
235
255
236
256
assert .Equal (t , autoscalingv2 .ScalingPolicySelect (tc .expectedSelectPolicy ), * up .SelectPolicy )
257
+ assert .Equal (t , tc .expectedTolerance , up .Tolerance )
237
258
})
238
259
}
239
260
}
240
261
262
+ func TestSetBehaviorDefaults (t * testing.T ) {
263
+ sampleTolerance := resource .MustParse ("0.5" )
264
+ maxPolicy := autoscalingv2 .MaxChangePolicySelect
265
+ policies := []autoscalingv2.HPAScalingPolicy {
266
+ {Type : autoscalingv2 .PercentScalingPolicy , Value : 7 , PeriodSeconds : 10 },
267
+ }
268
+ type TestCase struct {
269
+ behavior * autoscalingv2.HorizontalPodAutoscalerBehavior
270
+ expectedBehavior * autoscalingv2.HorizontalPodAutoscalerBehavior
271
+ annotation string
272
+ }
273
+
274
+ tests := []TestCase {
275
+ {
276
+ annotation : "Nil behavior" ,
277
+ behavior : nil ,
278
+ expectedBehavior : nil ,
279
+ },
280
+ {
281
+ annotation : "Behavior with stabilizationWindowSeconds and tolerance" ,
282
+ behavior : & autoscalingv2.HorizontalPodAutoscalerBehavior {
283
+ ScaleUp : & autoscalingv2.HPAScalingRules {
284
+ StabilizationWindowSeconds : utilpointer .Int32 (100 ),
285
+ Tolerance : & sampleTolerance ,
286
+ },
287
+ },
288
+ expectedBehavior : & autoscalingv2.HorizontalPodAutoscalerBehavior {
289
+ ScaleDown : & autoscalingv2.HPAScalingRules {
290
+ SelectPolicy : & maxPolicy ,
291
+ Policies : []autoscalingv2.HPAScalingPolicy {
292
+ {Type : autoscalingv2 .PercentScalingPolicy , Value : 100 , PeriodSeconds : 15 },
293
+ },
294
+ },
295
+ ScaleUp : & autoscalingv2.HPAScalingRules {
296
+ StabilizationWindowSeconds : utilpointer .Int32 (100 ),
297
+ SelectPolicy : & maxPolicy ,
298
+ Policies : []autoscalingv2.HPAScalingPolicy {
299
+ {Type : autoscalingv2 .PodsScalingPolicy , Value : 4 , PeriodSeconds : 15 },
300
+ {Type : autoscalingv2 .PercentScalingPolicy , Value : 100 , PeriodSeconds : 15 },
301
+ },
302
+ Tolerance : & sampleTolerance ,
303
+ },
304
+ },
305
+ },
306
+ {
307
+ annotation : "Behavior with policy, without tolerance" ,
308
+ behavior : & autoscalingv2.HorizontalPodAutoscalerBehavior {
309
+ ScaleDown : & autoscalingv2.HPAScalingRules {
310
+ Policies : policies ,
311
+ },
312
+ },
313
+ expectedBehavior : & autoscalingv2.HorizontalPodAutoscalerBehavior {
314
+ ScaleDown : & autoscalingv2.HPAScalingRules {
315
+ SelectPolicy : & maxPolicy ,
316
+ Policies : policies ,
317
+ },
318
+ ScaleUp : & autoscalingv2.HPAScalingRules {
319
+ StabilizationWindowSeconds : utilpointer .Int32 (0 ),
320
+ SelectPolicy : & maxPolicy ,
321
+ Policies : []autoscalingv2.HPAScalingPolicy {
322
+ {Type : autoscalingv2 .PodsScalingPolicy , Value : 4 , PeriodSeconds : 15 },
323
+ {Type : autoscalingv2 .PercentScalingPolicy , Value : 100 , PeriodSeconds : 15 },
324
+ },
325
+ },
326
+ },
327
+ },
328
+ }
329
+ for _ , tc := range tests {
330
+ t .Run (tc .annotation , func (t * testing.T ) {
331
+ hpa := autoscalingv2.HorizontalPodAutoscaler {
332
+ Spec : autoscalingv2.HorizontalPodAutoscalerSpec {
333
+ Behavior : tc .behavior ,
334
+ },
335
+ }
336
+ expectedHPA := autoscalingv2.HorizontalPodAutoscaler {
337
+ Spec : autoscalingv2.HorizontalPodAutoscalerSpec {
338
+ Behavior : tc .expectedBehavior ,
339
+ },
340
+ }
341
+ SetDefaults_HorizontalPodAutoscalerBehavior (& hpa )
342
+ assert .Equal (t , expectedHPA , hpa )
343
+ })
344
+ }
345
+ }
346
+
347
+ func TestSetBehaviorDefaultsConfigurableToleranceEnabled (t * testing.T ) {
348
+ // Enable HPAConfigurableTolerance feature gate.
349
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .HPAConfigurableTolerance , true )
350
+
351
+ // Verify that the tolerance field is left unset.
352
+ maxPolicy := autoscalingv2 .MaxChangePolicySelect
353
+
354
+ hpa := autoscalingv2.HorizontalPodAutoscaler {
355
+ Spec : autoscalingv2.HorizontalPodAutoscalerSpec {
356
+ Behavior : & autoscalingv2.HorizontalPodAutoscalerBehavior {
357
+ ScaleUp : & autoscalingv2.HPAScalingRules {
358
+ StabilizationWindowSeconds : utilpointer .Int32 (100 ),
359
+ },
360
+ },
361
+ },
362
+ }
363
+
364
+ expectedHPA := autoscalingv2.HorizontalPodAutoscaler {
365
+ Spec : autoscalingv2.HorizontalPodAutoscalerSpec {
366
+ Behavior : & autoscalingv2.HorizontalPodAutoscalerBehavior {
367
+ ScaleDown : & autoscalingv2.HPAScalingRules {
368
+ SelectPolicy : & maxPolicy ,
369
+ Policies : []autoscalingv2.HPAScalingPolicy {
370
+ {Type : autoscalingv2 .PercentScalingPolicy , Value : 100 , PeriodSeconds : 15 },
371
+ },
372
+ },
373
+ ScaleUp : & autoscalingv2.HPAScalingRules {
374
+ StabilizationWindowSeconds : utilpointer .Int32 (100 ),
375
+ SelectPolicy : & maxPolicy ,
376
+ Policies : []autoscalingv2.HPAScalingPolicy {
377
+ {Type : autoscalingv2 .PodsScalingPolicy , Value : 4 , PeriodSeconds : 15 },
378
+ {Type : autoscalingv2 .PercentScalingPolicy , Value : 100 , PeriodSeconds : 15 },
379
+ },
380
+ },
381
+ },
382
+ },
383
+ }
384
+
385
+ SetDefaults_HorizontalPodAutoscalerBehavior (& hpa )
386
+ assert .Equal (t , expectedHPA , hpa )
387
+ }
388
+
241
389
func TestHorizontalPodAutoscalerAnnotations (t * testing.T ) {
242
390
tests := []struct {
243
391
hpa autoscalingv2.HorizontalPodAutoscaler
0 commit comments