Skip to content

Commit 59d1d5d

Browse files
committed
making isEvictable and hasSelectorOrAffinity invoked only once
Signed-off-by: googs1025 <[email protected]>
1 parent 4548723 commit 59d1d5d

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

pkg/framework/plugins/removepodsviolatingtopologyspreadconstraint/topologyspreadconstraint.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -417,18 +417,23 @@ func sortDomains(constraintTopologyPairs map[topologyPair][]*v1.Pod, isEvictable
417417
// followed by the highest priority pods with affinity or nodeSelector
418418
sort.Slice(list, func(i, j int) bool {
419419
// any non-evictable pods should be considered last (ie, first in the list)
420-
if !isEvictable(list[i]) || !isEvictable(list[j]) {
420+
evictableI := isEvictable(list[i])
421+
evictableJ := isEvictable(list[j])
422+
423+
if !evictableI || !evictableJ {
421424
// false - i is the only non-evictable, so return true to put it first
422425
// true - j is non-evictable, so return false to put j before i
423426
// if true and both and non-evictable, order doesn't matter
424-
return !(isEvictable(list[i]) && !isEvictable(list[j]))
427+
return !(evictableI && !evictableJ)
425428
}
429+
hasSelectorOrAffinityI := hasSelectorOrAffinity(*list[i])
430+
hasSelectorOrAffinityJ := hasSelectorOrAffinity(*list[j])
426431
// if both pods have selectors/affinity, compare them by their priority
427-
if hasSelectorOrAffinity(*list[i]) == hasSelectorOrAffinity(*list[j]) {
432+
if hasSelectorOrAffinityI == hasSelectorOrAffinityJ {
428433
// Sort by priority in ascending order (lower priority Pods first)
429434
return !comparePodsByPriority(list[i], list[j])
430435
}
431-
return hasSelectorOrAffinity(*list[i]) && !hasSelectorOrAffinity(*list[j])
436+
return hasSelectorOrAffinityI && !hasSelectorOrAffinityJ
432437
})
433438
sortedTopologies = append(sortedTopologies, topology{pair: pair, pods: list})
434439
}

0 commit comments

Comments
 (0)