@@ -62,6 +62,7 @@ import (
6262"k8s.io/kubernetes/pkg/controller"
6363"k8s.io/kubernetes/pkg/controller/replicaset/metrics"
6464"k8s.io/kubernetes/pkg/features"
65+ "k8s.io/utils/ptr"
6566)
6667
6768const (
@@ -674,13 +675,19 @@ func (rsc *ReplicaSetController) manageReplicas(ctx context.Context, activePods
674675}
675676
676677// getRSPods returns the Pods that a given RS should manage.
677- func (rsc * ReplicaSetController ) getRSPods (rs * apps.ReplicaSet ) ([]* v1.Pod , error ) {
678+ func (rsc * ReplicaSetController ) getRSPods (rs * apps.ReplicaSet , orphanedPods bool ) ([]* v1.Pod , error ) {
678679// Iterate over two keys:
679680// The UID of the RS, which identifies Pods that are controlled by the RS.
680681// The OrphanPodIndexKey, which helps identify orphaned Pods that are not currently managed by any controller,
681682// but may be adopted later on if they have matching labels with the ReplicaSet.
682683podsForRS := []* v1.Pod {}
683- for _ , key := range []string {string (rs .UID ), controller .OrphanPodIndexKey } {
684+
685+ uidKeys := []string {string (rs .UID )}
686+ if orphanedPods {
687+ uidKeys = append (uidKeys , controller .OrphanPodIndexKey )
688+ }
689+
690+ for _ , key := range uidKeys {
684691podObjs , err := rsc .podIndexer .ByIndex (controller .PodControllerUIDIndex , key )
685692if err != nil {
686693return nil , err
@@ -729,7 +736,7 @@ func (rsc *ReplicaSetController) syncReplicaSet(ctx context.Context, key string)
729736}
730737
731738// List all pods indexed to RS UID and Orphan pods
732- allRSPods , err := rsc .getRSPods (rs )
739+ allRSPods , err := rsc .getRSPods (rs , true )
733740if err != nil {
734741return err
735742}
@@ -749,6 +756,7 @@ func (rsc *ReplicaSetController) syncReplicaSet(ctx context.Context, key string)
749756}
750757
751758var manageReplicasErr error
759+ var nextSyncInSeconds * int
752760if rsNeedsSync && rs .DeletionTimestamp == nil {
753761manageReplicasErr = rsc .manageReplicas (ctx , activePods , rs )
754762}
@@ -762,13 +770,19 @@ func (rsc *ReplicaSetController) syncReplicaSet(ctx context.Context, key string)
762770// Returning an error causes a requeue without forcing a hotloop
763771return err
764772}
773+ if manageReplicasErr != nil {
774+ return manageReplicasErr
775+ }
765776// Resync the ReplicaSet after MinReadySeconds as a last line of defense to guard against clock-skew.
766- if manageReplicasErr == nil && updatedRS .Spec .MinReadySeconds > 0 &&
777+ if updatedRS .Spec .MinReadySeconds > 0 &&
767778updatedRS .Status .ReadyReplicas == * (updatedRS .Spec .Replicas ) &&
768779updatedRS .Status .AvailableReplicas != * (updatedRS .Spec .Replicas ) {
769- rsc .queue .AddAfter (key , time .Duration (updatedRS .Spec .MinReadySeconds )* time .Second )
780+ nextSyncInSeconds = ptr .To (int (updatedRS .Spec .MinReadySeconds ))
781+ }
782+ if nextSyncInSeconds != nil {
783+ rsc .queue .AddAfter (key , time .Duration (* nextSyncInSeconds )* time .Second )
770784}
771- return manageReplicasErr
785+ return nil
772786}
773787
774788func (rsc * ReplicaSetController ) claimPods (ctx context.Context , rs * apps.ReplicaSet , selector labels.Selector , filteredPods []* v1.Pod ) ([]* v1.Pod , error ) {
0 commit comments