Skip to content

Commit

Permalink
Merge pull request #119256 from brianpursley/kubectl-1409
Browse files Browse the repository at this point in the history
Prefer pods without a deletion timestamp over pods with a deletion timestamp when getting an ordered list of active pods

Kubernetes-commit: 4575facd23b984c05ed02aac8715cc64b99de148
  • Loading branch information
k8s-publishing-bot committed Jul 18, 2023
2 parents 4de80e9 + 70109f0 commit 1971237
Show file tree
Hide file tree
Showing 2 changed files with 400 additions and 3 deletions.
18 changes: 15 additions & 3 deletions pkg/util/podutils/podutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func IsPodReady(pod *corev1.Pod) bool {
return isPodReadyConditionTrue(pod.Status)
}

func isPodDeleting(pod *corev1.Pod) bool {
return pod.DeletionTimestamp != nil
}

// IsPodReadyConditionTrue returns true if a pod is ready; false otherwise.
func isPodReadyConditionTrue(status corev1.PodStatus) bool {
condition := getPodReadyCondition(status)
Expand Down Expand Up @@ -142,18 +146,26 @@ func (s ActivePods) Less(i, j int) bool {
if IsPodReady(s[i]) != IsPodReady(s[j]) {
return !IsPodReady(s[i])
}
// 4. Deleting < Not deleting
if isPodDeleting(s[i]) != isPodDeleting(s[j]) {
return isPodDeleting(s[i])
}
// 5. Older deletion timestamp < newer deletion timestamp
if isPodDeleting(s[i]) && isPodDeleting(s[j]) && !s[i].ObjectMeta.DeletionTimestamp.Equal(s[j].ObjectMeta.DeletionTimestamp) {
return s[i].ObjectMeta.DeletionTimestamp.Before(s[j].ObjectMeta.DeletionTimestamp)
}
// TODO: take availability into account when we push minReadySeconds information from deployment into pods,
// see https://github.com/kubernetes/kubernetes/issues/22065
// 4. Been ready for empty time < less time < more time
// 6. Been ready for empty time < less time < more time
// If both pods are ready, the latest ready one is smaller
if IsPodReady(s[i]) && IsPodReady(s[j]) && !podReadyTime(s[i]).Equal(podReadyTime(s[j])) {
return afterOrZero(podReadyTime(s[i]), podReadyTime(s[j]))
}
// 5. Pods with containers with higher restart counts < lower restart counts
// 7. Pods with containers with higher restart counts < lower restart counts
if maxContainerRestarts(s[i]) != maxContainerRestarts(s[j]) {
return maxContainerRestarts(s[i]) > maxContainerRestarts(s[j])
}
// 6. Empty creation time pods < newer pods < older pods
// 8. Empty creation time pods < newer pods < older pods
if !s[i].CreationTimestamp.Equal(&s[j].CreationTimestamp) {
return afterOrZero(&s[i].CreationTimestamp, &s[j].CreationTimestamp)
}
Expand Down
Loading

0 comments on commit 1971237

Please sign in to comment.