@@ -196,6 +196,9 @@ type NodeInfo struct {
196
196
// The subset of pods with affinity.
197
197
PodsWithAffinity []* PodInfo
198
198
199
+ // The subset of pods with required anti-affinity.
200
+ PodsWithRequiredAntiAffinity []* PodInfo
201
+
199
202
// Ports allocated on the node.
200
203
UsedPorts HostPortInfo
201
204
@@ -457,6 +460,9 @@ func (n *NodeInfo) Clone() *NodeInfo {
457
460
if len (n .PodsWithAffinity ) > 0 {
458
461
clone .PodsWithAffinity = append ([]* PodInfo (nil ), n .PodsWithAffinity ... )
459
462
}
463
+ if len (n .PodsWithRequiredAntiAffinity ) > 0 {
464
+ clone .PodsWithRequiredAntiAffinity = append ([]* PodInfo (nil ), n .PodsWithRequiredAntiAffinity ... )
465
+ }
460
466
return clone
461
467
}
462
468
@@ -486,44 +492,67 @@ func (n *NodeInfo) AddPod(pod *v1.Pod) {
486
492
n .NonZeroRequested .MilliCPU += non0CPU
487
493
n .NonZeroRequested .Memory += non0Mem
488
494
n .Pods = append (n .Pods , podInfo )
489
- affinity := pod .Spec .Affinity
490
- if affinity != nil && (affinity .PodAffinity != nil || affinity .PodAntiAffinity != nil ) {
495
+ if podWithAffinity (pod ) {
491
496
n .PodsWithAffinity = append (n .PodsWithAffinity , podInfo )
492
497
}
498
+ if podWithRequiredAntiAffinity (pod ) {
499
+ n .PodsWithRequiredAntiAffinity = append (n .PodsWithRequiredAntiAffinity , podInfo )
500
+ }
493
501
494
502
// Consume ports when pods added.
495
503
n .updateUsedPorts (podInfo .Pod , true )
496
504
497
505
n .Generation = nextGeneration ()
498
506
}
499
507
500
- // RemovePod subtracts pod information from this NodeInfo.
501
- func (n * NodeInfo ) RemovePod (pod * v1.Pod ) error {
502
- k1 , err := GetPodKey (pod )
503
- if err != nil {
504
- return err
505
- }
508
+ func podWithAffinity (p * v1.Pod ) bool {
509
+ affinity := p .Spec .Affinity
510
+ return affinity != nil && (affinity .PodAffinity != nil || affinity .PodAntiAffinity != nil )
511
+ }
512
+
513
+ func podWithRequiredAntiAffinity (p * v1.Pod ) bool {
514
+ affinity := p .Spec .Affinity
515
+ return affinity != nil && affinity .PodAntiAffinity != nil &&
516
+ len (affinity .PodAntiAffinity .RequiredDuringSchedulingIgnoredDuringExecution ) != 0
517
+ }
506
518
507
- for i := range n .PodsWithAffinity {
508
- k2 , err := GetPodKey (n .PodsWithAffinity [i ].Pod )
519
+ func removeFromSlice (s []* PodInfo , k string ) []* PodInfo {
520
+ for i := range s {
521
+ k2 , err := GetPodKey (s [i ].Pod )
509
522
if err != nil {
510
523
klog .Errorf ("Cannot get pod key, err: %v" , err )
511
524
continue
512
525
}
513
- if k1 == k2 {
526
+ if k == k2 {
514
527
// delete the element
515
- n . PodsWithAffinity [i ] = n . PodsWithAffinity [len (n . PodsWithAffinity )- 1 ]
516
- n . PodsWithAffinity = n . PodsWithAffinity [:len (n . PodsWithAffinity )- 1 ]
528
+ s [i ] = s [len (s )- 1 ]
529
+ s = s [:len (s )- 1 ]
517
530
break
518
531
}
519
532
}
533
+ return s
534
+ }
535
+
536
+ // RemovePod subtracts pod information from this NodeInfo.
537
+ func (n * NodeInfo ) RemovePod (pod * v1.Pod ) error {
538
+ k , err := GetPodKey (pod )
539
+ if err != nil {
540
+ return err
541
+ }
542
+ if podWithAffinity (pod ) {
543
+ n .PodsWithAffinity = removeFromSlice (n .PodsWithAffinity , k )
544
+ }
545
+ if podWithRequiredAntiAffinity (pod ) {
546
+ n .PodsWithRequiredAntiAffinity = removeFromSlice (n .PodsWithRequiredAntiAffinity , k )
547
+ }
548
+
520
549
for i := range n .Pods {
521
550
k2 , err := GetPodKey (n .Pods [i ].Pod )
522
551
if err != nil {
523
552
klog .Errorf ("Cannot get pod key, err: %v" , err )
524
553
continue
525
554
}
526
- if k1 == k2 {
555
+ if k == k2 {
527
556
// delete the element
528
557
n .Pods [i ] = n .Pods [len (n .Pods )- 1 ]
529
558
n .Pods = n .Pods [:len (n .Pods )- 1 ]
@@ -558,6 +587,9 @@ func (n *NodeInfo) resetSlicesIfEmpty() {
558
587
if len (n .PodsWithAffinity ) == 0 {
559
588
n .PodsWithAffinity = nil
560
589
}
590
+ if len (n .PodsWithRequiredAntiAffinity ) == 0 {
591
+ n .PodsWithRequiredAntiAffinity = nil
592
+ }
561
593
if len (n .Pods ) == 0 {
562
594
n .Pods = nil
563
595
}
0 commit comments