@@ -31,12 +31,10 @@ import (
31
31
"fmt"
32
32
"math/rand"
33
33
"reflect"
34
- "slices"
35
34
"sync"
36
35
"time"
37
36
38
37
v1 "k8s.io/api/core/v1"
39
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
40
38
"k8s.io/apimachinery/pkg/types"
41
39
"k8s.io/apimachinery/pkg/util/sets"
42
40
"k8s.io/apimachinery/pkg/util/wait"
@@ -137,11 +135,6 @@ func NewSchedulingQueue(
137
135
return NewPriorityQueue (lessFn , informerFactory , opts ... )
138
136
}
139
137
140
- // NominatedNodeName returns nominated node name of a Pod.
141
- func NominatedNodeName (pod * v1.Pod ) string {
142
- return pod .Status .NominatedNodeName
143
- }
144
-
145
138
// PriorityQueue implements a scheduling queue.
146
139
// The head of PriorityQueue is the highest priority pending pod. This structure
147
140
// has two sub queues and a additional data structure, namely: activeQ,
@@ -1212,29 +1205,6 @@ func (p *PriorityQueue) Close() {
1212
1205
p .activeQ .broadcast ()
1213
1206
}
1214
1207
1215
- // DeleteNominatedPodIfExists deletes <pod> from nominatedPods.
1216
- func (npm * nominator ) DeleteNominatedPodIfExists (pod * v1.Pod ) {
1217
- npm .nLock .Lock ()
1218
- npm .delete (pod )
1219
- npm .nLock .Unlock ()
1220
- }
1221
-
1222
- // AddNominatedPod adds a pod to the nominated pods of the given node.
1223
- // This is called during the preemption process after a node is nominated to run
1224
- // the pod. We update the structure before sending a request to update the pod
1225
- // object to avoid races with the following scheduling cycles.
1226
- func (npm * nominator ) AddNominatedPod (logger klog.Logger , pi * framework.PodInfo , nominatingInfo * framework.NominatingInfo ) {
1227
- npm .nLock .Lock ()
1228
- npm .addNominatedPodUnlocked (logger , pi , nominatingInfo )
1229
- npm .nLock .Unlock ()
1230
- }
1231
-
1232
- func (npm * nominator ) nominatedPodsForNode (nodeName string ) []PodRef {
1233
- npm .nLock .RLock ()
1234
- defer npm .nLock .RUnlock ()
1235
- return slices .Clone (npm .nominatedPods [nodeName ])
1236
- }
1237
-
1238
1208
// NominatedPodsForNode returns a copy of pods that are nominated to run on the given node,
1239
1209
// but they are waiting for other pods to be removed from the node.
1240
1210
// CAUTION: Make sure you don't call this function while taking any queue's lock in any scenario.
@@ -1362,147 +1332,6 @@ func newUnschedulablePods(unschedulableRecorder, gatedRecorder metrics.MetricRec
1362
1332
}
1363
1333
}
1364
1334
1365
- type PodRef struct {
1366
- Name string
1367
- Namespace string
1368
- UID types.UID
1369
- }
1370
-
1371
- func PodToRef (pod * v1.Pod ) PodRef {
1372
- return PodRef {
1373
- Name : pod .Name ,
1374
- Namespace : pod .Namespace ,
1375
- UID : pod .UID ,
1376
- }
1377
- }
1378
-
1379
- func (np PodRef ) ToPod () * v1.Pod {
1380
- return & v1.Pod {
1381
- ObjectMeta : metav1.ObjectMeta {
1382
- Name : np .Name ,
1383
- Namespace : np .Namespace ,
1384
- UID : np .UID ,
1385
- },
1386
- }
1387
- }
1388
-
1389
- // nominator is a structure that stores pods nominated to run on nodes.
1390
- // It exists because nominatedNodeName of pod objects stored in the structure
1391
- // may be different than what scheduler has here. We should be able to find pods
1392
- // by their UID and update/delete them.
1393
- type nominator struct {
1394
- // nLock synchronizes all operations related to nominator.
1395
- // Caution: DO NOT take ("SchedulingQueue.lock" or "activeQueue.lock") after taking "nLock".
1396
- // You should always take "SchedulingQueue.lock" and "activeQueue.lock" first,
1397
- // otherwise the nominator could end up in deadlock.
1398
- // Correct locking order is: SchedulingQueue.lock > activeQueue.lock > nLock.
1399
- nLock sync.RWMutex
1400
-
1401
- // podLister is used to verify if the given pod is alive.
1402
- podLister listersv1.PodLister
1403
- // nominatedPods is a map keyed by a node name and the value is a list of
1404
- // pods which are nominated to run on the node. These are pods which can be in
1405
- // the activeQ or unschedulablePods.
1406
- nominatedPods map [string ][]PodRef
1407
- // nominatedPodToNode is map keyed by a Pod UID to the node name where it is
1408
- // nominated.
1409
- nominatedPodToNode map [types.UID ]string
1410
- // nominatedPodsToInfo returns PodInfos cached in the queues for nominated PodRefs.
1411
- // Note: it takes SchedulingQueue.lock inside.
1412
- // Make sure you don't call this function while taking any lock in any scenario.
1413
- nominatedPodsToInfo func ([]PodRef ) []* framework.PodInfo
1414
- }
1415
-
1416
- func (npm * nominator ) addNominatedPodUnlocked (logger klog.Logger , pi * framework.PodInfo , nominatingInfo * framework.NominatingInfo ) {
1417
- // Always delete the pod if it already exists, to ensure we never store more than
1418
- // one instance of the pod.
1419
- npm .delete (pi .Pod )
1420
-
1421
- var nodeName string
1422
- if nominatingInfo .Mode () == framework .ModeOverride {
1423
- nodeName = nominatingInfo .NominatedNodeName
1424
- } else if nominatingInfo .Mode () == framework .ModeNoop {
1425
- if pi .Pod .Status .NominatedNodeName == "" {
1426
- return
1427
- }
1428
- nodeName = pi .Pod .Status .NominatedNodeName
1429
- }
1430
-
1431
- if npm .podLister != nil {
1432
- // If the pod was removed or if it was already scheduled, don't nominate it.
1433
- updatedPod , err := npm .podLister .Pods (pi .Pod .Namespace ).Get (pi .Pod .Name )
1434
- if err != nil {
1435
- logger .V (4 ).Info ("Pod doesn't exist in podLister, aborted adding it to the nominator" , "pod" , klog .KObj (pi .Pod ))
1436
- return
1437
- }
1438
- if updatedPod .Spec .NodeName != "" {
1439
- logger .V (4 ).Info ("Pod is already scheduled to a node, aborted adding it to the nominator" , "pod" , klog .KObj (pi .Pod ), "node" , updatedPod .Spec .NodeName )
1440
- return
1441
- }
1442
- }
1443
-
1444
- npm .nominatedPodToNode [pi .Pod .UID ] = nodeName
1445
- for _ , np := range npm .nominatedPods [nodeName ] {
1446
- if np .UID == pi .Pod .UID {
1447
- logger .V (4 ).Info ("Pod already exists in the nominator" , "pod" , np .UID )
1448
- return
1449
- }
1450
- }
1451
- npm .nominatedPods [nodeName ] = append (npm .nominatedPods [nodeName ], PodToRef (pi .Pod ))
1452
- }
1453
-
1454
- func (npm * nominator ) delete (p * v1.Pod ) {
1455
- nnn , ok := npm .nominatedPodToNode [p .UID ]
1456
- if ! ok {
1457
- return
1458
- }
1459
- for i , np := range npm .nominatedPods [nnn ] {
1460
- if np .UID == p .UID {
1461
- npm .nominatedPods [nnn ] = append (npm .nominatedPods [nnn ][:i ], npm .nominatedPods [nnn ][i + 1 :]... )
1462
- if len (npm .nominatedPods [nnn ]) == 0 {
1463
- delete (npm .nominatedPods , nnn )
1464
- }
1465
- break
1466
- }
1467
- }
1468
- delete (npm .nominatedPodToNode , p .UID )
1469
- }
1470
-
1471
- // UpdateNominatedPod updates the <oldPod> with <newPod>.
1472
- func (npm * nominator ) UpdateNominatedPod (logger klog.Logger , oldPod * v1.Pod , newPodInfo * framework.PodInfo ) {
1473
- npm .nLock .Lock ()
1474
- defer npm .nLock .Unlock ()
1475
- // In some cases, an Update event with no "NominatedNode" present is received right
1476
- // after a node("NominatedNode") is reserved for this pod in memory.
1477
- // In this case, we need to keep reserving the NominatedNode when updating the pod pointer.
1478
- var nominatingInfo * framework.NominatingInfo
1479
- // We won't fall into below `if` block if the Update event represents:
1480
- // (1) NominatedNode info is added
1481
- // (2) NominatedNode info is updated
1482
- // (3) NominatedNode info is removed
1483
- if NominatedNodeName (oldPod ) == "" && NominatedNodeName (newPodInfo .Pod ) == "" {
1484
- if nnn , ok := npm .nominatedPodToNode [oldPod .UID ]; ok {
1485
- // This is the only case we should continue reserving the NominatedNode
1486
- nominatingInfo = & framework.NominatingInfo {
1487
- NominatingMode : framework .ModeOverride ,
1488
- NominatedNodeName : nnn ,
1489
- }
1490
- }
1491
- }
1492
- // We update irrespective of the nominatedNodeName changed or not, to ensure
1493
- // that pod pointer is updated.
1494
- npm .delete (oldPod )
1495
- npm .addNominatedPodUnlocked (logger , newPodInfo , nominatingInfo )
1496
- }
1497
-
1498
- func newPodNominator (podLister listersv1.PodLister ) * nominator {
1499
- return & nominator {
1500
- podLister : podLister ,
1501
- nominatedPods : make (map [string ][]PodRef ),
1502
- nominatedPodToNode : make (map [types.UID ]string ),
1503
- }
1504
- }
1505
-
1506
1335
func podInfoKeyFunc (pInfo * framework.QueuedPodInfo ) string {
1507
1336
return cache .NewObjectName (pInfo .Pod .Namespace , pInfo .Pod .Name ).String ()
1508
1337
}
0 commit comments