Skip to content

Commit a521285

Browse files
authored
Merge pull request kubernetes#72073 from misterikkit/cleanup
Minor cleanup in scheduler/PriorityQueue
2 parents 680ee44 + d27d28a commit a521285

File tree

2 files changed

+49
-36
lines changed

2 files changed

+49
-36
lines changed

pkg/scheduler/internal/queue/scheduling_queue.go

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -276,31 +276,34 @@ func (p *PriorityQueue) run() {
276276
// already exist in the map. Adding an existing pod is not going to update the pod.
277277
func (p *PriorityQueue) addNominatedPodIfNeeded(pod *v1.Pod) {
278278
nnn := NominatedNodeName(pod)
279-
if len(nnn) > 0 {
280-
for _, np := range p.nominatedPods[nnn] {
281-
if np.UID == pod.UID {
282-
klog.V(4).Infof("Pod %v/%v already exists in the nominated map!", pod.Namespace, pod.Name)
283-
return
284-
}
279+
if len(nnn) <= 0 {
280+
return
281+
}
282+
for _, np := range p.nominatedPods[nnn] {
283+
if np.UID == pod.UID {
284+
klog.V(4).Infof("Pod %v/%v already exists in the nominated map!", pod.Namespace, pod.Name)
285+
return
285286
}
286-
p.nominatedPods[nnn] = append(p.nominatedPods[nnn], pod)
287287
}
288+
p.nominatedPods[nnn] = append(p.nominatedPods[nnn], pod)
288289
}
289290

290291
// deleteNominatedPodIfExists deletes a pod from the nominatedPods.
291292
// NOTE: this function assumes lock has been acquired in caller.
292293
func (p *PriorityQueue) deleteNominatedPodIfExists(pod *v1.Pod) {
293294
nnn := NominatedNodeName(pod)
294-
if len(nnn) > 0 {
295-
for i, np := range p.nominatedPods[nnn] {
296-
if np.UID == pod.UID {
297-
p.nominatedPods[nnn] = append(p.nominatedPods[nnn][:i], p.nominatedPods[nnn][i+1:]...)
298-
if len(p.nominatedPods[nnn]) == 0 {
299-
delete(p.nominatedPods, nnn)
300-
}
301-
break
302-
}
295+
if len(nnn) <= 0 {
296+
return
297+
}
298+
for i, np := range p.nominatedPods[nnn] {
299+
if np.UID != pod.UID {
300+
continue
301+
}
302+
p.nominatedPods[nnn] = append(p.nominatedPods[nnn][:i], p.nominatedPods[nnn][i+1:]...)
303+
if len(p.nominatedPods[nnn]) == 0 {
304+
delete(p.nominatedPods, nnn)
303305
}
306+
break
304307
}
305308
}
306309

@@ -317,23 +320,23 @@ func (p *PriorityQueue) updateNominatedPod(oldPod, newPod *v1.Pod) {
317320
func (p *PriorityQueue) Add(pod *v1.Pod) error {
318321
p.lock.Lock()
319322
defer p.lock.Unlock()
320-
err := p.activeQ.Add(pod)
321-
if err != nil {
323+
if err := p.activeQ.Add(pod); err != nil {
322324
klog.Errorf("Error adding pod %v/%v to the scheduling queue: %v", pod.Namespace, pod.Name, err)
323-
} else {
324-
if p.unschedulableQ.get(pod) != nil {
325-
klog.Errorf("Error: pod %v/%v is already in the unschedulable queue.", pod.Namespace, pod.Name)
326-
p.deleteNominatedPodIfExists(pod)
327-
p.unschedulableQ.delete(pod)
328-
}
329-
// Delete pod from backoffQ if it is backing off
330-
if err = p.podBackoffQ.Delete(pod); err == nil {
331-
klog.Errorf("Error: pod %v/%v is already in the podBackoff queue.", pod.Namespace, pod.Name)
332-
}
333-
p.addNominatedPodIfNeeded(pod)
334-
p.cond.Broadcast()
325+
return err
335326
}
336-
return err
327+
if p.unschedulableQ.get(pod) != nil {
328+
klog.Errorf("Error: pod %v/%v is already in the unschedulable queue.", pod.Namespace, pod.Name)
329+
p.deleteNominatedPodIfExists(pod)
330+
p.unschedulableQ.delete(pod)
331+
}
332+
// Delete pod from backoffQ if it is backing off
333+
if err := p.podBackoffQ.Delete(pod); err == nil {
334+
klog.Errorf("Error: pod %v/%v is already in the podBackoff queue.", pod.Namespace, pod.Name)
335+
}
336+
p.addNominatedPodIfNeeded(pod)
337+
p.cond.Broadcast()
338+
339+
return nil
337340
}
338341

339342
// AddIfNotPresent adds a pod to the active queue if it is not present in any of

pkg/scheduler/internal/queue/scheduling_queue_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,15 @@ var highPriorityPod, highPriNominatedPod, medPriorityPod, unschedulablePod = v1.
9696

9797
func TestPriorityQueue_Add(t *testing.T) {
9898
q := NewPriorityQueue(nil)
99-
q.Add(&medPriorityPod)
100-
q.Add(&unschedulablePod)
101-
q.Add(&highPriorityPod)
99+
if err := q.Add(&medPriorityPod); err != nil {
100+
t.Errorf("add failed: %v", err)
101+
}
102+
if err := q.Add(&unschedulablePod); err != nil {
103+
t.Errorf("add failed: %v", err)
104+
}
105+
if err := q.Add(&highPriorityPod); err != nil {
106+
t.Errorf("add failed: %v", err)
107+
}
102108
expectedNominatedPods := map[string][]*v1.Pod{
103109
"node1": {&medPriorityPod, &unschedulablePod},
104110
}
@@ -228,7 +234,9 @@ func TestPriorityQueue_Delete(t *testing.T) {
228234
q := NewPriorityQueue(nil)
229235
q.Update(&highPriorityPod, &highPriNominatedPod)
230236
q.Add(&unschedulablePod)
231-
q.Delete(&highPriNominatedPod)
237+
if err := q.Delete(&highPriNominatedPod); err != nil {
238+
t.Errorf("delete failed: %v", err)
239+
}
232240
if _, exists, _ := q.activeQ.Get(&unschedulablePod); !exists {
233241
t.Errorf("Expected %v to be in activeQ.", unschedulablePod.Name)
234242
}
@@ -238,7 +246,9 @@ func TestPriorityQueue_Delete(t *testing.T) {
238246
if len(q.nominatedPods) != 1 {
239247
t.Errorf("Expected nomindatePods to have only 'unschedulablePod': %v", q.nominatedPods)
240248
}
241-
q.Delete(&unschedulablePod)
249+
if err := q.Delete(&unschedulablePod); err != nil {
250+
t.Errorf("delete failed: %v", err)
251+
}
242252
if len(q.nominatedPods) != 0 {
243253
t.Errorf("Expected nomindatePods to be empty: %v", q.nominatedPods)
244254
}

0 commit comments

Comments
 (0)