Skip to content

Commit e4f878e

Browse files
committed
cleanup: use string instead of v1.Node as key of nodeToVictims
1 parent 0024c83 commit e4f878e

File tree

8 files changed

+162
-106
lines changed

8 files changed

+162
-106
lines changed

pkg/scheduler/core/extender.go

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,13 @@ type SchedulerExtender interface {
6969
// given:
7070
// 1. Pod to schedule
7171
// 2. Candidate nodes and victim pods (nodeToVictims) generated by previous scheduling process.
72-
// 3. nodeNameToInfo to restore v1.Node from node name if extender cache is enabled.
7372
// The possible changes made by extender may include:
7473
// 1. Subset of given candidate nodes after preemption phase of extender.
7574
// 2. A different set of victim pod for every given candidate node after preemption phase of extender.
7675
ProcessPreemption(
7776
pod *v1.Pod,
78-
nodeToVictims map[*v1.Node]*extenderv1.Victims,
79-
nodeInfos framework.NodeInfoLister) (map[*v1.Node]*extenderv1.Victims, error)
77+
nodeToVictims map[string]*extenderv1.Victims,
78+
nodeInfos framework.NodeInfoLister) (map[string]*extenderv1.Victims, error)
8079

8180
// SupportsPreemption returns if the scheduler extender support preemption or not.
8281
SupportsPreemption() bool
@@ -212,9 +211,9 @@ func (h *HTTPExtender) SupportsPreemption() bool {
212211
// ProcessPreemption returns filtered candidate nodes and victims after running preemption logic in extender.
213212
func (h *HTTPExtender) ProcessPreemption(
214213
pod *v1.Pod,
215-
nodeToVictims map[*v1.Node]*extenderv1.Victims,
214+
nodeNameToVictims map[string]*extenderv1.Victims,
216215
nodeInfos framework.NodeInfoLister,
217-
) (map[*v1.Node]*extenderv1.Victims, error) {
216+
) (map[string]*extenderv1.Victims, error) {
218217
var (
219218
result extenderv1.ExtenderPreemptionResult
220219
args *extenderv1.ExtenderPreemptionArgs
@@ -226,13 +225,12 @@ func (h *HTTPExtender) ProcessPreemption(
226225

227226
if h.nodeCacheCapable {
228227
// If extender has cached node info, pass NodeNameToMetaVictims in args.
229-
nodeNameToMetaVictims := convertToNodeNameToMetaVictims(nodeToVictims)
228+
nodeNameToMetaVictims := convertToNodeNameToMetaVictims(nodeNameToVictims)
230229
args = &extenderv1.ExtenderPreemptionArgs{
231230
Pod: pod,
232231
NodeNameToMetaVictims: nodeNameToMetaVictims,
233232
}
234233
} else {
235-
nodeNameToVictims := convertToNodeNameToVictims(nodeToVictims)
236234
args = &extenderv1.ExtenderPreemptionArgs{
237235
Pod: pod,
238236
NodeNameToVictims: nodeNameToVictims,
@@ -244,22 +242,22 @@ func (h *HTTPExtender) ProcessPreemption(
244242
}
245243

246244
// Extender will always return NodeNameToMetaVictims.
247-
// So let's convert it to NodeToVictims by using NodeNameToInfo.
248-
newNodeToVictims, err := h.convertToNodeToVictims(result.NodeNameToMetaVictims, nodeInfos)
245+
// So let's convert it to NodeNameToVictims by using <nodeInfos>.
246+
newNodeNameToVictims, err := h.convertToNodeNameToVictims(result.NodeNameToMetaVictims, nodeInfos)
249247
if err != nil {
250248
return nil, err
251249
}
252-
// Do not override nodeToVictims
253-
return newNodeToVictims, nil
250+
// Do not override <nodeNameToVictims>.
251+
return newNodeNameToVictims, nil
254252
}
255253

256-
// convertToNodeToVictims converts "nodeNameToMetaVictims" from object identifiers,
254+
// convertToNodeNameToVictims converts "nodeNameToMetaVictims" from object identifiers,
257255
// such as UIDs and names, to object pointers.
258-
func (h *HTTPExtender) convertToNodeToVictims(
256+
func (h *HTTPExtender) convertToNodeNameToVictims(
259257
nodeNameToMetaVictims map[string]*extenderv1.MetaVictims,
260258
nodeInfos framework.NodeInfoLister,
261-
) (map[*v1.Node]*extenderv1.Victims, error) {
262-
nodeToVictims := map[*v1.Node]*extenderv1.Victims{}
259+
) (map[string]*extenderv1.Victims, error) {
260+
nodeNameToVictims := map[string]*extenderv1.Victims{}
263261
for nodeName, metaVictims := range nodeNameToMetaVictims {
264262
nodeInfo, err := nodeInfos.Get(nodeName)
265263
if err != nil {
@@ -275,9 +273,9 @@ func (h *HTTPExtender) convertToNodeToVictims(
275273
}
276274
victims.Pods = append(victims.Pods, pod)
277275
}
278-
nodeToVictims[nodeInfo.Node()] = victims
276+
nodeNameToVictims[nodeName] = victims
279277
}
280-
return nodeToVictims, nil
278+
return nodeNameToVictims, nil
281279
}
282280

283281
// convertPodUIDToPod returns v1.Pod object for given MetaPod and node info.
@@ -298,10 +296,10 @@ func (h *HTTPExtender) convertPodUIDToPod(
298296

299297
// convertToNodeNameToMetaVictims converts from struct type to meta types.
300298
func convertToNodeNameToMetaVictims(
301-
nodeToVictims map[*v1.Node]*extenderv1.Victims,
299+
nodeNameToVictims map[string]*extenderv1.Victims,
302300
) map[string]*extenderv1.MetaVictims {
303-
nodeNameToVictims := map[string]*extenderv1.MetaVictims{}
304-
for node, victims := range nodeToVictims {
301+
nodeNameToMetaVictims := map[string]*extenderv1.MetaVictims{}
302+
for node, victims := range nodeNameToVictims {
305303
metaVictims := &extenderv1.MetaVictims{
306304
Pods: []*extenderv1.MetaPod{},
307305
}
@@ -311,20 +309,9 @@ func convertToNodeNameToMetaVictims(
311309
}
312310
metaVictims.Pods = append(metaVictims.Pods, metaPod)
313311
}
314-
nodeNameToVictims[node.GetName()] = metaVictims
315-
}
316-
return nodeNameToVictims
317-
}
318-
319-
// convertToNodeNameToVictims converts from node type to node name as key.
320-
func convertToNodeNameToVictims(
321-
nodeToVictims map[*v1.Node]*extenderv1.Victims,
322-
) map[string]*extenderv1.Victims {
323-
nodeNameToVictims := map[string]*extenderv1.Victims{}
324-
for node, victims := range nodeToVictims {
325-
nodeNameToVictims[node.GetName()] = victims
312+
nodeNameToMetaVictims[node] = metaVictims
326313
}
327-
return nodeNameToVictims
314+
return nodeNameToMetaVictims
328315
}
329316

330317
// Filter based on extender implemented predicate functions. The filtered list is

pkg/scheduler/core/extender_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,36 +159,37 @@ func (f *FakeExtender) SupportsPreemption() bool {
159159

160160
func (f *FakeExtender) ProcessPreemption(
161161
pod *v1.Pod,
162-
nodeToVictims map[*v1.Node]*extenderv1.Victims,
162+
nodeNameToVictims map[string]*extenderv1.Victims,
163163
nodeInfos framework.NodeInfoLister,
164-
) (map[*v1.Node]*extenderv1.Victims, error) {
165-
nodeToVictimsCopy := map[*v1.Node]*extenderv1.Victims{}
166-
// We don't want to change the original nodeToVictims
167-
for k, v := range nodeToVictims {
164+
) (map[string]*extenderv1.Victims, error) {
165+
nodeNameToVictimsCopy := map[string]*extenderv1.Victims{}
166+
// We don't want to change the original nodeNameToVictims
167+
for k, v := range nodeNameToVictims {
168168
// In real world implementation, extender's user should have their own way to get node object
169169
// by name if needed (e.g. query kube-apiserver etc).
170170
//
171171
// For test purpose, we just use node from parameters directly.
172-
nodeToVictimsCopy[k] = v
172+
nodeNameToVictimsCopy[k] = v
173173
}
174174

175-
for node, victims := range nodeToVictimsCopy {
175+
for nodeName, victims := range nodeNameToVictimsCopy {
176176
// Try to do preemption on extender side.
177-
extenderVictimPods, extendernPDBViolations, fits, err := f.selectVictimsOnNodeByExtender(pod, node)
177+
nodeInfo, _ := nodeInfos.Get(nodeName)
178+
extenderVictimPods, extenderPDBViolations, fits, err := f.selectVictimsOnNodeByExtender(pod, nodeInfo.Node())
178179
if err != nil {
179180
return nil, err
180181
}
181182
// If it's unfit after extender's preemption, this node is unresolvable by preemption overall,
182183
// let's remove it from potential preemption nodes.
183184
if !fits {
184-
delete(nodeToVictimsCopy, node)
185+
delete(nodeNameToVictimsCopy, nodeName)
185186
} else {
186187
// Append new victims to original victims
187-
nodeToVictimsCopy[node].Pods = append(victims.Pods, extenderVictimPods...)
188-
nodeToVictimsCopy[node].NumPDBViolations = victims.NumPDBViolations + int64(extendernPDBViolations)
188+
nodeNameToVictimsCopy[nodeName].Pods = append(victims.Pods, extenderVictimPods...)
189+
nodeNameToVictimsCopy[nodeName].NumPDBViolations = victims.NumPDBViolations + int64(extenderPDBViolations)
189190
}
190191
}
191-
return nodeToVictimsCopy, nil
192+
return nodeNameToVictimsCopy, nil
192193
}
193194

194195
// selectVictimsOnNodeByExtender checks the given nodes->pods map with predicates on extender's side.

pkg/scheduler/core/generic_scheduler.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ type ScheduleAlgorithm interface {
105105
// the pod by preempting lower priority pods if possible.
106106
// It returns the node where preemption happened, a list of preempted pods, a
107107
// list of pods whose nominated node name should be removed, and error if any.
108-
Preempt(context.Context, *profile.Profile, *framework.CycleState, *v1.Pod, error) (selectedNode *v1.Node, preemptedPods []*v1.Pod, cleanupNominatedPods []*v1.Pod, err error)
108+
Preempt(context.Context, *profile.Profile, *framework.CycleState, *v1.Pod, error) (selectedNode string, preemptedPods []*v1.Pod, cleanupNominatedPods []*v1.Pod, err error)
109109
// Extenders returns a slice of extender config. This is exposed for
110110
// testing.
111111
Extenders() []SchedulerExtender
@@ -251,74 +251,74 @@ func (g *genericScheduler) selectHost(nodeScoreList framework.NodeScoreList) (st
251251
// other pods with the same priority. The nominated pod prevents other pods from
252252
// using the nominated resources and the nominated pod could take a long time
253253
// before it is retried after many other pending pods.
254-
func (g *genericScheduler) Preempt(ctx context.Context, prof *profile.Profile, state *framework.CycleState, pod *v1.Pod, scheduleErr error) (*v1.Node, []*v1.Pod, []*v1.Pod, error) {
254+
func (g *genericScheduler) Preempt(ctx context.Context, prof *profile.Profile, state *framework.CycleState, pod *v1.Pod, scheduleErr error) (string, []*v1.Pod, []*v1.Pod, error) {
255255
// Scheduler may return various types of errors. Consider preemption only if
256256
// the error is of type FitError.
257257
fitError, ok := scheduleErr.(*FitError)
258258
if !ok || fitError == nil {
259-
return nil, nil, nil, nil
259+
return "", nil, nil, nil
260260
}
261261
if !podEligibleToPreemptOthers(pod, g.nodeInfoSnapshot.NodeInfos()) {
262262
klog.V(5).Infof("Pod %v/%v is not eligible for more preemption.", pod.Namespace, pod.Name)
263-
return nil, nil, nil, nil
263+
return "", nil, nil, nil
264264
}
265265
allNodes, err := g.nodeInfoSnapshot.NodeInfos().List()
266266
if err != nil {
267-
return nil, nil, nil, err
267+
return "", nil, nil, err
268268
}
269269
if len(allNodes) == 0 {
270-
return nil, nil, nil, ErrNoNodesAvailable
270+
return "", nil, nil, ErrNoNodesAvailable
271271
}
272272
potentialNodes := nodesWherePreemptionMightHelp(allNodes, fitError)
273273
if len(potentialNodes) == 0 {
274274
klog.V(3).Infof("Preemption will not help schedule pod %v/%v on any node.", pod.Namespace, pod.Name)
275275
// In this case, we should clean-up any existing nominated node name of the pod.
276-
return nil, nil, []*v1.Pod{pod}, nil
276+
return "", nil, []*v1.Pod{pod}, nil
277277
}
278278
var pdbs []*policy.PodDisruptionBudget
279279
if g.pdbLister != nil {
280280
pdbs, err = g.pdbLister.List(labels.Everything())
281281
if err != nil {
282-
return nil, nil, nil, err
282+
return "", nil, nil, err
283283
}
284284
}
285-
nodeToVictims, err := g.selectNodesForPreemption(ctx, prof, state, pod, potentialNodes, pdbs)
285+
nodeNameToVictims, err := g.selectNodesForPreemption(ctx, prof, state, pod, potentialNodes, pdbs)
286286
if err != nil {
287-
return nil, nil, nil, err
287+
return "", nil, nil, err
288288
}
289289

290-
// We will only check nodeToVictims with extenders that support preemption.
290+
// We will only check nodeNameToVictims with extenders that support preemption.
291291
// Extenders which do not support preemption may later prevent preemptor from being scheduled on the nominated
292292
// node. In that case, scheduler will find a different host for the preemptor in subsequent scheduling cycles.
293-
nodeToVictims, err = g.processPreemptionWithExtenders(pod, nodeToVictims)
293+
nodeNameToVictims, err = g.processPreemptionWithExtenders(pod, nodeNameToVictims)
294294
if err != nil {
295-
return nil, nil, nil, err
295+
return "", nil, nil, err
296296
}
297297

298-
candidateNode := pickOneNodeForPreemption(nodeToVictims)
299-
if candidateNode == nil {
300-
return nil, nil, nil, nil
298+
candidateNode := pickOneNodeForPreemption(nodeNameToVictims)
299+
if len(candidateNode) == 0 {
300+
return "", nil, nil, nil
301301
}
302302

303303
// Lower priority pods nominated to run on this node, may no longer fit on
304304
// this node. So, we should remove their nomination. Removing their
305305
// nomination updates these pods and moves them to the active queue. It
306306
// lets scheduler find another place for them.
307-
nominatedPods := g.getLowerPriorityNominatedPods(pod, candidateNode.Name)
308-
return candidateNode, nodeToVictims[candidateNode].Pods, nominatedPods, nil
307+
nominatedPods := g.getLowerPriorityNominatedPods(pod, candidateNode)
308+
return candidateNode, nodeNameToVictims[candidateNode].Pods, nominatedPods, nil
309309
}
310310

311311
// processPreemptionWithExtenders processes preemption with extenders
312312
func (g *genericScheduler) processPreemptionWithExtenders(
313313
pod *v1.Pod,
314-
nodeToVictims map[*v1.Node]*extenderv1.Victims,
315-
) (map[*v1.Node]*extenderv1.Victims, error) {
316-
if len(nodeToVictims) > 0 {
314+
nodeNameToVictims map[string]*extenderv1.Victims,
315+
) (map[string]*extenderv1.Victims, error) {
316+
if len(nodeNameToVictims) > 0 {
317317
for _, extender := range g.extenders {
318318
if extender.SupportsPreemption() && extender.IsInterested(pod) {
319-
newNodeToVictims, err := extender.ProcessPreemption(
319+
newNodeNameToVictims, err := extender.ProcessPreemption(
320320
pod,
321-
nodeToVictims,
321+
nodeNameToVictims,
322322
g.nodeInfoSnapshot.NodeInfos(),
323323
)
324324
if err != nil {
@@ -330,19 +330,19 @@ func (g *genericScheduler) processPreemptionWithExtenders(
330330
return nil, err
331331
}
332332

333-
// Replace nodeToVictims with new result after preemption. So the
333+
// Replace nodeNameToVictims with new result after preemption. So the
334334
// rest of extenders can continue use it as parameter.
335-
nodeToVictims = newNodeToVictims
335+
nodeNameToVictims = newNodeNameToVictims
336336

337337
// If node list becomes empty, no preemption can happen regardless of other extenders.
338-
if len(nodeToVictims) == 0 {
338+
if len(nodeNameToVictims) == 0 {
339339
break
340340
}
341341
}
342342
}
343343
}
344344

345-
return nodeToVictims, nil
345+
return nodeNameToVictims, nil
346346
}
347347

348348
// getLowerPriorityNominatedPods returns pods whose priority is smaller than the
@@ -719,12 +719,12 @@ func (g *genericScheduler) prioritizeNodes(
719719
// 6. If there are still ties, the first such node is picked (sort of randomly).
720720
// The 'minNodes1' and 'minNodes2' are being reused here to save the memory
721721
// allocation and garbage collection time.
722-
func pickOneNodeForPreemption(nodesToVictims map[*v1.Node]*extenderv1.Victims) *v1.Node {
722+
func pickOneNodeForPreemption(nodesToVictims map[string]*extenderv1.Victims) string {
723723
if len(nodesToVictims) == 0 {
724-
return nil
724+
return ""
725725
}
726726
minNumPDBViolatingPods := int64(math.MaxInt32)
727-
var minNodes1 []*v1.Node
727+
var minNodes1 []string
728728
lenNodes1 := 0
729729
for node, victims := range nodesToVictims {
730730
if len(victims.Pods) == 0 {
@@ -752,7 +752,7 @@ func pickOneNodeForPreemption(nodesToVictims map[*v1.Node]*extenderv1.Victims) *
752752
// There are more than one node with minimum number PDB violating pods. Find
753753
// the one with minimum highest priority victim.
754754
minHighestPriority := int32(math.MaxInt32)
755-
var minNodes2 = make([]*v1.Node, lenNodes1)
755+
var minNodes2 = make([]string, lenNodes1)
756756
lenNodes2 := 0
757757
for i := 0; i < lenNodes1; i++ {
758758
node := minNodes1[i]
@@ -855,8 +855,8 @@ func (g *genericScheduler) selectNodesForPreemption(
855855
pod *v1.Pod,
856856
potentialNodes []*framework.NodeInfo,
857857
pdbs []*policy.PodDisruptionBudget,
858-
) (map[*v1.Node]*extenderv1.Victims, error) {
859-
nodeToVictims := map[*v1.Node]*extenderv1.Victims{}
858+
) (map[string]*extenderv1.Victims, error) {
859+
nodeNameToVictims := map[string]*extenderv1.Victims{}
860860
var resultLock sync.Mutex
861861

862862
checkNode := func(i int) {
@@ -869,12 +869,12 @@ func (g *genericScheduler) selectNodesForPreemption(
869869
Pods: pods,
870870
NumPDBViolations: int64(numPDBViolations),
871871
}
872-
nodeToVictims[potentialNodes[i].Node()] = &victims
872+
nodeNameToVictims[potentialNodes[i].Node().Name] = &victims
873873
resultLock.Unlock()
874874
}
875875
}
876876
parallelize.Until(ctx, len(potentialNodes), checkNode)
877-
return nodeToVictims, nil
877+
return nodeNameToVictims, nil
878878
}
879879

880880
// filterPodsWithPDBViolation groups the given "pods" into two groups of "violatingPods"

0 commit comments

Comments
 (0)