Skip to content

Commit

Permalink
fix: fix didn't delete pod when CR's replicas = 0
Browse files Browse the repository at this point in the history
Signed-off-by: Rory Z <[email protected]>
  • Loading branch information
Rory-Z committed Sep 22, 2023
1 parent 8e2a9c9 commit 051588b
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions controllers/apps/v1beta3/emqx_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ func (r *EmqxReconciler) Do(ctx context.Context, instance appsv1beta3.Emqx) (ctr
return ctrl.Result{}, nil
}
if instance.GetReplicas() == nil || *instance.GetReplicas() == 0 {
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
storageSts := &appsv1.StatefulSet{}
if err := r.Get(ctx, client.ObjectKeyFromObject(generateStatefulSetDef(instance)), storageSts); err != nil {
if k8sErrors.IsNotFound(err) {
return nil
}
return err
}
storageSts.Spec.Replicas = instance.GetReplicas()
return r.Handler.Update(storageSts, func(o client.Object) error { return nil })
}); err != nil {
return ctrl.Result{}, emperror.Wrap(err, "failed to update statefulSet")
}

if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
_ = r.Get(ctx, client.ObjectKeyFromObject(instance), instance)
instance = updateEmqxStatus(instance, []appsv1beta3.EmqxNode{})
Expand Down Expand Up @@ -885,20 +899,27 @@ func updateEmqxStatus(instance appsv1beta3.Emqx, emqxNodes []appsv1beta3.EmqxNod
}

var cond *appsv1beta3.Condition
if status.Replicas == status.ReadyReplicas {
if status.Replicas == 0 {
cond = appsv1beta3.NewCondition(
appsv1beta3.ConditionRunning,
corev1.ConditionTrue,
"ClusterReady",
"All resources are ready",
corev1.ConditionFalse,
"ClusterNotReady",
"Replicas is 0",
)
} else {
} else if status.Replicas != status.ReadyReplicas {
cond = appsv1beta3.NewCondition(
appsv1beta3.ConditionRunning,
corev1.ConditionFalse,
"ClusterNotReady",
"Some nodes are not ready",
)
} else {
cond = appsv1beta3.NewCondition(
appsv1beta3.ConditionRunning,
corev1.ConditionTrue,
"ClusterReady",
"All resources are ready",
)
}
status.SetCondition(*cond)
instance.SetStatus(status)
Expand Down

0 comments on commit 051588b

Please sign in to comment.