Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix didn't delete pod when CR's replicas = 0 #952

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading