From d27d28a44e6a20b94dfbd6981b25e4a31f51285f Mon Sep 17 00:00:00 2001 From: Jonathan Basseri Date: Fri, 14 Dec 2018 16:33:12 -0800 Subject: [PATCH] Flatten nominated pod logic in PriorityQueue. This replaces deeply nested ifs & fors with early returns & continues. --- .../internal/queue/scheduling_queue.go | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/pkg/scheduler/internal/queue/scheduling_queue.go b/pkg/scheduler/internal/queue/scheduling_queue.go index f5e0a6fd26..cb185fbf0b 100644 --- a/pkg/scheduler/internal/queue/scheduling_queue.go +++ b/pkg/scheduler/internal/queue/scheduling_queue.go @@ -276,31 +276,34 @@ func (p *PriorityQueue) run() { // already exist in the map. Adding an existing pod is not going to update the pod. func (p *PriorityQueue) addNominatedPodIfNeeded(pod *v1.Pod) { nnn := NominatedNodeName(pod) - if len(nnn) > 0 { - for _, np := range p.nominatedPods[nnn] { - if np.UID == pod.UID { - klog.V(4).Infof("Pod %v/%v already exists in the nominated map!", pod.Namespace, pod.Name) - return - } - } - p.nominatedPods[nnn] = append(p.nominatedPods[nnn], pod) + if len(nnn) <= 0 { + return } + for _, np := range p.nominatedPods[nnn] { + if np.UID == pod.UID { + klog.V(4).Infof("Pod %v/%v already exists in the nominated map!", pod.Namespace, pod.Name) + return + } + } + p.nominatedPods[nnn] = append(p.nominatedPods[nnn], pod) } // deleteNominatedPodIfExists deletes a pod from the nominatedPods. // NOTE: this function assumes lock has been acquired in caller. func (p *PriorityQueue) deleteNominatedPodIfExists(pod *v1.Pod) { nnn := NominatedNodeName(pod) - if len(nnn) > 0 { - for i, np := range p.nominatedPods[nnn] { - if np.UID == pod.UID { - p.nominatedPods[nnn] = append(p.nominatedPods[nnn][:i], p.nominatedPods[nnn][i+1:]...) - if len(p.nominatedPods[nnn]) == 0 { - delete(p.nominatedPods, nnn) - } - break - } + if len(nnn) <= 0 { + return + } + for i, np := range p.nominatedPods[nnn] { + if np.UID != pod.UID { + continue } + p.nominatedPods[nnn] = append(p.nominatedPods[nnn][:i], p.nominatedPods[nnn][i+1:]...) + if len(p.nominatedPods[nnn]) == 0 { + delete(p.nominatedPods, nnn) + } + break } }