Skip to content

Commit 9aff91f

Browse files
committed
fix #648: add more comments in prepareForDeletion and better error msg in statefulSetUID
1 parent a621a6a commit 9aff91f

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

controllers/reconcile_finalizer.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (r *RabbitmqClusterReconciler) removeFinalizer(ctx context.Context, rabbitm
4141
func (r *RabbitmqClusterReconciler) prepareForDeletion(ctx context.Context, rabbitmqCluster *rabbitmqv1beta1.RabbitmqCluster) error {
4242
if controllerutil.ContainsFinalizer(rabbitmqCluster, deletionFinalizer) {
4343
if err := clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
44-
uid, err := r.statefulSetUID(ctx, rabbitmqCluster);
44+
uid, err := r.statefulSetUID(ctx, rabbitmqCluster)
4545
if err != nil {
4646
return err
4747
}
@@ -58,6 +58,8 @@ func (r *RabbitmqClusterReconciler) prepareForDeletion(ctx context.Context, rabb
5858
// Delete StatefulSet immediately after changing pod labels to minimize risk of them respawning.
5959
// There is a window where the StatefulSet could respawn Pods without the deletion label in this order.
6060
// But we can't delete it before because the DownwardAPI doesn't update once a Pod enters Terminating.
61+
// Addressing #648: if both rabbitmqCluster and the statefulSet returned by r.Get() are stale (and match each other),
62+
// setting the stale statefulSet's uid in the precondition can avoid mis-deleting any currently running statefulSet sharing the same name.
6163
if err := r.Client.Delete(ctx, sts, &client.DeleteOptions{Preconditions: &metav1.Preconditions{UID: &uid}}); client.IgnoreNotFound(err) != nil {
6264
return fmt.Errorf("cannot delete StatefulSet: %s", err.Error())
6365
}

controllers/utils.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
88
appsv1 "k8s.io/api/apps/v1"
99
corev1 "k8s.io/api/core/v1"
10-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1110
"k8s.io/apimachinery/pkg/api/errors"
1211
"k8s.io/apimachinery/pkg/api/meta"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1313
"k8s.io/apimachinery/pkg/types"
1414
"k8s.io/client-go/util/retry"
1515
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -67,16 +67,21 @@ func (r *RabbitmqClusterReconciler) statefulSet(ctx context.Context, rmq *rabbit
6767
return sts, nil
6868
}
6969

70+
// statefulSetUID only returns the UID successfully when the controller reference uid matches the rmq uid
7071
func (r *RabbitmqClusterReconciler) statefulSetUID(ctx context.Context, rmq *rabbitmqv1beta1.RabbitmqCluster) (types.UID, error) {
71-
uid := types.UID("")
72-
if sts, err := r.statefulSet(ctx, rmq); err == nil {
73-
if ref := metav1.GetControllerOf(sts); ref != nil {
74-
if string(rmq.GetUID()) == string(ref.UID) {
75-
return sts.UID, nil
76-
}
77-
}
72+
var err error
73+
var sts *appsv1.StatefulSet
74+
var ref *metav1.OwnerReference
75+
if sts, err = r.statefulSet(ctx, rmq); err != nil {
76+
return "", fmt.Errorf("failed to get statefulSet: %s", err.Error())
77+
}
78+
if ref = metav1.GetControllerOf(sts); ref == nil {
79+
return "", errors.New("failed to get controller reference for statefulSet")
80+
}
81+
if string(rmq.GetUID()) != string(ref.UID) {
82+
return "", errors.New("statefulSet not owner by current RabbitmqCluster")
7883
}
79-
return uid, fmt.Errorf("failed to get the uid of the statefulset owned by the current rabbitmqCluster")
84+
return sts.UID, nil
8085
}
8186

8287
func (r *RabbitmqClusterReconciler) configMap(ctx context.Context, rmq *rabbitmqv1beta1.RabbitmqCluster, name string) (*corev1.ConfigMap, error) {

0 commit comments

Comments
 (0)