1
0
mirror of https://github.com/kubernetes-sigs/descheduler.git synced 2026-01-26 13:29:11 +01:00

use pod informers for listing pods in removepodsviolatingtopologyspreadconstraint and removepodsviolatinginterpodantiaffinity (#1163)

* use pod informers for listing pods in removepodsviolatingtopologyspreadconstraint and removepodsviolatinginterpodantiaffinity

Signed-off-by: Amir Alavi <amiralavi7@gmail.com>

* workaround in topologyspreadconstraint test to ensure that informer's index returns pods sorted by name

---------

Signed-off-by: Amir Alavi <amiralavi7@gmail.com>
This commit is contained in:
Amir Alavi
2023-06-16 02:30:19 -04:00
committed by GitHub
parent 5163ac3ace
commit f5a7f716b3
4 changed files with 61 additions and 51 deletions

View File

@@ -78,20 +78,15 @@ func (d *RemovePodsViolatingInterPodAntiAffinity) Name() string {
}
func (d *RemovePodsViolatingInterPodAntiAffinity) Deschedule(ctx context.Context, nodes []*v1.Node) *frameworktypes.Status {
podsList, err := d.handle.ClientSet().CoreV1().Pods("").List(ctx, metav1.ListOptions{})
pods, err := podutil.ListPodsOnNodes(nodes, d.handle.GetPodsAssignedToNodeFunc(), d.podFilter)
if err != nil {
return &frameworktypes.Status{
Err: fmt.Errorf("error listing all pods: %v", err),
}
}
podsInANamespace := groupByNamespace(podsList)
runningPodFilter := func(pod *v1.Pod) bool {
return pod.Status.Phase != v1.PodSucceeded && pod.Status.Phase != v1.PodFailed
}
podsOnANode := groupByNodeName(podsList, podutil.WrapFilterFuncs(runningPodFilter, d.podFilter))
podsInANamespace := podutil.GroupByNamespace(pods)
podsOnANode := groupByNodeName(pods)
nodeMap := createNodeMap(nodes)
loop:
@@ -136,25 +131,11 @@ func removePodFromNamespaceMap(podToRemove *v1.Pod, podMap map[string][]*v1.Pod)
return podMap
}
func groupByNamespace(pods *v1.PodList) map[string][]*v1.Pod {
func groupByNodeName(pods []*v1.Pod) map[string][]*v1.Pod {
m := make(map[string][]*v1.Pod)
for i := 0; i < len(pods.Items); i++ {
pod := &(pods.Items[i])
m[pod.Namespace] = append(m[pod.Namespace], pod)
}
return m
}
func groupByNodeName(pods *v1.PodList, filter podutil.FilterFunc) map[string][]*v1.Pod {
m := make(map[string][]*v1.Pod)
if filter == nil {
filter = func(p *v1.Pod) bool { return true }
}
for i := 0; i < len(pods.Items); i++ {
pod := &(pods.Items[i])
if filter(pod) {
m[pod.Spec.NodeName] = append(m[pod.Spec.NodeName], pod)
}
for i := 0; i < len(pods); i++ {
pod := pods[i]
m[pod.Spec.NodeName] = append(m[pod.Spec.NodeName], pod)
}
return m
}