@@ -1401,3 +1401,121 @@ func TestMinAvailable(t *testing.T) {
1401
1401
})
1402
1402
}
1403
1403
}
1404
+
1405
+ func TestGetReplicaSetFraction (t * testing.T ) {
1406
+ tests := []struct {
1407
+ name string
1408
+ enableDeploymentPodReplacementPolicy bool
1409
+ deploymentReplicas int32
1410
+ deploymentStatusReplicas int32
1411
+ deploymentMaxSurge int32
1412
+ rsReplicas int32
1413
+ rsAnnotations map [string ]string
1414
+ expectedFraction int32
1415
+ }{
1416
+ {
1417
+ name : "empty deployment always scales to 0" ,
1418
+ deploymentReplicas : 0 ,
1419
+ rsReplicas : 10 ,
1420
+ expectedFraction : - 10 ,
1421
+ },
1422
+ {
1423
+ name : "unsynced deployment does not scale when max-replicas annotation is missing (removed by a 3rd party)" ,
1424
+ deploymentReplicas : 10 ,
1425
+ rsReplicas : 5 ,
1426
+ expectedFraction : 0 ,
1427
+ },
1428
+ {
1429
+ name : "unsynced deployment does not scale when max-replicas annotation is incorrectly set to 0 (by a 3rd party)" ,
1430
+ deploymentReplicas : 10 ,
1431
+ rsReplicas : 5 ,
1432
+ rsAnnotations : map [string ]string {
1433
+ MaxReplicasAnnotation : "0" ,
1434
+ },
1435
+ expectedFraction : 0 ,
1436
+ },
1437
+ {
1438
+ name : "scale up by 1/5 should increase RS replicas by 1/5 when max-replicas annotation is missing (removed by a 3rd party)" ,
1439
+ deploymentReplicas : 120 ,
1440
+ deploymentStatusReplicas : 100 ,
1441
+ rsReplicas : 50 ,
1442
+ expectedFraction : 10 ,
1443
+ },
1444
+ {
1445
+ name : "scale up by 1/5 should increase RS replicas by 1/5 when max-replicas annotation is incorrectly set to 0 (by a 3rd party)" ,
1446
+ deploymentReplicas : 120 ,
1447
+ deploymentStatusReplicas : 100 ,
1448
+ rsReplicas : 50 ,
1449
+ rsAnnotations : map [string ]string {
1450
+ MaxReplicasAnnotation : "0" ,
1451
+ },
1452
+ expectedFraction : 10 ,
1453
+ },
1454
+ {
1455
+ name : "scale up by 1/5 should increase RS replicas by 1/5" ,
1456
+ deploymentReplicas : 120 ,
1457
+ rsReplicas : 50 ,
1458
+ rsAnnotations : map [string ]string {
1459
+ MaxReplicasAnnotation : "100" ,
1460
+ },
1461
+ expectedFraction : 10 ,
1462
+ },
1463
+ {
1464
+ name : "scale up with maxSurge by 1/5 should increase RS replicas approximately by 1/5" ,
1465
+ deploymentReplicas : 120 ,
1466
+ deploymentMaxSurge : 10 ,
1467
+ rsReplicas : 50 ,
1468
+ rsAnnotations : map [string ]string {
1469
+ MaxReplicasAnnotation : "110" ,
1470
+ },
1471
+ // expectedFraction is not the whole 1/5 (10) since maxSurge pods have to be taken into account
1472
+ // and replica sets with these surge pods should proportionally scale as well during a rollout
1473
+ expectedFraction : 9 ,
1474
+ },
1475
+ {
1476
+ name : "scale down by 1/6 should decrease RS replicas by 1/6" ,
1477
+ deploymentReplicas : 10 ,
1478
+ rsReplicas : 6 ,
1479
+ rsAnnotations : map [string ]string {
1480
+ MaxReplicasAnnotation : "12" ,
1481
+ },
1482
+ expectedFraction : - 1 ,
1483
+ },
1484
+ {
1485
+ name : "scale down with maxSurge by 1/6 should decrease RS replicas approximately by 1/6" ,
1486
+ deploymentReplicas : 100 ,
1487
+ deploymentMaxSurge : 10 ,
1488
+ rsReplicas : 50 ,
1489
+ rsAnnotations : map [string ]string {
1490
+ MaxReplicasAnnotation : "130" ,
1491
+ },
1492
+ expectedFraction : - 8 ,
1493
+ },
1494
+ }
1495
+
1496
+ for _ , test := range tests {
1497
+ t .Run (test .name , func (t * testing.T ) {
1498
+ logger , _ := ktesting .NewTestContext (t )
1499
+
1500
+ tDeployment := generateDeployment ("nginx" )
1501
+ tDeployment .Status .Replicas = test .deploymentStatusReplicas
1502
+ tDeployment .Spec .Replicas = ptr .To (test .deploymentReplicas )
1503
+ tDeployment .Spec .Strategy = apps.DeploymentStrategy {
1504
+ Type : apps .RollingUpdateDeploymentStrategyType ,
1505
+ RollingUpdate : & apps.RollingUpdateDeployment {
1506
+ MaxSurge : ptr .To (intstr .FromInt32 (test .deploymentMaxSurge )),
1507
+ MaxUnavailable : ptr .To (intstr .FromInt32 (1 )),
1508
+ },
1509
+ }
1510
+
1511
+ tRS := generateRS (tDeployment )
1512
+ tRS .Annotations = test .rsAnnotations
1513
+ tRS .Spec .Replicas = ptr .To (test .rsReplicas )
1514
+
1515
+ fraction := getReplicaSetFraction (logger , tRS , tDeployment )
1516
+ if test .expectedFraction != fraction {
1517
+ t .Fatalf ("expected fraction: %v, got:%v" , test .expectedFraction , fraction )
1518
+ }
1519
+ })
1520
+ }
1521
+ }
0 commit comments