fixup: fix comments and use a channel to pass err

This commit is contained in:
Wei Huang 2019-07-19 00:20:37 -07:00
parent dce6686c9a
commit 249752cc1f
No known key found for this signature in database
GPG Key ID: BE5E9752F8B6E005
2 changed files with 13 additions and 23 deletions

View File

@ -202,8 +202,8 @@ func getTPMapMatchingSpreadConstraints(pod *v1.Pod, nodeInfoMap map[string]*sche
allNodeNames = append(allNodeNames, name) allNodeNames = append(allNodeNames, name)
} }
errCh := schedutil.NewErrorChannel()
var lock sync.Mutex var lock sync.Mutex
var firstError error
topologyPairsPodSpreadMap := &topologyPairsPodSpreadMap{ topologyPairsPodSpreadMap := &topologyPairsPodSpreadMap{
// topologyKeyToMinPodsMap will be initilized with proper size later. // topologyKeyToMinPodsMap will be initilized with proper size later.
@ -215,13 +215,6 @@ func getTPMapMatchingSpreadConstraints(pod *v1.Pod, nodeInfoMap map[string]*sche
topologyPairsPodSpreadMap.appendMaps(toAppend) topologyPairsPodSpreadMap.appendMaps(toAppend)
lock.Unlock() lock.Unlock()
} }
catchError := func(err error) {
lock.Lock()
if firstError == nil {
firstError = err
}
lock.Unlock()
}
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
@ -229,12 +222,11 @@ func getTPMapMatchingSpreadConstraints(pod *v1.Pod, nodeInfoMap map[string]*sche
nodeInfo := nodeInfoMap[allNodeNames[i]] nodeInfo := nodeInfoMap[allNodeNames[i]]
node := nodeInfo.Node() node := nodeInfo.Node()
if node == nil { if node == nil {
catchError(fmt.Errorf("node %q not found", allNodeNames[i])) klog.Errorf("node %q not found", allNodeNames[i])
cancel()
return return
} }
// Be design if NodeAffinity or NodeSelector is defined, spreading is // In accordance to design, if NodeAffinity or NodeSelector is defined,
// applied to nodes that pass those filters. // spreading is applied to nodes that pass those filters.
if !podMatchesNodeSelectorAndAffinityTerms(pod, node) { if !podMatchesNodeSelectorAndAffinityTerms(pod, node) {
return return
} }
@ -250,8 +242,7 @@ func getTPMapMatchingSpreadConstraints(pod *v1.Pod, nodeInfoMap map[string]*sche
for _, existingPod := range nodeInfo.Pods() { for _, existingPod := range nodeInfo.Pods() {
ok, err := podMatchesAllSpreadConstraints(existingPod, pod.Namespace, constraints) ok, err := podMatchesAllSpreadConstraints(existingPod, pod.Namespace, constraints)
if err != nil { if err != nil {
catchError(err) errCh.SendErrorWithCancel(err, cancel)
cancel()
return return
} }
if ok { if ok {
@ -280,8 +271,8 @@ func getTPMapMatchingSpreadConstraints(pod *v1.Pod, nodeInfoMap map[string]*sche
} }
workqueue.ParallelizeUntil(ctx, 16, len(allNodeNames), processNode) workqueue.ParallelizeUntil(ctx, 16, len(allNodeNames), processNode)
if firstError != nil { if err := errCh.ReceiveError(); err != nil {
return nil, firstError return nil, err
} }
// calculate min match for each topology pair // calculate min match for each topology pair
@ -395,19 +386,18 @@ func (m *topologyPairsMaps) clone() *topologyPairsMaps {
return copy return copy
} }
func (podSpreadMap *topologyPairsPodSpreadMap) clone() *topologyPairsPodSpreadMap { func (m *topologyPairsPodSpreadMap) clone() *topologyPairsPodSpreadMap {
// podSpreadMap could be nil when EvenPodsSpread feature is disabled // m could be nil when EvenPodsSpread feature is disabled
if podSpreadMap == nil { if m == nil {
return nil return nil
} }
copy := &topologyPairsPodSpreadMap{ copy := &topologyPairsPodSpreadMap{
topologyKeyToMinPodsMap: make(map[string]int32), topologyKeyToMinPodsMap: make(map[string]int32),
topologyPairsMaps: newTopologyPairsMaps(), topologyPairsMaps: m.topologyPairsMaps.clone(),
} }
for key, minMatched := range podSpreadMap.topologyKeyToMinPodsMap { for key, minMatched := range m.topologyKeyToMinPodsMap {
copy.topologyKeyToMinPodsMap[key] = minMatched copy.topologyKeyToMinPodsMap[key] = minMatched
} }
copy.topologyPairsMaps.appendMaps(podSpreadMap.topologyPairsMaps)
return copy return copy
} }

View File

@ -214,7 +214,7 @@ func (p *PodWrapper) Label(k, v string) *PodWrapper {
return p return p
} }
// NodeWrapper wraps a LabelSelector inside. // NodeWrapper wraps a Node inside.
type NodeWrapper struct{ v1.Node } type NodeWrapper struct{ v1.Node }
// MakeNode creates a Node wrapper. // MakeNode creates a Node wrapper.