Move NominatedPodsForNode to scheduling queue directly

This commit is contained in:
Maciej Skoczeń 2024-07-22 11:53:08 +00:00
parent b8dcc2c983
commit 33815db3c1
7 changed files with 70 additions and 58 deletions

View File

@ -337,7 +337,7 @@ func TestSchedulerWithExtenders(t *testing.T) {
test.registerPlugins, "", test.registerPlugins, "",
runtime.WithClientSet(client), runtime.WithClientSet(client),
runtime.WithInformerFactory(informerFactory), runtime.WithInformerFactory(informerFactory),
runtime.WithPodNominator(internalqueue.NewTestPodNominator(informerFactory.Core().V1().Pods().Lister())), runtime.WithPodNominator(internalqueue.NewSchedulingQueue(nil, informerFactory)),
runtime.WithLogger(logger), runtime.WithLogger(logger),
) )
if err != nil { if err != nil {

View File

@ -380,7 +380,7 @@ func TestPostFilter(t *testing.T) {
frameworkruntime.WithClientSet(cs), frameworkruntime.WithClientSet(cs),
frameworkruntime.WithEventRecorder(&events.FakeRecorder{}), frameworkruntime.WithEventRecorder(&events.FakeRecorder{}),
frameworkruntime.WithInformerFactory(informerFactory), frameworkruntime.WithInformerFactory(informerFactory),
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(informerFactory.Core().V1().Pods().Lister())), frameworkruntime.WithPodNominator(internalqueue.NewSchedulingQueue(nil, informerFactory)),
frameworkruntime.WithExtenders(extenders), frameworkruntime.WithExtenders(extenders),
frameworkruntime.WithSnapshotSharedLister(internalcache.NewSnapshot(tt.pods, tt.nodes)), frameworkruntime.WithSnapshotSharedLister(internalcache.NewSnapshot(tt.pods, tt.nodes)),
frameworkruntime.WithLogger(logger), frameworkruntime.WithLogger(logger),
@ -1117,7 +1117,7 @@ func TestDryRunPreemption(t *testing.T) {
fwk, err := tf.NewFramework( fwk, err := tf.NewFramework(
ctx, ctx,
registeredPlugins, "", registeredPlugins, "",
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(informerFactory.Core().V1().Pods().Lister())), frameworkruntime.WithPodNominator(internalqueue.NewSchedulingQueue(nil, informerFactory)),
frameworkruntime.WithSnapshotSharedLister(snapshot), frameworkruntime.WithSnapshotSharedLister(snapshot),
frameworkruntime.WithInformerFactory(informerFactory), frameworkruntime.WithInformerFactory(informerFactory),
frameworkruntime.WithParallelism(parallelism), frameworkruntime.WithParallelism(parallelism),
@ -1376,7 +1376,7 @@ func TestSelectBestCandidate(t *testing.T) {
tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
}, },
"", "",
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(informerFactory.Core().V1().Pods().Lister())), frameworkruntime.WithPodNominator(internalqueue.NewSchedulingQueue(nil, informerFactory)),
frameworkruntime.WithSnapshotSharedLister(snapshot), frameworkruntime.WithSnapshotSharedLister(snapshot),
frameworkruntime.WithLogger(logger), frameworkruntime.WithLogger(logger),
) )
@ -1760,7 +1760,7 @@ func TestPreempt(t *testing.T) {
frameworkruntime.WithClientSet(client), frameworkruntime.WithClientSet(client),
frameworkruntime.WithEventRecorder(&events.FakeRecorder{}), frameworkruntime.WithEventRecorder(&events.FakeRecorder{}),
frameworkruntime.WithExtenders(extenders), frameworkruntime.WithExtenders(extenders),
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(informerFactory.Core().V1().Pods().Lister())), frameworkruntime.WithPodNominator(internalqueue.NewSchedulingQueue(nil, informerFactory)),
frameworkruntime.WithSnapshotSharedLister(internalcache.NewSnapshot(test.pods, nodes)), frameworkruntime.WithSnapshotSharedLister(internalcache.NewSnapshot(test.pods, nodes)),
frameworkruntime.WithInformerFactory(informerFactory), frameworkruntime.WithInformerFactory(informerFactory),
frameworkruntime.WithWaitingPods(waitingPods), frameworkruntime.WithWaitingPods(waitingPods),

