Skip to content

Commit

Permalink
Merge pull request kubernetes#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
  • Loading branch information
k8s-ci-robot committed Jul 18, 2023
2 parents 56b59c8 + d281dae commit 4575fac
Show file tree
Hide file tree
Showing 2 changed files with 400 additions and 3 deletions.
18 changes: 15 additions & 3 deletions staging/src/k8s.io/kubectl/pkg/util/podutils/podutils.go
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

0 comments on commit 4575fac

Please sign in to comment.