Skip to content

Commit aac00c1

Browse files
committed
add orphanedPods parameter to getRSPods
and improve code flow in syncReplicaSet
1 parent d70229f commit aac00c1

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

pkg/controller/replicaset/replica_set.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6768
const (
@@ -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.
682683
podsForRS := []*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 {
684691
podObjs, err := rsc.podIndexer.ByIndex(controller.PodControllerUIDIndex, key)
685692
if err != nil {
686693
return 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)
733740
if err != nil {
734741
return err
735742
}
@@ -749,6 +756,7 @@ func (rsc *ReplicaSetController) syncReplicaSet(ctx context.Context, key string)
749756
}
750757

751758
var manageReplicasErr error
759+
var nextSyncInSeconds *int
752760
if rsNeedsSync && rs.DeletionTimestamp == nil {
753761
manageReplicasErr = 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
763771
return 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 &&
767778
updatedRS.Status.ReadyReplicas == *(updatedRS.Spec.Replicas) &&
768779
updatedRS.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

774788
func (rsc *ReplicaSetController) claimPods(ctx context.Context, rs *apps.ReplicaSet, selector labels.Selector, filteredPods []*v1.Pod) ([]*v1.Pod, error) {

0 commit comments

Comments
 (0)