Weisen Pan a877aed45f AI-based CFN Traffic Control and Computer Force Scheduling
Change-Id: I16cd7730c1e0732253ac52f51010f6b813295aa7
2023-11-03 00:09:19 -07:00

37 lines
839 B
Go

package algo
// Author: Weisen Pan
// Date: 2023-10-24
import (
corev1 "k8s.io/api/core/v1"
)
// AffinityQueue is a data structure used to sort pods by Affinity.
type AffinityQueue struct {
pods []*corev1.Pod
}
// NewAffinityQueue creates and returns a new AffinityQueue instance.
func NewAffinityQueue(pods []*corev1.Pod) *AffinityQueue {
return &AffinityQueue{
pods: pods,
}
}
// Len returns the number of pods in the AffinityQueue.
func (aff *AffinityQueue) Len() int {
return len(aff.pods)
}
// Swap swaps the positions of two pods in the AffinityQueue.
func (aff *AffinityQueue) Swap(i, j int) {
aff.pods[i], aff.pods[j] = aff.pods[j], aff.pods[i]
}
// Less returns true if the pod at index 'i' has a non-nil NodeSelector.
func (aff *AffinityQueue) Less(i, j int) bool {
return aff.pods[i].Spec.NodeSelector != nil
}