/
cephrbdmirrors.go
345 lines (323 loc) · 12.6 KB
/
cephrbdmirrors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package storagecluster
import (
"context"
"fmt"
"os"
"time"
ocsv1 "github.com/red-hat-storage/ocs-operator/api/v4/v1"
"github.com/red-hat-storage/ocs-operator/v4/controllers/defaults"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
type ocsCephRbdMirrors struct{}
var (
rbdMirrorDebugLoggingEnabled = false
)
func (r *StorageClusterReconciler) fetchCephRbdMirrorInstance(cephRbdMirror *cephv1.CephRBDMirror) (cephv1.CephRBDMirror, error) {
existing := cephv1.CephRBDMirror{}
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: cephRbdMirror.Name, Namespace: cephRbdMirror.Namespace}, &existing)
return existing, err
}
func (r *StorageClusterReconciler) deleteCephRbdMirrorInstance(cephRbdMirrors []*cephv1.CephRBDMirror) error {
for _, cephRbdMirror := range cephRbdMirrors {
existing, err := r.fetchCephRbdMirrorInstance(cephRbdMirror)
if err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get CephRbdMirror %v: %v", cephRbdMirror.Name, err)
}
continue
}
if cephRbdMirror.GetDeletionTimestamp().IsZero() {
r.Log.Info("Deleting CephRbdMirror.", "CephRbdMirror", klog.KRef(cephRbdMirror.Namespace, cephRbdMirror.Name))
err := r.Client.Delete(context.TODO(), &existing)
if err != nil {
r.Log.Error(err, "Failed to delete CephRbdMirror.", "CephRbdMirror", klog.KRef(existing.Namespace, existing.Name))
return fmt.Errorf("failed to delete CephRbdMirror %v: %v", existing.Name, err)
}
r.Log.Info("CephRbdMirror is deleted.", "CephRbdMirror", klog.KRef(cephRbdMirror.Namespace, cephRbdMirror.Name))
}
}
return nil
}
// newCephRbdMirrorInstances returns the cephRbdMirror instances that should be created
// on first run.
func (r *StorageClusterReconciler) newCephRbdMirrorInstances(initData *ocsv1.StorageCluster) ([]*cephv1.CephRBDMirror, error) {
ret := []*cephv1.CephRBDMirror{
{
ObjectMeta: metav1.ObjectMeta{
Name: generateNameForCephRbdMirror(initData),
Namespace: initData.Namespace,
},
Spec: cephv1.RBDMirroringSpec{
Count: initData.Spec.ManagedResources.CephRBDMirror.DaemonCount,
Resources: defaults.GetDaemonResources("rbd-mirror", initData.Spec.Resources),
Placement: getPlacement(initData, "rbd-mirror"),
},
},
}
for _, obj := range ret {
err := controllerutil.SetControllerReference(initData, obj, r.Scheme)
if err != nil {
r.Log.Error(err, "Unable to set controller reference for CephRBDMirror.", "CephRBDMirror", klog.KRef(obj.Namespace, obj.Name))
return nil, err
}
}
return ret, nil
}
// ensureCreated ensures that cephRbdMirror resources exist in the desired state.
func (obj *ocsCephRbdMirrors) ensureCreated(r *StorageClusterReconciler, instance *ocsv1.StorageCluster) (reconcile.Result, error) {
reconcileStrategy := ReconcileStrategy(instance.Spec.ManagedResources.CephRBDMirror.ReconcileStrategy)
if reconcileStrategy == ReconcileStrategyIgnore {
return reconcile.Result{}, nil
}
cephRbdMirrors, err := r.newCephRbdMirrorInstances(instance)
if err != nil {
return reconcile.Result{}, err
}
if !instance.Spec.Mirroring.Enabled {
return reconcile.Result{}, r.deleteCephRbdMirrorInstance(cephRbdMirrors)
}
for _, cephRbdMirror := range cephRbdMirrors {
existing, err := r.fetchCephRbdMirrorInstance(cephRbdMirror)
if err != nil {
if errors.IsNotFound(err) {
r.Log.Info("Creating CephRbdMirror.", "CephRbdMirror", klog.KRef(cephRbdMirror.Namespace, cephRbdMirror.Name))
err = r.Client.Create(context.TODO(), cephRbdMirror)
if err != nil {
r.Log.Error(err, "Failed to create CephRbdMirror.", "CephRbdMirror", klog.KRef(cephRbdMirror.Namespace, cephRbdMirror.Name))
return reconcile.Result{}, err
}
continue
}
r.Log.Error(err, "Failed to get CephRbdMirror.", "CephRbdMirror", klog.KRef(cephRbdMirror.Namespace, cephRbdMirror.Name))
return reconcile.Result{}, err
}
if existing.DeletionTimestamp != nil {
r.Log.Info("Unable to restore CephRbdMirror, It is marked for deletion.", "CephRbdMirror", klog.KRef(existing.Namespace, existing.Name))
return reconcile.Result{}, fmt.Errorf("failed to restore initialization object %s, It is marked for deletion", existing.Name)
}
r.Log.Info("Restoring original CephRbdMirror.", "CephRbdMirror", klog.KRef(cephRbdMirror.Namespace, cephRbdMirror.Name))
existing.ObjectMeta.OwnerReferences = cephRbdMirror.ObjectMeta.OwnerReferences
cephRbdMirror.ObjectMeta = existing.ObjectMeta
err = r.Client.Update(context.TODO(), cephRbdMirror)
if err != nil {
r.Log.Error(err, "Failed to update CephRbdMirror.", "CephRbdMirror", klog.KRef(cephRbdMirror.Namespace, cephRbdMirror.Name))
return reconcile.Result{}, err
}
}
return reconcile.Result{}, nil
}
// ensureDeleted deletes the CephRbdMirrors owned by the StorageCluster
func (obj *ocsCephRbdMirrors) ensureDeleted(r *StorageClusterReconciler, sc *ocsv1.StorageCluster) (reconcile.Result, error) {
cephRbdMirrors, err := r.newCephRbdMirrorInstances(sc)
if err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{}, r.deleteCephRbdMirrorInstance(cephRbdMirrors)
}
func (r *StorageClusterReconciler) ensureRbdMirrorDebugLogging(sc *ocsv1.StorageCluster) (reconcile.Result, error) {
enableRbdMirrorDebugLoggingJobName := "enable-rbd-mirror-debug-logging"
disableRbdMirrorDebugLoggingJobName := "disable-rbd-mirror-debug-logging"
enableRbdMirrorDebugLoggingCommands :=
`echo "starting configuration of mirror logging"
ceph config set client.rbd-mirror.a debug_ms 1
ceph config set client.rbd-mirror.a debug_rbd 15
ceph config set client.rbd-mirror.a debug_rbd_mirror 30
ceph config set client.rbd-mirror.a log_file /var/log/ceph/\$cluster-\$name.log
ceph config set client.rbd-mirror-peer debug_ms 1
ceph config set client.rbd-mirror-peer debug_rbd 15
ceph config set client.rbd-mirror-peer debug_rbd_mirror 30
ceph config set client.rbd-mirror-peer log_file /var/log/ceph/\$cluster-\$name.log
ceph config set mgr mgr/rbd_support/log_level debug
echo "completed"`
disableRbdMirrorDebugLoggingCommands :=
`echo "Removing configuration of mirror logging"
ceph config rm client.rbd-mirror.a debug_ms
ceph config rm client.rbd-mirror.a debug_rbd
ceph config rm client.rbd-mirror.a debug_rbd_mirror
ceph config rm client.rbd-mirror.a log_file
ceph config rm client.rbd-mirror-peer debug_ms
ceph config rm client.rbd-mirror-peer debug_rbd
ceph config rm client.rbd-mirror-peer debug_rbd_mirror
ceph config rm client.rbd-mirror-peer log_file
ceph config rm mgr mgr/rbd_support/log_level
echo "completed"`
// Mirroring is enabled but the debug logging is disabled
if sc.Spec.Mirroring.Enabled && !rbdMirrorDebugLoggingEnabled {
err := r.Client.Create(r.ctx, getRbdMirrorDebugLoggingJob(sc, enableRbdMirrorDebugLoggingJobName, enableRbdMirrorDebugLoggingCommands))
if err != nil && !errors.IsAlreadyExists(err) {
return reconcile.Result{}, err
}
// Check if the job has succeeded
job := &batchv1.Job{}
err = r.Client.Get(r.ctx, types.NamespacedName{Name: enableRbdMirrorDebugLoggingJobName, Namespace: sc.Namespace}, job)
if err != nil {
return reconcile.Result{}, err
}
if job.Status.Succeeded == 0 {
// Job has not succeeded yet, so requeue a reconcile request after 10 sec to check again
r.Log.Info("Waiting for enable-rbd-mirror-debug-logging job to complete")
return reconcile.Result{RequeueAfter: 10 * time.Second}, nil
}
r.Log.Info("Successfully enabled rbd mirror debug logging")
rbdMirrorDebugLoggingEnabled = true
}
// Mirroring is disabled but the debug logging is enabled
if !sc.Spec.Mirroring.Enabled && rbdMirrorDebugLoggingEnabled {
err := r.Client.Create(r.ctx, getRbdMirrorDebugLoggingJob(sc, disableRbdMirrorDebugLoggingJobName, disableRbdMirrorDebugLoggingCommands))
if err != nil && !errors.IsAlreadyExists(err) {
return reconcile.Result{}, err
}
// Check if the job has succeeded
job := &batchv1.Job{}
err = r.Client.Get(r.ctx, types.NamespacedName{Name: disableRbdMirrorDebugLoggingJobName, Namespace: sc.Namespace}, job)
if err != nil {
return reconcile.Result{}, err
}
if job.Status.Succeeded == 0 {
// Job has not succeeded yet, so requeue a reconcile request after 10 sec to check again
r.Log.Info("Waiting for disable-rbd-mirror-debug-logging job to complete")
return reconcile.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil
}
// Delete both the enable & disable jobs
err = r.Client.Delete(r.ctx, &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: enableRbdMirrorDebugLoggingJobName,
Namespace: sc.Namespace,
}})
if err != nil && !errors.IsNotFound(err) {
return reconcile.Result{}, err
}
err = r.Client.Delete(r.ctx, &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: disableRbdMirrorDebugLoggingJobName,
Namespace: sc.Namespace,
}})
if err != nil && !errors.IsNotFound(err) {
return reconcile.Result{}, err
}
r.Log.Info("Successfully disabled rbd mirror debug logging")
rbdMirrorDebugLoggingEnabled = false
}
return reconcile.Result{}, nil
}
func getRbdMirrorDebugLoggingJob(sc *ocsv1.StorageCluster, jobName string, configCommands string) *batchv1.Job {
rookImage := os.Getenv("ROOK_CEPH_IMAGE")
debugJob := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: jobName,
Namespace: sc.Namespace,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: sc.APIVersion,
Kind: sc.Kind,
Name: sc.Name,
UID: sc.UID,
},
},
},
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{
{
Name: "config-init",
Image: rookImage,
Command: []string{"/usr/local/bin/toolbox.sh"},
Args: []string{"--skip-watch"},
ImagePullPolicy: corev1.PullIfNotPresent,
Env: []corev1.EnvVar{
{
Name: "ROOK_CEPH_USERNAME",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "rook-ceph-mon"},
Key: "ceph-username",
},
},
},
{
Name: "ROOK_CEPH_SECRET",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "rook-ceph-mon"},
Key: "ceph-secret",
},
},
},
},
VolumeMounts: []corev1.VolumeMount{
{Name: "ceph-config", MountPath: "/etc/ceph"},
{Name: "mon-endpoint-volume", MountPath: "/etc/rook"},
},
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: ptr.To(false),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
},
},
},
Containers: []corev1.Container{
{
Name: "script",
Image: rookImage,
VolumeMounts: []corev1.VolumeMount{
{Name: "ceph-config", MountPath: "/etc/ceph", ReadOnly: true},
},
Command: []string{
"bash",
"-c",
configCommands,
},
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: ptr.To(false),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
},
},
},
Volumes: []corev1.Volume{
{Name: "ceph-config", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}},
{Name: "mon-endpoint-volume", VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{Name: "rook-ceph-mon-endpoints"},
Items: []corev1.KeyToPath{{Key: "data", Path: "mon-endpoints"}},
},
},
},
},
RestartPolicy: corev1.RestartPolicyNever,
SecurityContext: &corev1.PodSecurityContext{
RunAsNonRoot: ptr.To(true),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
},
Tolerations: []corev1.Toleration{
{
Key: defaults.NodeTolerationKey,
Operator: corev1.TolerationOpEqual,
Value: "true",
Effect: corev1.TaintEffectNoSchedule,
},
},
},
},
},
}
return debugJob
}