View File

@ -208,7 +208,7 @@ func TestDryRunPreemption(t *testing.T) {
fwk, err := tf.NewFramework( fwk, err := tf.NewFramework(
ctx, ctx,
registeredPlugins, "", registeredPlugins, "",
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(informerFactory.Core().V1().Pods().Lister())), frameworkruntime.WithPodNominator(internalqueue.NewSchedulingQueue(nil, informerFactory)),
frameworkruntime.WithInformerFactory(informerFactory), frameworkruntime.WithInformerFactory(informerFactory),
frameworkruntime.WithParallelism(parallelism), frameworkruntime.WithParallelism(parallelism),
frameworkruntime.WithSnapshotSharedLister(internalcache.NewSnapshot(tt.testPods, tt.nodes)), frameworkruntime.WithSnapshotSharedLister(internalcache.NewSnapshot(tt.testPods, tt.nodes)),
@ -313,7 +313,7 @@ func TestSelectCandidate(t *testing.T) {
ctx, ctx,
registeredPlugins, registeredPlugins,
"", "",
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(informerFactory.Core().V1().Pods().Lister())), frameworkruntime.WithPodNominator(internalqueue.NewSchedulingQueue(nil, informerFactory)),
frameworkruntime.WithSnapshotSharedLister(snapshot), frameworkruntime.WithSnapshotSharedLister(snapshot),
frameworkruntime.WithLogger(logger), frameworkruntime.WithLogger(logger),
) )

View File

