Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: affinity priority #1548

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/controllers/provisioning/scheduling/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"

"sigs.k8s.io/karpenter/pkg/utils/pod"
"sigs.k8s.io/karpenter/pkg/utils/resources"
)

Expand Down Expand Up @@ -96,6 +97,15 @@ func byCPUAndMemoryDescending(pods []*v1.Pod) func(i int, j int) bool {
return true
}

// anti-affinity pods should be sorted before normal pods
if affinityCmp := pod.PodAffinityCmp(lhsPod, rhsPod); affinityCmp != 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like the right move, but I'm not sure how this breaks down in our bin-packing algorithm. From what I understand, this just sorts pods with affinity + tsc before others with the same exact pod requests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, after testing this approach (there is a small test case in the previous section), scheduling the mutually exclusive pods further ahead helps to get a more balanced scheduling result

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this approach, the cluster will be more stable (e.g., draining one node will not cause most pods to be rescheduled). I observed that Karpenter attempts to distribute the pods across all nodes:
Scheduler Code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @njtran @jonathan-innis , please take a look

return affinityCmp > 0
}

if len(lhsPod.Spec.TopologySpreadConstraints) != len(rhsPod.Spec.TopologySpreadConstraints) {
return len(lhsPod.Spec.TopologySpreadConstraints) > len(rhsPod.Spec.TopologySpreadConstraints)
}

// If all else is equal, give a consistent ordering. This reduces the number of NominatePod events as we
// de-duplicate those based on identical content.

Expand Down
35 changes: 35 additions & 0 deletions pkg/utils/pod/scheduling.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,38 @@ func HasPodAntiAffinity(pod *corev1.Pod) bool {
(len(pod.Spec.Affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution) != 0 ||
len(pod.Spec.Affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution) != 0)
}

// PodAffinityCmp compares two pods based on their affinity
func PodAffinityCmp(lhsPod *corev1.Pod, rhsPod *corev1.Pod) int {
if lhsPod.Spec.Affinity != nil && rhsPod.Spec.Affinity != nil {
if HasRequiredPodAntiAffinity(lhsPod) && !HasRequiredPodAntiAffinity(rhsPod) {
return 1
} else if !HasRequiredPodAntiAffinity(lhsPod) && HasRequiredPodAntiAffinity(rhsPod) {
return -1
} else if HasRequiredPodAntiAffinity(lhsPod) && HasRequiredPodAntiAffinity(rhsPod) {
return PodAntiAffinityCmp(lhsPod, rhsPod)
}
}

return 0
}

// PodAntiAffinityCmp compares two pods based on their the size of their anti-affinity constraints
func PodAntiAffinityCmp(lhsPod *corev1.Pod, rhsPod *corev1.Pod) int {
lPodAntiAffinity := lhsPod.Spec.Affinity.PodAntiAffinity
rPodAntiAffinity := rhsPod.Spec.Affinity.PodAntiAffinity
if len(lPodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution) > len(rPodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution) {
return 1
} else if len(lPodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution) <
len(rPodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution) {
return -1
}

if len(lPodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution) > len(rPodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution) {
return 1
} else if len(lPodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution) < len(rPodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution) {
return -1
}

return 0
}
Loading