Skip to content

Commit 4f7ae54

Browse files
committed
fixup: add podLister as a member field of DefaultPreemption
1 parent 52bf6ba commit 4f7ae54

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

pkg/scheduler/framework/plugins/defaultpreemption/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ go_library(
2424
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
2525
"//staging/src/k8s.io/client-go/informers:go_default_library",
2626
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
27+
"//staging/src/k8s.io/client-go/listers/core/v1:go_default_library",
2728
"//staging/src/k8s.io/client-go/listers/policy/v1beta1:go_default_library",
2829
"//staging/src/k8s.io/kube-scheduler/extender/v1:go_default_library",
2930
"//vendor/k8s.io/klog/v2:go_default_library",

pkg/scheduler/framework/plugins/defaultpreemption/default_preemption.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
utilfeature "k8s.io/apiserver/pkg/util/feature"
3434
"k8s.io/client-go/informers"
3535
"k8s.io/client-go/kubernetes"
36+
corelisters "k8s.io/client-go/listers/core/v1"
3637
policylisters "k8s.io/client-go/listers/policy/v1beta1"
3738
extenderv1 "k8s.io/kube-scheduler/extender/v1"
3839
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
@@ -52,6 +53,7 @@ const (
5253
// DefaultPreemption is a PostFilter plugin implements the preemption logic.
5354
type DefaultPreemption struct {
5455
fh framework.FrameworkHandle
56+
podLister corelisters.PodLister
5557
pdbLister policylisters.PodDisruptionBudgetLister
5658
}
5759

@@ -66,6 +68,7 @@ func (pl *DefaultPreemption) Name() string {
6668
func New(_ runtime.Object, fh framework.FrameworkHandle) (framework.Plugin, error) {
6769
pl := DefaultPreemption{
6870
fh: fh,
71+
podLister: fh.SharedInformerFactory().Core().V1().Pods().Lister(),
6972
pdbLister: getPDBLister(fh.SharedInformerFactory()),
7073
}
7174
return &pl, nil
@@ -109,7 +112,7 @@ func (pl *DefaultPreemption) preempt(ctx context.Context, state *framework.Cycle
109112
// It's safe to directly fetch pod here. Because the informer cache has already been
110113
// initialized when creating the Scheduler obj, i.e., factory.go#MakeDefaultErrorFunc().
111114
// However, tests may need to manually initialize the shared pod informer.
112-
pod, err := pl.fh.SharedInformerFactory().Core().V1().Pods().Lister().Pods(pod.Namespace).Get(pod.Name)
115+
pod, err := pl.podLister.Pods(pod.Namespace).Get(pod.Name)
113116
if err != nil {
114117
klog.Errorf("Error getting the updated preemptor pod object: %v", err)
115118
return "", err

pkg/scheduler/framework/plugins/defaultpreemption/default_preemption_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,18 @@ func TestPostFilter(t *testing.T) {
218218
if err != nil {
219219
t.Fatal(err)
220220
}
221+
p := DefaultPreemption{
222+
fh: f,
223+
podLister: informerFactory.Core().V1().Pods().Lister(),
224+
pdbLister: getPDBLister(informerFactory),
225+
}
221226

222227
state := framework.NewCycleState()
223228
// Ensure <state> is populated.
224229
if status := f.RunPreFilterPlugins(context.Background(), state, tt.pod); !status.IsSuccess() {
225230
t.Errorf("Unexpected PreFilter Status: %v", status)
226231
}
227232

228-
p := DefaultPreemption{
229-
fh: f,
230-
pdbLister: getPDBLister(informerFactory),
231-
}
232233
gotResult, gotStatus := p.PostFilter(context.TODO(), state, tt.pod, tt.filteredNodesStatuses)
233234
if !reflect.DeepEqual(gotStatus, tt.wantStatus) {
234235
t.Errorf("Status does not match: %v, want: %v", gotStatus, tt.wantStatus)
@@ -1307,6 +1308,7 @@ func TestPreempt(t *testing.T) {
13071308
// Call preempt and check the expected results.
13081309
pl := DefaultPreemption{
13091310
fh: fwk,
1311+
podLister: informerFactory.Core().V1().Pods().Lister(),
13101312
pdbLister: getPDBLister(informerFactory),
13111313
}
13121314
node, err := pl.preempt(context.Background(), state, test.pod, make(framework.NodeToStatusMap))

0 commit comments

Comments
 (0)