@ -31,6 +31,8 @@ import (
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/informers"
clientsetfake "k8s.io/client-go/kubernetes/fake"
"k8s.io/component-base/metrics/testutil" "k8s.io/component-base/metrics/testutil"
"k8s.io/klog/v2/ktesting" "k8s.io/klog/v2/ktesting"
"k8s.io/kubernetes/pkg/scheduler/apis/config" "k8s.io/kubernetes/pkg/scheduler/apis/config"
@ -2385,7 +2387,20 @@ func TestFilterPluginsWithNominatedPods(t *testing.T) {
) )
} }
podNominator := internalqueue.NewTestPodNominator(nil) informerFactory := informers.NewSharedInformerFactory(clientsetfake.NewClientset(), 0)
podInformer := informerFactory.Core().V1().Pods().Informer()
err := podInformer.GetStore().Add(tt.pod)
if err != nil {
t.Fatalf("Error adding pod to podInformer: %s", err)
}
if tt.nominatedPod != nil {
err = podInformer.GetStore().Add(tt.nominatedPod)
if err != nil {
t.Fatalf("Error adding nominated pod to podInformer: %s", err)
}
}
podNominator := internalqueue.NewSchedulingQueue(nil, informerFactory)
if tt.nominatedPod != nil { if tt.nominatedPod != nil {
podNominator.AddNominatedPod( podNominator.AddNominatedPod(
logger, logger,

View File

@ -349,7 +349,7 @@ func NewPriorityQueue(
} }
pq.podBackoffQ = heap.NewWithRecorder(podInfoKeyFunc, pq.podsCompareBackoffCompleted, metrics.NewBackoffPodsRecorder()) pq.podBackoffQ = heap.NewWithRecorder(podInfoKeyFunc, pq.podsCompareBackoffCompleted, metrics.NewBackoffPodsRecorder())
pq.nsLister = informerFactory.Core().V1().Namespaces().Lister() pq.nsLister = informerFactory.Core().V1().Namespaces().Lister()
pq.nominator = newPodNominator(options.podLister, pq.nominatedPodsToInfo) pq.nominator = newPodNominator(options.podLister)
return pq return pq
} }
@ -1203,18 +1203,6 @@ func (p *PriorityQueue) nominatedPodToInfo(np PodRef) *framework.PodInfo {
return &framework.PodInfo{Pod: pod} return &framework.PodInfo{Pod: pod}
} }
func (p *PriorityQueue) nominatedPodsToInfo(nominatedPods []PodRef) []*framework.PodInfo {
p.lock.RLock()
defer p.lock.RUnlock()
p.activeQ.getLock().RLock()
defer p.activeQ.getLock().RUnlock()
pods := make([]*framework.PodInfo, len(nominatedPods))
for i, np := range nominatedPods {
pods[i] = p.nominatedPodToInfo(np).DeepCopy()
}
return pods
}
// Close closes the priority queue. // Close closes the priority queue.
func (p *PriorityQueue) Close() { func (p *PriorityQueue) Close() {
p.lock.Lock() p.lock.Lock()
@ -1241,15 +1229,27 @@ func (npm *nominator) AddNominatedPod(logger klog.Logger, pi *framework.PodInfo,
npm.nLock.Unlock() npm.nLock.Unlock()
} }
func (npm *nominator) nominatedPodsForNode(nodeName string) []PodRef {
npm.nLock.RLock()
defer npm.nLock.RUnlock()
return slices.Clone(npm.nominatedPods[nodeName])
}
// NominatedPodsForNode returns a copy of pods that are nominated to run on the given node, // NominatedPodsForNode returns a copy of pods that are nominated to run on the given node,
// but they are waiting for other pods to be removed from the node. // but they are waiting for other pods to be removed from the node.
// CAUTION: Make sure you don't call this function while taking any lock in any scenario. // CAUTION: Make sure you don't call this function while taking any queue's lock in any scenario.
func (npm *nominator) NominatedPodsForNode(nodeName string) []*framework.PodInfo { func (p *PriorityQueue) NominatedPodsForNode(nodeName string) []*framework.PodInfo {
npm.nLock.RLock() p.lock.RLock()
nominatedPods := slices.Clone(npm.nominatedPods[nodeName]) defer p.lock.RUnlock()
npm.nLock.RUnlock() nominatedPods := p.nominator.nominatedPodsForNode(nodeName)
// Note that nominatedPodsToInfo takes SchedulingQueue.lock inside.
return npm.nominatedPodsToInfo(nominatedPods) p.activeQ.getLock().RLock()
defer p.activeQ.getLock().RUnlock()
pods := make([]*framework.PodInfo, len(nominatedPods))
for i, np := range nominatedPods {
pods[i] = p.nominatedPodToInfo(np).DeepCopy()
}
return pods
} }
func (p *PriorityQueue) podsCompareBackoffCompleted(pInfo1, pInfo2 *framework.QueuedPodInfo) bool { func (p *PriorityQueue) podsCompareBackoffCompleted(pInfo1, pInfo2 *framework.QueuedPodInfo) bool {
@ -1495,12 +1495,11 @@ func (npm *nominator) UpdateNominatedPod(logger klog.Logger, oldPod *v1.Pod, new
npm.addNominatedPodUnlocked(logger, newPodInfo, nominatingInfo) npm.addNominatedPodUnlocked(logger, newPodInfo, nominatingInfo)
} }
func newPodNominator(podLister listersv1.PodLister, nominatedPodsToInfo func([]PodRef) []*framework.PodInfo) *nominator { func newPodNominator(podLister listersv1.PodLister) *nominator {
return &nominator{ return &nominator{
podLister: podLister, podLister: podLister,
nominatedPods: make(map[string][]PodRef), nominatedPods: make(map[string][]PodRef),
nominatedPodToNode: make(map[types.UID]string), nominatedPodToNode: make(map[types.UID]string),
nominatedPodsToInfo: nominatedPodsToInfo,
} }
} }

View File

@ -22,7 +22,6 @@ import (
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/informers" "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/fake"
listersv1 "k8s.io/client-go/listers/core/v1"
"k8s.io/kubernetes/pkg/scheduler/framework" "k8s.io/kubernetes/pkg/scheduler/framework"
) )
@ -54,17 +53,3 @@ func NewTestQueueWithInformerFactory(
informerFactory.WaitForCacheSync(ctx.Done()) informerFactory.WaitForCacheSync(ctx.Done())
return pq return pq
} }
// NewPodNominator creates a nominator as a backing of framework.PodNominator.
// A podLister is passed in so as to check if the pod exists
// before adding its nominatedNode info.
func NewTestPodNominator(podLister listersv1.PodLister) framework.PodNominator {
nominatedPodsToInfo := func(nominatedPods []PodRef) []*framework.PodInfo {
pods := make([]*framework.PodInfo, len(nominatedPods))
for i, np := range nominatedPods {
pods[i] = &framework.PodInfo{Pod: np.ToPod()}
}
return pods
}
return newPodNominator(podLister, nominatedPodsToInfo)
}

