From fa055b6ce078ab9b7a39c648fe1c79c91cf3ea29 Mon Sep 17 00:00:00 2001 From: Rory Z <16801068+Rory-Z@users.noreply.github.com> Date: Sun, 7 Jan 2024 19:39:14 +0800 Subject: [PATCH] fix: check EMQX condition before revert sts/rs Signed-off-by: Rory Z <16801068+Rory-Z@users.noreply.github.com> --- controllers/apps/v2beta1/add_emqx_core.go | 16 +++++++++------- controllers/apps/v2beta1/add_emqx_repl.go | 16 +++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/controllers/apps/v2beta1/add_emqx_core.go b/controllers/apps/v2beta1/add_emqx_core.go index 640397dad..8aeeef302 100644 --- a/controllers/apps/v2beta1/add_emqx_core.go +++ b/controllers/apps/v2beta1/add_emqx_core.go @@ -50,14 +50,16 @@ func (a *addCore) reconcile(ctx context.Context, instance *appsv2beta1.EMQX, _ i _ = ctrl.SetControllerReference(instance, preSts, a.Scheme) if err := a.Handler.Create(preSts); err != nil { if k8sErrors.IsAlreadyExists(emperror.Cause(err)) { - // Sometimes the updated statefulSet will not be ready, because the EMQX node can not be started. - // And then we will rollback EMQX CR spec, the EMQX operator controller will create a new statefulSet. - // But the new statefulSet will be the same as the previous one, so we didn't need to create it, just change the EMQX status. - if preStsHash == instance.Status.CoreNodesStatus.CurrentRevision { - _ = a.updateEMQXStatus(ctx, instance, "RevertStatefulSet", "Revert to current statefulSet", preStsHash) - return subResult{} + cond := instance.Status.GetLastTrueCondition() + if cond != nil && cond.Type != appsv2beta1.Available && cond.Type != appsv2beta1.Ready { + // Sometimes the updated statefulSet will not be ready, because the EMQX node can not be started. + // And then we will rollback EMQX CR spec, the EMQX operator controller will create a new statefulSet. + // But the new statefulSet will be the same as the previous one, so we didn't need to create it, just change the EMQX status. + if preStsHash == instance.Status.CoreNodesStatus.CurrentRevision { + _ = a.updateEMQXStatus(ctx, instance, "RevertStatefulSet", "Revert to current statefulSet", preStsHash) + return subResult{} + } } - if instance.Status.CoreNodesStatus.CollisionCount == nil { instance.Status.CoreNodesStatus.CollisionCount = pointer.Int32(0) } diff --git a/controllers/apps/v2beta1/add_emqx_repl.go b/controllers/apps/v2beta1/add_emqx_repl.go index b2640e071..ef3248809 100644 --- a/controllers/apps/v2beta1/add_emqx_repl.go +++ b/controllers/apps/v2beta1/add_emqx_repl.go @@ -57,14 +57,16 @@ func (a *addRepl) reconcile(ctx context.Context, instance *appsv2beta1.EMQX, _ i _ = ctrl.SetControllerReference(instance, preRs, a.Scheme) if err := a.Handler.Create(preRs); err != nil { if k8sErrors.IsAlreadyExists(emperror.Cause(err)) { - // Sometimes the updated replicaSet will not be ready, because the EMQX node can not be started. - // And then we will rollback EMQX CR spec, the EMQX operator controller will create a new replicaSet. - // But the new replicaSet will be the same as the previous one, so we didn't need to create it, just change the EMQX status. - if preRsHash == instance.Status.ReplicantNodesStatus.CurrentRevision { - _ = a.updateEMQXStatus(ctx, instance, "RevertReplicaSet", "Revert to current replicaSet", preRsHash) - return subResult{} + cond := instance.Status.GetLastTrueCondition() + if cond != nil && cond.Type != appsv2beta1.Available && cond.Type != appsv2beta1.Ready { + // Sometimes the updated replicaSet will not be ready, because the EMQX node can not be started. + // And then we will rollback EMQX CR spec, the EMQX operator controller will create a new replicaSet. + // But the new replicaSet will be the same as the previous one, so we didn't need to create it, just change the EMQX status. + if preRsHash == instance.Status.ReplicantNodesStatus.CurrentRevision { + _ = a.updateEMQXStatus(ctx, instance, "RevertReplicaSet", "Revert to current replicaSet", preRsHash) + return subResult{} + } } - if instance.Status.ReplicantNodesStatus.CollisionCount == nil { instance.Status.ReplicantNodesStatus.CollisionCount = pointer.Int32(0) }