Skip to content

Commit 846dc5c

Browse files
handle corner cases for StorageID generation
There can be cases where Ceph FSID is not yet initialized or missing from the server response. In such cases, it is better to error out early than to proceed with incorrect state. We also reconcile all blue secrets irrespective of the changes to ensure that we do not miss out on any updates. Additionally, we watch CephCluster for FSID changes. Signed-off-by: Umanga Chapagain <uchapaga@redhat.com>
1 parent 7f49789 commit 846dc5c

File tree

3 files changed

+111
-6
lines changed

3 files changed

+111
-6
lines changed

addons/blue_secret_controller.go

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ package addons
22

33
import (
44
"context"
5+
"fmt"
56
"log/slog"
67

8+
rookv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
9+
corev1 "k8s.io/api/core/v1"
10+
"k8s.io/apimachinery/pkg/api/errors"
11+
"k8s.io/apimachinery/pkg/runtime"
12+
"k8s.io/apimachinery/pkg/types"
713
ctrl "sigs.k8s.io/controller-runtime"
814
"sigs.k8s.io/controller-runtime/pkg/builder"
915
"sigs.k8s.io/controller-runtime/pkg/client"
1016
"sigs.k8s.io/controller-runtime/pkg/event"
1117
"sigs.k8s.io/controller-runtime/pkg/handler"
1218
"sigs.k8s.io/controller-runtime/pkg/predicate"
13-
14-
corev1 "k8s.io/api/core/v1"
15-
"k8s.io/apimachinery/pkg/api/errors"
16-
"k8s.io/apimachinery/pkg/runtime"
19+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1720
)
1821

1922
// BlueSecretReconciler reconciles a MirrorPeer object
@@ -46,12 +49,50 @@ func (r *BlueSecretReconciler) SetupWithManager(mgr ctrl.Manager) error {
4649
},
4750
}
4851

52+
cephClusterPredicate := predicate.Funcs{
53+
CreateFunc: func(e event.CreateEvent) bool {
54+
c, ok := e.Object.(*rookv1.CephCluster)
55+
if !ok {
56+
return false
57+
}
58+
return c.Status.CephStatus.FSID != ""
59+
},
60+
DeleteFunc: func(e event.DeleteEvent) bool {
61+
return false
62+
},
63+
UpdateFunc: func(e event.UpdateEvent) bool {
64+
oc, ok := e.ObjectOld.(*rookv1.CephCluster)
65+
if !ok {
66+
return false
67+
}
68+
nc, ok := e.ObjectNew.(*rookv1.CephCluster)
69+
if !ok {
70+
return false
71+
}
72+
return oc.Status.CephStatus.FSID != nc.Status.CephStatus.FSID
73+
},
74+
GenericFunc: func(_ event.GenericEvent) bool {
75+
return false
76+
},
77+
}
78+
4979
r.Logger.Info("Setting up controller with manager")
5080

5181
return ctrl.NewControllerManagedBy(mgr).
5282
Named("bluesecret_controller").
5383
Watches(&corev1.Secret{}, &handler.EnqueueRequestForObject{},
54-
builder.WithPredicates(predicate.LabelChangedPredicate{}, blueSecretPredicate)).
84+
builder.WithPredicates(blueSecretPredicate)).
85+
Watches(&rookv1.CephCluster{}, handler.EnqueueRequestsFromMapFunc(
86+
func(_ context.Context, obj client.Object) []reconcile.Request {
87+
return []reconcile.Request{
88+
{
89+
NamespacedName: types.NamespacedName{
90+
Name: fmt.Sprintf("%s-%s", RookDefaultBlueSecretMatchConvergedString, obj.GetName()),
91+
Namespace: obj.GetNamespace(),
92+
},
93+
},
94+
}
95+
}), builder.WithPredicates(cephClusterPredicate)).
5596
Complete(r)
5697
}
5798

controllers/utils/hash_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,64 @@ func TestCreateUniqueReplicationId(t *testing.T) {
110110
})
111111
}
112112
}
113+
114+
func TestCalculateMD5Hash(t *testing.T) {
115+
type args struct {
116+
value any
117+
}
118+
tests := []struct {
119+
name string
120+
args args
121+
want string
122+
}{
123+
{
124+
name: "RBD with empty CephFSID",
125+
args: args{
126+
value: [2]string{"", ""},
127+
},
128+
want: "2432510f5993984b053e7d74ce53d94c",
129+
},
130+
{
131+
name: "CephFS with empty CephFSID",
132+
args: args{
133+
value: [2]string{"", "csi"},
134+
},
135+
want: "793b48b9b17bdad5cd141e6daadfdd4e",
136+
},
137+
{
138+
name: "C1/RBD",
139+
args: args{
140+
value: [2]string{"a0eaa723-bf87-48db-ad11-17e276edda61", ""},
141+
},
142+
want: "bb2361459436fd47121419c658d4c79b",
143+
},
144+
{
145+
name: "C1/CephFS",
146+
args: args{
147+
value: [2]string{"a0eaa723-bf87-48db-ad11-17e276edda61", "csi"},
148+
},
149+
want: "41711cb35464e85f4446ace583adbaa1",
150+
},
151+
{
152+
name: "C2/RBD",
153+
args: args{
154+
value: [2]string{"30dc85db-f35f-4c03-bb43-c8592487d8b9", ""},
155+
},
156+
want: "9e4fbb3d9e52875d6393409a51927243",
157+
},
158+
{
159+
name: "C2/CephFS",
160+
args: args{
161+
value: [2]string{"30dc85db-f35f-4c03-bb43-c8592487d8b9", "csi"},
162+
},
163+
want: "4b7e331b508946ca7165f14804c37ffc",
164+
},
165+
}
166+
for _, tt := range tests {
167+
t.Run(tt.name, func(t *testing.T) {
168+
if got := CalculateMD5Hash(tt.args.value); got != tt.want {
169+
t.Errorf("CalculateMD5Hash() = %v, want %v", got, tt.want)
170+
}
171+
})
172+
}
173+
}

controllers/utils/storageclass.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,11 @@ func CalculateStorageId(ctx context.Context, c client.Client, sc *storagev1.Stor
121121
}
122122

123123
cephClusterFSID := cephCluster.Status.CephStatus.FSID
124-
var storageId string
124+
if cephClusterFSID == "" {
125+
return "", fmt.Errorf("failed to calculate StorageID. Ceph FSID is empty")
126+
}
125127

128+
var storageId string
126129
switch sc.Provisioner {
127130
case fmt.Sprintf(RBDProvisionerTemplate, storageClusterNamespacedName.Namespace):
128131
storageId = CalculateMD5Hash([2]string{cephClusterFSID, DefaultRadosNamespace})

0 commit comments

Comments
 (0)