@@ -2,6 +2,7 @@ package psmdb
2
2
3
3
import (
4
4
"context"
5
+ "sort"
5
6
6
7
appsv1 "k8s.io/api/apps/v1"
7
8
corev1 "k8s.io/api/core/v1"
@@ -55,9 +56,9 @@ func GetRSPods(ctx context.Context, k8sclient client.Client, cr *api.PerconaServ
55
56
for _ , sts := range stsList .Items {
56
57
lbls := RSLabels (cr , rsName )
57
58
lbls ["app.kubernetes.io/component" ] = sts .Labels ["app.kubernetes.io/component" ]
58
- compPods := corev1.PodList {}
59
+ pods := corev1.PodList {}
59
60
err := k8sclient .List (ctx ,
60
- & compPods ,
61
+ & pods ,
61
62
& client.ListOptions {
62
63
Namespace : cr .Namespace ,
63
64
LabelSelector : labels .SelectorFromSet (lbls ),
@@ -67,7 +68,17 @@ func GetRSPods(ctx context.Context, k8sclient client.Client, cr *api.PerconaServ
67
68
return rsPods , errors .Wrap (err , "failed to list pods" )
68
69
}
69
70
70
- rsPods .Items = append (rsPods .Items , compPods .Items ... )
71
+ // `k8sclient.List` returns unsorted list of pods
72
+ // We should sort pods to truncate pods that are going to be deleted during resize
73
+ // More info: https://github.com/percona/percona-server-mongodb-operator/pull/1323#issue-1904904799
74
+ sort .Slice (pods .Items , func (i , j int ) bool {
75
+ return pods .Items [i ].Name < pods .Items [j ].Name
76
+ })
77
+ if len (pods .Items ) >= int (* sts .Spec .Replicas ) {
78
+ pods .Items = pods .Items [:* sts .Spec .Replicas ]
79
+ }
80
+
81
+ rsPods .Items = append (rsPods .Items , pods .Items ... )
71
82
}
72
83
73
84
return rsPods , nil
0 commit comments