Skip to content

Commit 86bced0

Browse files
authored
Merge pull request #651 from srteam2020/main
fix #648: delete the statefulset with precondition set to the correct…
2 parents 990b61a + 3d48973 commit 86bced0

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

controllers/reconcile_finalizer.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ 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)
45+
if err != nil {
46+
return err
47+
}
4448
sts := &appsv1.StatefulSet{
4549
ObjectMeta: metav1.ObjectMeta{
4650
Name: rabbitmqCluster.ChildResourceName("server"),
@@ -54,7 +58,9 @@ func (r *RabbitmqClusterReconciler) prepareForDeletion(ctx context.Context, rabb
5458
// Delete StatefulSet immediately after changing pod labels to minimize risk of them respawning.
5559
// There is a window where the StatefulSet could respawn Pods without the deletion label in this order.
5660
// But we can't delete it before because the DownwardAPI doesn't update once a Pod enters Terminating.
57-
if err := r.Client.Delete(ctx, sts); client.IgnoreNotFound(err) != nil {
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.
63+
if err := r.Client.Delete(ctx, sts, &client.DeleteOptions{Preconditions: &metav1.Preconditions{UID: &uid}}); client.IgnoreNotFound(err) != nil {
5864
return fmt.Errorf("cannot delete StatefulSet: %s", err.Error())
5965
}
6066

controllers/utils.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package controllers
22

33
import (
44
"context"
5+
"fmt"
6+
57
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
68
appsv1 "k8s.io/api/apps/v1"
79
corev1 "k8s.io/api/core/v1"
810
"k8s.io/apimachinery/pkg/api/errors"
911
"k8s.io/apimachinery/pkg/api/meta"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1013
"k8s.io/apimachinery/pkg/types"
1114
"k8s.io/client-go/util/retry"
1215
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -64,6 +67,23 @@ func (r *RabbitmqClusterReconciler) statefulSet(ctx context.Context, rmq *rabbit
6467
return sts, nil
6568
}
6669

70+
// statefulSetUID only returns the UID successfully when the controller reference uid matches the rmq uid
71+
func (r *RabbitmqClusterReconciler) statefulSetUID(ctx context.Context, rmq *rabbitmqv1beta1.RabbitmqCluster) (types.UID, error) {
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 "", fmt.Errorf("failed to get controller reference for statefulSet %s", sts.GetName())
80+
}
81+
if string(rmq.GetUID()) != string(ref.UID) {
82+
return "", fmt.Errorf("statefulSet %s not owned by current RabbitmqCluster %s", sts.GetName(), rmq.GetName())
83+
}
84+
return sts.UID, nil
85+
}
86+
6787
func (r *RabbitmqClusterReconciler) configMap(ctx context.Context, rmq *rabbitmqv1beta1.RabbitmqCluster, name string) (*corev1.ConfigMap, error) {
6888
configMap := &corev1.ConfigMap{}
6989
if err := r.Get(ctx, types.NamespacedName{Namespace: rmq.Namespace, Name: name}, configMap); err != nil {

0 commit comments

Comments
 (0)