Skip to content

Commit 57eef32

Browse files
authored
Merge pull request kubernetes#79657 from josephburnett/hpastuck
Ignore unschedulable pods
2 parents 978c38d + 80e279d commit 57eef32

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

pkg/controller/podautoscaler/replica_calculator.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,18 @@ func groupPods(pods []*v1.Pod, metrics metricsclient.PodMetricsInfo, resource v1
365365
if pod.DeletionTimestamp != nil || pod.Status.Phase == v1.PodFailed {
366366
continue
367367
}
368+
// Pending pods are ignored.
369+
if pod.Status.Phase == v1.PodPending {
370+
ignoredPods.Insert(pod.Name)
371+
continue
372+
}
373+
// Pods missing metrics.
368374
metric, found := metrics[pod.Name]
369375
if !found {
370376
missingPods.Insert(pod.Name)
371377
continue
372378
}
379+
// Unready pods are ignored.
373380
if resource == v1.ResourceCPU {
374381
var ignorePod bool
375382
_, condition := podutil.GetPodCondition(&pod.Status, v1.PodReady)

pkg/controller/podautoscaler/replica_calculator_test.go

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,25 @@ func TestReplicaCalcScaleDownIncludeUnreadyPods(t *testing.T) {
862862
tc.runTest(t)
863863
}
864864

865+
func TestReplicaCalcScaleDownExcludeUnscheduledPods(t *testing.T) {
866+
tc := replicaCalcTestCase{
867+
currentReplicas: 5,
868+
expectedReplicas: 1,
869+
podReadiness: []v1.ConditionStatus{v1.ConditionTrue, v1.ConditionFalse, v1.ConditionFalse, v1.ConditionFalse, v1.ConditionFalse},
870+
podPhase: []v1.PodPhase{v1.PodRunning, v1.PodPending, v1.PodPending, v1.PodPending, v1.PodPending},
871+
resource: &resourceInfo{
872+
name: v1.ResourceCPU,
873+
requests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
874+
levels: []int64{100},
875+
876+
targetUtilization: 50,
877+
expectedUtilization: 10,
878+
expectedValue: numContainersPerPod * 100,
879+
},
880+
}
881+
tc.runTest(t)
882+
}
883+
865884
func TestReplicaCalcScaleDownIgnoreHotCpuPods(t *testing.T) {
866885
tc := replicaCalcTestCase{
867886
currentReplicas: 5,
@@ -1616,18 +1635,38 @@ func TestGroupPods(t *testing.T) {
16161635
sets.NewString("lucretius"),
16171636
sets.NewString("epicurus"),
16181637
},
1638+
{
1639+
name: "pending pods are ignored",
1640+
pods: []*v1.Pod{
1641+
{
1642+
ObjectMeta: metav1.ObjectMeta{
1643+
Name: "unscheduled",
1644+
},
1645+
Status: v1.PodStatus{
1646+
Phase: v1.PodPending,
1647+
},
1648+
},
1649+
},
1650+
metrics: metricsclient.PodMetricsInfo{},
1651+
resource: v1.ResourceCPU,
1652+
expectReadyPodCount: 0,
1653+
expectIgnoredPods: sets.NewString("unscheduled"),
1654+
expectMissingPods: sets.NewString(),
1655+
},
16191656
}
16201657
for _, tc := range tests {
1621-
readyPodCount, ignoredPods, missingPods := groupPods(tc.pods, tc.metrics, tc.resource, defaultTestingCpuInitializationPeriod, defaultTestingDelayOfInitialReadinessStatus)
1622-
if readyPodCount != tc.expectReadyPodCount {
1623-
t.Errorf("%s got readyPodCount %d, expected %d", tc.name, readyPodCount, tc.expectReadyPodCount)
1624-
}
1625-
if !ignoredPods.Equal(tc.expectIgnoredPods) {
1626-
t.Errorf("%s got unreadyPods %v, expected %v", tc.name, ignoredPods, tc.expectIgnoredPods)
1627-
}
1628-
if !missingPods.Equal(tc.expectMissingPods) {
1629-
t.Errorf("%s got missingPods %v, expected %v", tc.name, missingPods, tc.expectMissingPods)
1630-
}
1658+
t.Run(tc.name, func(t *testing.T) {
1659+
readyPodCount, ignoredPods, missingPods := groupPods(tc.pods, tc.metrics, tc.resource, defaultTestingCpuInitializationPeriod, defaultTestingDelayOfInitialReadinessStatus)
1660+
if readyPodCount != tc.expectReadyPodCount {
1661+
t.Errorf("%s got readyPodCount %d, expected %d", tc.name, readyPodCount, tc.expectReadyPodCount)
1662+
}
1663+
if !ignoredPods.Equal(tc.expectIgnoredPods) {
1664+
t.Errorf("%s got unreadyPods %v, expected %v", tc.name, ignoredPods, tc.expectIgnoredPods)
1665+
}
1666+
if !missingPods.Equal(tc.expectMissingPods) {
1667+
t.Errorf("%s got missingPods %v, expected %v", tc.name, missingPods, tc.expectMissingPods)
1668+
}
1669+
})
16311670
}
16321671
}
16331672

0 commit comments

Comments
 (0)