37 lines
839 B
Go
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
|
|
}
|