@@ -28,7 +28,6 @@ import (
28
28
appsv1 "k8s.io/api/apps/v1"
29
29
v1 "k8s.io/api/core/v1"
30
30
"k8s.io/apimachinery/pkg/api/errors"
31
-
32
31
"k8s.io/utils/clock"
33
32
"knative.dev/pkg/logging"
34
33
"knative.dev/pkg/ptr"
@@ -109,12 +108,13 @@ func (c *Controller) run(ctx context.Context) {
109
108
continue
110
109
}
111
110
112
- // don't consolidate as we recently scaled down too soon
111
+ // don't consolidate as we recently scaled up/ down too soon
113
112
stabilizationTime := c .clock .Now ().Add (- c .stabilizationWindow (ctx ))
114
113
// capture the state before we process so if something changes during consolidation we'll re-look
115
114
// immediately
116
115
clusterState := c .cluster .ClusterConsolidationState ()
117
- if c .cluster .LastNodeDeletionTime ().Before (stabilizationTime ) {
116
+ if c .cluster .LastNodeDeletionTime ().Before (stabilizationTime ) &&
117
+ c .cluster .LastNodeCreationTime ().Before (stabilizationTime ) {
118
118
result , err := c .ProcessCluster (ctx )
119
119
if err != nil {
120
120
logging .FromContext (ctx ).Errorf ("consolidating cluster, %s" , err )
@@ -480,10 +480,12 @@ func (c *Controller) nodeConsolidationOptionReplaceOrDelete(ctx context.Context,
480
480
if node .Name == n .Node .Name && n .MarkedForDeletion {
481
481
candidateNodeIsDeleting = true
482
482
}
483
- if ! n .MarkedForDeletion {
484
- stateNodes = append (stateNodes , n .DeepCopy ())
485
- } else {
486
- markedForDeletionNodes = append (markedForDeletionNodes , n .DeepCopy ())
483
+ if n .Node .Name != node .Name {
484
+ if ! n .MarkedForDeletion {
485
+ stateNodes = append (stateNodes , n .DeepCopy ())
486
+ } else {
487
+ markedForDeletionNodes = append (markedForDeletionNodes , n .DeepCopy ())
488
+ }
487
489
}
488
490
return true
489
491
})
@@ -502,7 +504,6 @@ func (c *Controller) nodeConsolidationOptionReplaceOrDelete(ctx context.Context,
502
504
pods := append (node .pods , deletingNodePods ... )
503
505
scheduler , err := c .provisioner .NewScheduler (ctx , pods , stateNodes , scheduling.SchedulerOptions {
504
506
SimulationMode : true ,
505
- ExcludeNodes : []string {node .Name },
506
507
})
507
508
508
509
if err != nil {
@@ -576,11 +577,30 @@ func (c *Controller) hasPendingPods(ctx context.Context) bool {
576
577
return false
577
578
}
578
579
580
+ func (c * Controller ) deploymentsReady (ctx context.Context ) bool {
581
+ var depList appsv1.DeploymentList
582
+ if err := c .kubeClient .List (ctx , & depList ); err != nil {
583
+ // failed to list, assume there must be one non-ready as it's harmless and just ensures we wait longer
584
+ return false
585
+ }
586
+ for _ , ds := range depList .Items {
587
+ desired := ptr .Int32Value (ds .Spec .Replicas )
588
+ if ds .Spec .Replicas == nil {
589
+ // unspecified defaults to 1
590
+ desired = 1
591
+ }
592
+ if ds .Status .ReadyReplicas < desired || ds .Status .UpdatedReplicas < desired {
593
+ return false
594
+ }
595
+ }
596
+ return true
597
+ }
598
+
579
599
func (c * Controller ) replicaSetsReady (ctx context.Context ) bool {
580
600
var rsList appsv1.ReplicaSetList
581
601
if err := c .kubeClient .List (ctx , & rsList ); err != nil {
582
602
// failed to list, assume there must be one non-ready as it's harmless and just ensures we wait longer
583
- return true
603
+ return false
584
604
}
585
605
for _ , rs := range rsList .Items {
586
606
desired := ptr .Int32Value (rs .Spec .Replicas )
@@ -599,7 +619,7 @@ func (c *Controller) replicationControllersReady(ctx context.Context) bool {
599
619
var rsList v1.ReplicationControllerList
600
620
if err := c .kubeClient .List (ctx , & rsList ); err != nil {
601
621
// failed to list, assume there must be one non-ready as it's harmless and just ensures we wait longer
602
- return true
622
+ return false
603
623
}
604
624
for _ , rs := range rsList .Items {
605
625
desired := ptr .Int32Value (rs .Spec .Replicas )
@@ -618,15 +638,15 @@ func (c *Controller) statefulSetsReady(ctx context.Context) bool {
618
638
var sslist appsv1.StatefulSetList
619
639
if err := c .kubeClient .List (ctx , & sslist ); err != nil {
620
640
// failed to list, assume there must be one non-ready as it's harmless and just ensures we wait longer
621
- return true
641
+ return false
622
642
}
623
643
for _ , rs := range sslist .Items {
624
644
desired := ptr .Int32Value (rs .Spec .Replicas )
625
645
if rs .Spec .Replicas == nil {
626
646
// unspecified defaults to 1
627
647
desired = 1
628
648
}
629
- if rs .Status .ReadyReplicas < desired {
649
+ if rs .Status .ReadyReplicas < desired || rs . Status . UpdatedReplicas < desired {
630
650
return false
631
651
}
632
652
}
@@ -635,7 +655,7 @@ func (c *Controller) statefulSetsReady(ctx context.Context) bool {
635
655
636
656
func (c * Controller ) stabilizationWindow (ctx context.Context ) time.Duration {
637
657
// no pending pods, and all replica sets/replication controllers are reporting ready so quickly consider another consolidation
638
- if ! c .hasPendingPods (ctx ) && c .replicaSetsReady (ctx ) &&
658
+ if ! c .hasPendingPods (ctx ) && c .deploymentsReady ( ctx ) && c . replicaSetsReady (ctx ) &&
639
659
c .replicationControllersReady (ctx ) && c .statefulSetsReady (ctx ) {
640
660
return 0
641
661
}
0 commit comments