mirror of
https://github.com/kubernetes-sigs/descheduler.git
synced 2026-04-05 18:27:16 +00:00
Extract shared container state matching helpers into podutil
Move container waiting/terminated state checking from PodLifeTime and RemovePodsHavingTooManyRestarts into podutil as separate exported helpers: HasMatchingContainerWaitingState and HasMatchingContainerTerminatedState. Each plugin composes only the helpers it needs.
This commit is contained in:
@@ -292,6 +292,28 @@ func SortPodsBasedOnAge(pods []*v1.Pod) {
|
||||
})
|
||||
}
|
||||
|
||||
// HasMatchingContainerWaitingState returns true if any container status has a
|
||||
// waiting reason present in the given set.
|
||||
func HasMatchingContainerWaitingState(statuses []v1.ContainerStatus, states sets.Set[string]) bool {
|
||||
for _, cs := range statuses {
|
||||
if cs.State.Waiting != nil && states.Has(cs.State.Waiting.Reason) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// HasMatchingContainerTerminatedState returns true if any container status has
|
||||
// a terminated reason present in the given set.
|
||||
func HasMatchingContainerTerminatedState(statuses []v1.ContainerStatus, states sets.Set[string]) bool {
|
||||
for _, cs := range statuses {
|
||||
if cs.State.Terminated != nil && states.Has(cs.State.Terminated.Reason) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func GroupByNodeName(pods []*v1.Pod) map[string][]*v1.Pod {
|
||||
m := make(map[string][]*v1.Pod)
|
||||
for i := 0; i < len(pods); i++ {
|
||||
|
||||
@@ -84,19 +84,13 @@ func New(ctx context.Context, args runtime.Object, handle frameworktypes.Handle)
|
||||
if states.Has(string(pod.Status.Phase)) {
|
||||
return true
|
||||
}
|
||||
|
||||
containerStatuses := pod.Status.ContainerStatuses
|
||||
|
||||
if tooManyRestartsArgs.IncludingInitContainers {
|
||||
containerStatuses = append(containerStatuses, pod.Status.InitContainerStatuses...)
|
||||
if podutil.HasMatchingContainerWaitingState(pod.Status.ContainerStatuses, states) {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, containerStatus := range containerStatuses {
|
||||
if containerStatus.State.Waiting != nil && states.Has(containerStatus.State.Waiting.Reason) {
|
||||
return true
|
||||
}
|
||||
if tooManyRestartsArgs.IncludingInitContainers &&
|
||||
podutil.HasMatchingContainerWaitingState(pod.Status.InitContainerStatuses, states) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user