Skip to content

Commit a84de03

Browse files
authored
K8SPSMDB-980: fix cluster pause/deletion (#1323)
1 parent f7d237e commit a84de03

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

pkg/psmdb/getters.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package psmdb
22

33
import (
44
"context"
5+
"sort"
56

67
appsv1 "k8s.io/api/apps/v1"
78
corev1 "k8s.io/api/core/v1"
@@ -55,9 +56,9 @@ func GetRSPods(ctx context.Context, k8sclient client.Client, cr *api.PerconaServ
5556
for _, sts := range stsList.Items {
5657
lbls := RSLabels(cr, rsName)
5758
lbls["app.kubernetes.io/component"] = sts.Labels["app.kubernetes.io/component"]
58-
compPods := corev1.PodList{}
59+
pods := corev1.PodList{}
5960
err := k8sclient.List(ctx,
60-
&compPods,
61+
&pods,
6162
&client.ListOptions{
6263
Namespace: cr.Namespace,
6364
LabelSelector: labels.SelectorFromSet(lbls),
@@ -67,7 +68,17 @@ func GetRSPods(ctx context.Context, k8sclient client.Client, cr *api.PerconaServ
6768
return rsPods, errors.Wrap(err, "failed to list pods")
6869
}
6970

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...)
7182
}
7283

7384
return rsPods, nil

0 commit comments

Comments
 (0)