Skip to content

Commit 1b30105

Browse files
Huang-Weirenjian52
andcommitted
Fix scheduler cache inconsistency upon Pods with same name but different UIDs
Co-authored-by: Wei Huang <[email protected]> Co-authored-by: tianxia52 <[email protected]>
1 parent ab207be commit 1b30105

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

pkg/scheduler/eventhandlers.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ func (sched *Scheduler) updatePodInCache(oldObj, newObj interface{}) {
243243
return
244244
}
245245

246+
// A Pod delete event followed by an immediate Pod add event may be merged
247+
// into a Pod update event. In this case, we should invalidate the old Pod, and
248+
// then add the new Pod.
249+
if oldPod.UID != newPod.UID {
250+
sched.deletePodFromCache(oldObj)
251+
sched.addPodToCache(newObj)
252+
return
253+
}
254+
246255
// NOTE: Updates must be written to scheduler cache before invalidating
247256
// equivalence cache, because we could snapshot equivalence cache after the
248257
// invalidation and then snapshot the cache itself. If the cache is

pkg/scheduler/eventhandlers_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ package scheduler
1919
import (
2020
"reflect"
2121
"testing"
22+
"time"
2223

2324
"k8s.io/api/core/v1"
2425
"k8s.io/apimachinery/pkg/api/resource"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/kubernetes/pkg/scheduler/internal/cache"
2628
fakecache "k8s.io/kubernetes/pkg/scheduler/internal/cache/fake"
29+
"k8s.io/kubernetes/pkg/scheduler/internal/queue"
2730
)
2831

2932
func TestSkipPodUpdate(t *testing.T) {
@@ -346,3 +349,51 @@ func TestNodeConditionsChanged(t *testing.T) {
346349
})
347350
}
348351
}
352+
353+
func TestUpdatePodInCache(t *testing.T) {
354+
ttl := 10 * time.Second
355+
nodeName := "node"
356+
357+
tests := []struct {
358+
name string
359+
oldObj interface{}
360+
newObj interface{}
361+
}{
362+
{
363+
name: "pod updated with the same UID",
364+
oldObj: withPodName(podWithPort("oldUID", nodeName, 80), "pod"),
365+
newObj: withPodName(podWithPort("oldUID", nodeName, 8080), "pod"),
366+
},
367+
{
368+
name: "pod updated with different UIDs",
369+
oldObj: withPodName(podWithPort("oldUID", nodeName, 80), "pod"),
370+
newObj: withPodName(podWithPort("newUID", nodeName, 8080), "pod"),
371+
},
372+
}
373+
for _, tt := range tests {
374+
t.Run(tt.name, func(t *testing.T) {
375+
stopCh := make(chan struct{})
376+
defer close(stopCh)
377+
schedulerCache := cache.New(ttl, stopCh)
378+
schedulerQueue := queue.NewPriorityQueue(nil)
379+
sched := &Scheduler{
380+
SchedulerCache: schedulerCache,
381+
SchedulingQueue: schedulerQueue,
382+
}
383+
sched.addPodToCache(tt.oldObj)
384+
sched.updatePodInCache(tt.oldObj, tt.newObj)
385+
pod, err := sched.SchedulerCache.GetPod(tt.newObj.(*v1.Pod))
386+
if err != nil {
387+
t.Errorf("Failed to get pod from scheduler: %v", err)
388+
}
389+
if pod.UID != tt.newObj.(*v1.Pod).UID {
390+
t.Errorf("Want pod UID %v, got %v", tt.newObj.(*v1.Pod).UID, pod.UID)
391+
}
392+
})
393+
}
394+
}
395+
396+
func withPodName(pod *v1.Pod, name string) *v1.Pod {
397+
pod.Name = name
398+
return pod
399+
}

pkg/scheduler/scheduler_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ func (fp fakePodPreemptor) removeNominatedNodeName(pod *v1.Pod) error {
9292
func podWithID(id, desiredHost string) *v1.Pod {
9393
return &v1.Pod{
9494
ObjectMeta: metav1.ObjectMeta{
95-
Name: id,
96-
UID: types.UID(id),
97-
SelfLink: fmt.Sprintf("/api/v1/%s/%s", string(v1.ResourcePods), id),
95+
Name: id,
96+
UID: types.UID(id),
9897
},
9998
Spec: v1.PodSpec{
10099
NodeName: desiredHost,
@@ -110,7 +109,6 @@ func deletingPod(id string) *v1.Pod {
110109
Name: id,
111110
UID: types.UID(id),
112111
DeletionTimestamp: &deletionTimestamp,
113-
SelfLink: fmt.Sprintf("/api/v1/%s/%s", string(v1.ResourcePods), id),
114112
},
115113
Spec: v1.PodSpec{
116114
NodeName: "",

0 commit comments

Comments
 (0)