From 59d1d5d1b97f349e0fd873f324678cb42aadc19c Mon Sep 17 00:00:00 2001 From: googs1025 Date: Wed, 12 Feb 2025 15:45:19 +0800 Subject: [PATCH] making isEvictable and hasSelectorOrAffinity invoked only once Signed-off-by: googs1025 --- .../topologyspreadconstraint.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pkg/framework/plugins/removepodsviolatingtopologyspreadconstraint/topologyspreadconstraint.go b/pkg/framework/plugins/removepodsviolatingtopologyspreadconstraint/topologyspreadconstraint.go index 1adf3d731..cc460106c 100644 --- a/pkg/framework/plugins/removepodsviolatingtopologyspreadconstraint/topologyspreadconstraint.go +++ b/pkg/framework/plugins/removepodsviolatingtopologyspreadconstraint/topologyspreadconstraint.go @@ -417,18 +417,23 @@ func sortDomains(constraintTopologyPairs map[topologyPair][]*v1.Pod, isEvictable // followed by the highest priority pods with affinity or nodeSelector sort.Slice(list, func(i, j int) bool { // any non-evictable pods should be considered last (ie, first in the list) - if !isEvictable(list[i]) || !isEvictable(list[j]) { + evictableI := isEvictable(list[i]) + evictableJ := isEvictable(list[j]) + + if !evictableI || !evictableJ { // false - i is the only non-evictable, so return true to put it first // true - j is non-evictable, so return false to put j before i // if true and both and non-evictable, order doesn't matter - return !(isEvictable(list[i]) && !isEvictable(list[j])) + return !(evictableI && !evictableJ) } + hasSelectorOrAffinityI := hasSelectorOrAffinity(*list[i]) + hasSelectorOrAffinityJ := hasSelectorOrAffinity(*list[j]) // if both pods have selectors/affinity, compare them by their priority - if hasSelectorOrAffinity(*list[i]) == hasSelectorOrAffinity(*list[j]) { + if hasSelectorOrAffinityI == hasSelectorOrAffinityJ { // Sort by priority in ascending order (lower priority Pods first) return !comparePodsByPriority(list[i], list[j]) } - return hasSelectorOrAffinity(*list[i]) && !hasSelectorOrAffinity(*list[j]) + return hasSelectorOrAffinityI && !hasSelectorOrAffinityJ }) sortedTopologies = append(sortedTopologies, topology{pair: pair, pods: list}) }