View File

@ -793,13 +793,14 @@ func TestSchedulerScheduleOne(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
informerFactory := informers.NewSharedInformerFactory(client, 0)
sched := &Scheduler{ sched := &Scheduler{
Cache: cache, Cache: cache,
client: client, client: client,
NextPod: func(logger klog.Logger) (*framework.QueuedPodInfo, error) { NextPod: func(logger klog.Logger) (*framework.QueuedPodInfo, error) {
return &framework.QueuedPodInfo{PodInfo: mustNewPodInfo(t, item.sendPod)}, nil return &framework.QueuedPodInfo{PodInfo: mustNewPodInfo(t, item.sendPod)}, nil
}, },
SchedulingQueue: internalqueue.NewTestQueue(ctx, nil), SchedulingQueue: internalqueue.NewSchedulingQueue(nil, informerFactory),
Profiles: profile.Map{testSchedulerName: fwk}, Profiles: profile.Map{testSchedulerName: fwk},
} }
@ -2472,7 +2473,7 @@ func TestSchedulerSchedulePod(t *testing.T) {
test.registerPlugins, "", test.registerPlugins, "",
frameworkruntime.WithSnapshotSharedLister(snapshot), frameworkruntime.WithSnapshotSharedLister(snapshot),
frameworkruntime.WithInformerFactory(informerFactory), frameworkruntime.WithInformerFactory(informerFactory),
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(informerFactory.Core().V1().Pods().Lister())), frameworkruntime.WithPodNominator(internalqueue.NewSchedulingQueue(nil, informerFactory)),
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -2538,7 +2539,7 @@ func TestFindFitAllError(t *testing.T) {
tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
}, },
"", "",
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(nil)), frameworkruntime.WithPodNominator(internalqueue.NewTestQueue(ctx, nil)),
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -2581,7 +2582,7 @@ func TestFindFitSomeError(t *testing.T) {
tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
}, },
"", "",
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(nil)), frameworkruntime.WithPodNominator(internalqueue.NewTestQueue(ctx, nil)),
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -2652,10 +2653,18 @@ func TestFindFitPredicateCallCounts(t *testing.T) {
logger, ctx := ktesting.NewTestContext(t) logger, ctx := ktesting.NewTestContext(t)
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
informerFactory := informers.NewSharedInformerFactory(clientsetfake.NewClientset(), 0)
podInformer := informerFactory.Core().V1().Pods().Informer()
err := podInformer.GetStore().Add(test.pod)
if err != nil {
t.Fatalf("Error adding pod to podInformer: %s", err)
}
fwk, err := tf.NewFramework( fwk, err := tf.NewFramework(
ctx, ctx,
registerPlugins, "", registerPlugins, "",
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(nil)), frameworkruntime.WithPodNominator(internalqueue.NewSchedulingQueue(nil, informerFactory)),
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -2669,6 +2678,10 @@ func TestFindFitPredicateCallCounts(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = podInformer.GetStore().Add(podinfo.Pod)
if err != nil {
t.Fatalf("Error adding nominated pod to podInformer: %s", err)
}
fwk.AddNominatedPod(logger, podinfo, &framework.NominatingInfo{NominatingMode: framework.ModeOverride, NominatedNodeName: "1"}) fwk.AddNominatedPod(logger, podinfo, &framework.NominatingInfo{NominatingMode: framework.ModeOverride, NominatedNodeName: "1"})
_, _, err = scheduler.findNodesThatFitPod(ctx, fwk, framework.NewCycleState(), test.pod) _, _, err = scheduler.findNodesThatFitPod(ctx, fwk, framework.NewCycleState(), test.pod)
@ -2796,7 +2809,7 @@ func TestZeroRequest(t *testing.T) {
frameworkruntime.WithInformerFactory(informerFactory), frameworkruntime.WithInformerFactory(informerFactory),
frameworkruntime.WithSnapshotSharedLister(snapshot), frameworkruntime.WithSnapshotSharedLister(snapshot),
frameworkruntime.WithClientSet(client), frameworkruntime.WithClientSet(client),
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(informerFactory.Core().V1().Pods().Lister())), frameworkruntime.WithPodNominator(internalqueue.NewSchedulingQueue(nil, informerFactory)),
) )
if err != nil { if err != nil {
t.Fatalf("error creating framework: %+v", err) t.Fatalf("error creating framework: %+v", err)
@ -3199,7 +3212,7 @@ func Test_prioritizeNodes(t *testing.T) {
frameworkruntime.WithInformerFactory(informerFactory), frameworkruntime.WithInformerFactory(informerFactory),
frameworkruntime.WithSnapshotSharedLister(snapshot), frameworkruntime.WithSnapshotSharedLister(snapshot),
frameworkruntime.WithClientSet(client), frameworkruntime.WithClientSet(client),
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(informerFactory.Core().V1().Pods().Lister())), frameworkruntime.WithPodNominator(internalqueue.NewSchedulingQueue(nil, informerFactory)),
) )
if err != nil { if err != nil {
t.Fatalf("error creating framework: %+v", err) t.Fatalf("error creating framework: %+v", err)
@ -3317,7 +3330,7 @@ func TestFairEvaluationForNodes(t *testing.T) {
tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
}, },
"", "",
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(nil)), frameworkruntime.WithPodNominator(internalqueue.NewTestQueue(ctx, nil)),
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -3399,7 +3412,7 @@ func TestPreferNominatedNodeFilterCallCounts(t *testing.T) {
ctx, ctx,
registerPlugins, "", registerPlugins, "",
frameworkruntime.WithClientSet(client), frameworkruntime.WithClientSet(client),
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(informerFactory.Core().V1().Pods().Lister())), frameworkruntime.WithPodNominator(internalqueue.NewSchedulingQueue(nil, informerFactory)),
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -3557,7 +3570,7 @@ func setupTestScheduler(ctx context.Context, t *testing.T, queuedPodStore *clien
frameworkruntime.WithClientSet(client), frameworkruntime.WithClientSet(client),
frameworkruntime.WithEventRecorder(recorder), frameworkruntime.WithEventRecorder(recorder),
frameworkruntime.WithInformerFactory(informerFactory), frameworkruntime.WithInformerFactory(informerFactory),
frameworkruntime.WithPodNominator(internalqueue.NewTestPodNominator(informerFactory.Core().V1().Pods().Lister())), frameworkruntime.WithPodNominator(schedulingQueue),
frameworkruntime.WithWaitingPods(waitingPods), frameworkruntime.WithWaitingPods(waitingPods),
) )