From d3d15f2c9c1d6f84b707eb646ee8b29c2320e94f Mon Sep 17 00:00:00 2001 From: hexin <574252631@qq.com> Date: Thu, 26 Sep 2024 14:33:04 +0800 Subject: [PATCH] make candidates order stable (#285) --- pkg/controllers/operationjob/operationjob_controller_test.go | 3 ++- pkg/controllers/operationjob/operationjob_manager.go | 4 +++- pkg/controllers/operationjob/opscore/candidate.go | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/controllers/operationjob/operationjob_controller_test.go b/pkg/controllers/operationjob/operationjob_controller_test.go index 23735227..7b906503 100644 --- a/pkg/controllers/operationjob/operationjob_controller_test.go +++ b/pkg/controllers/operationjob/operationjob_controller_test.go @@ -236,7 +236,8 @@ var _ = Describe("operationjob controller", func() { Expect(c.Get(ctx, types.NamespacedName{Namespace: oj.Namespace, Name: oj.Name}, oj)).Should(BeNil()) replacingPod := 0 for i := range podList.Items { - if _, exist := podList.Items[i].Labels[appsv1alpha1.PodReplacePairOriginName]; exist { + if name, exist := podList.Items[i].Labels[appsv1alpha1.PodReplacePairOriginName]; exist { + Expect(name).Should(BeEquivalentTo(oj.Spec.Targets[partition-1].Name)) replacingPod++ } } diff --git a/pkg/controllers/operationjob/operationjob_manager.go b/pkg/controllers/operationjob/operationjob_manager.go index 4b34e0ba..86a12f5d 100644 --- a/pkg/controllers/operationjob/operationjob_manager.go +++ b/pkg/controllers/operationjob/operationjob_manager.go @@ -56,10 +56,12 @@ func (r *ReconcileOperationJob) getActionHandler(operationJob *appsv1alpha1.Oper func (r *ReconcileOperationJob) listTargets(ctx context.Context, operationJob *appsv1alpha1.OperationJob) ([]*OpsCandidate, error) { var candidates []*OpsCandidate podOpsStatusMap := ojutils.MapOpsStatusByPod(operationJob) - for _, target := range operationJob.Spec.Targets { + for idx := range operationJob.Spec.Targets { + target := operationJob.Spec.Targets[idx] var candidate OpsCandidate var pod corev1.Pod + candidate.Idx = idx // fulfil target pod candidate.PodName = target.Name err := r.Client.Get(ctx, types.NamespacedName{Namespace: operationJob.Namespace, Name: target.Name}, &pod) diff --git a/pkg/controllers/operationjob/opscore/candidate.go b/pkg/controllers/operationjob/opscore/candidate.go index ce26f3ba..8e8a6882 100644 --- a/pkg/controllers/operationjob/opscore/candidate.go +++ b/pkg/controllers/operationjob/opscore/candidate.go @@ -25,6 +25,7 @@ import ( ) type OpsCandidate struct { + Idx int *corev1.Pod PodName string Containers []string @@ -62,7 +63,7 @@ func (o activeCandidateToStart) Less(i, j int) bool { if lNotStarted != rNotStarted { return rNotStarted } - return true + return l.Idx < r.Idx } func IsCandidateOpsPending(candidate *OpsCandidate) bool {