This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
volumehandler.go
572 lines (523 loc) · 17.9 KB
/
volumehandler.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
Copyright 2020 The Scribe authors.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package controllers
import (
"context"
"errors"
"fmt"
"time"
scribev1alpha1 "github.com/backube/scribe/api/v1alpha1"
"github.com/go-logr/logr"
snapv1 "github.com/kubernetes-csi/external-snapshotter/v2/pkg/apis/volumesnapshot/v1beta1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
const (
// Annotation used to track the name of the snapshot being created
snapshotAnnotation = "scribe.backube/snapname"
// Time format for snapshot names and labels
timeYYYYMMDDHHMMSS = "20060102150405"
)
type destinationVolumeHandler struct {
ReplicationDestinationReconciler
Ctx context.Context
Instance *scribev1alpha1.ReplicationDestination
Options *scribev1alpha1.ReplicationDestinationVolumeOptions
PVC *v1.PersistentVolumeClaim
Snapshot *snapv1.VolumeSnapshot
resticCache *v1.PersistentVolumeClaim
}
func (h *destinationVolumeHandler) useProvidedPVC(l logr.Logger) (bool, error) {
h.PVC = &v1.PersistentVolumeClaim{}
pvcName := types.NamespacedName{Name: *h.Options.DestinationPVC, Namespace: h.Instance.Namespace}
err := h.Client.Get(h.Ctx, pvcName, h.PVC)
if err != nil {
l.Error(err, "failed to get PVC with provided name", "PVC", pvcName)
return false, err
}
return true, nil
}
// EnsurePVC ensures that there is a PVC available to replicate into. The PVC
// may be either user-provided or provisioned by this reconcile function. The
// resulting PVC to use will be available in h.PVC if this function returns
// successfully.
func (h *destinationVolumeHandler) EnsurePVC(l logr.Logger) (bool, error) {
if h.Options.DestinationPVC != nil {
return h.useProvidedPVC(l)
}
pvcName := types.NamespacedName{Name: "scribe-dest-" + h.Instance.Name, Namespace: h.Instance.Namespace}
logger := l.WithValues("PVC", pvcName)
// Ensure required configuration parameters have been provided in order to create volume
if h.Options.AccessModes == nil || len(h.Options.AccessModes) == 0 {
return false, errors.New("accessModes must be provided when destinationPVC is not")
}
if h.Options.Capacity == nil {
return false, errors.New("capacity must be provided when destinationPVC is not")
}
h.PVC = &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: pvcName.Name,
Namespace: pvcName.Namespace,
},
}
// Note: we don't reconcile the immutable fields. We could do it by deleting
// and recreating the PVC.
op, err := ctrlutil.CreateOrUpdate(h.Ctx, h.Client, h.PVC, func() error {
if err := ctrl.SetControllerReference(h.Instance, h.PVC, h.Scheme); err != nil {
logger.Error(err, "unable to set controller reference")
return err
}
if h.PVC.CreationTimestamp.IsZero() { // set immutable fields
h.PVC.Spec.AccessModes = h.Options.AccessModes
h.PVC.Spec.StorageClassName = h.Options.StorageClassName
volumeMode := corev1.PersistentVolumeFilesystem
h.PVC.Spec.VolumeMode = &volumeMode
}
h.PVC.Spec.Resources.Requests = corev1.ResourceList{
corev1.ResourceStorage: *h.Options.Capacity,
}
return nil
})
if err != nil {
logger.Error(err, "reconcile failed")
return false, err
}
logger.V(1).Info("PVC reconciled", "operation", op)
return true, nil
}
func (h *destinationVolumeHandler) createSnapshot(l logr.Logger) (bool, error) {
// Track the name of the (in-progress) snapshot as a PVC annotation
snapName := types.NamespacedName{Namespace: h.Instance.Namespace}
if h.PVC.Annotations == nil {
h.PVC.Annotations = make(map[string]string)
}
if name, ok := h.PVC.Annotations[snapshotAnnotation]; ok {
snapName.Name = name
} else {
ts := time.Now().Format(timeYYYYMMDDHHMMSS)
snapName.Name = "scribe-dest-" + h.Instance.Name + "-" + ts
h.PVC.Annotations[snapshotAnnotation] = snapName.Name
if err := h.Client.Update(h.Ctx, h.PVC); err != nil {
l.Error(err, "unable to update PVC")
return false, err
}
}
logger := l.WithValues("snapshot", snapName)
h.Snapshot = &snapv1.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: snapName.Name,
Namespace: snapName.Namespace,
},
}
op, err := ctrlutil.CreateOrUpdate(h.Ctx, h.Client, h.Snapshot, func() error {
if err := ctrl.SetControllerReference(h.Instance, h.Snapshot, h.Scheme); err != nil {
logger.Error(err, "unable to set controller reference")
return err
}
if h.Snapshot.CreationTimestamp.IsZero() {
h.Snapshot.Spec = snapv1.VolumeSnapshotSpec{
Source: snapv1.VolumeSnapshotSource{
PersistentVolumeClaimName: &h.PVC.Name,
},
VolumeSnapshotClassName: h.Options.VolumeSnapshotClassName,
}
}
return nil
})
if err != nil {
logger.Error(err, "reconcile failed")
return false, err
}
logger.V(1).Info("Snapshot reconciled", "operation", op)
// We only continue reconciling if the snapshot has been bound
if h.Snapshot.Status == nil || h.Snapshot.Status.BoundVolumeSnapshotContentName == nil {
return false, nil
}
return true, nil
}
func (h *destinationVolumeHandler) cleanupOldSnapshot(l logr.Logger) (bool, error) {
// Make sure we only delete an old snapshot (it's a snapshot, but not the
// current one)
// There's no latestImage
if h.Instance.Status.LatestImage == nil {
return true, nil
}
// LatestImage is not a snapshot
if h.Instance.Status.LatestImage.Kind != "VolumeSnapshot" ||
*h.Instance.Status.LatestImage.APIGroup != snapv1.SchemeGroupVersion.Group {
return true, nil
}
// Also don't clean it up if it's the snap we're trying to preserve
if h.Snapshot != nil && h.Instance.Status.LatestImage.Name == h.Snapshot.Name {
return true, nil
}
oldSnap := &snapv1.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: h.Instance.Status.LatestImage.Name,
Namespace: h.Instance.Namespace,
},
}
err := h.Client.Delete(h.Ctx, oldSnap)
if err != nil && !kerrors.IsNotFound(err) {
l.Error(err, "unable to delete old snapshot")
return false, err
}
// Don't need to force the status update
h.Instance.Status.LatestImage = nil
l.Info("Old snapshot deleted.", "snapshotname", oldSnap)
return true, nil
}
func (h *destinationVolumeHandler) recordNewSnapshot(l logr.Logger) (bool, error) {
h.Instance.Status.LatestImage = &v1.TypedLocalObjectReference{
APIGroup: &snapv1.SchemeGroupVersion.Group,
Kind: h.Snapshot.Kind,
Name: h.Snapshot.Name,
}
err := h.Status().Update(h.Ctx, h.Instance)
if err != nil {
l.Error(err, "unable to save snapshot name")
return false, err
}
return true, nil
}
func (h *destinationVolumeHandler) removeSnapshotAnnotation(l logr.Logger) (bool, error) {
delete(h.PVC.Annotations, snapshotAnnotation)
if err := h.Client.Update(h.Ctx, h.PVC); err != nil {
l.Error(err, "unable to remove snapshot annotation from PVC")
return false, err
}
return true, nil
}
func (h *destinationVolumeHandler) recordPVC(l logr.Logger) (bool, error) {
coreAPI := ""
h.Instance.Status.LatestImage = &v1.TypedLocalObjectReference{
APIGroup: &coreAPI,
Kind: h.PVC.Kind,
Name: h.PVC.Name,
}
err := h.Status().Update(h.Ctx, h.Instance)
if err != nil {
l.Error(err, "unable to save PVC name")
return false, err
}
return true, nil
}
// PreserveImage implements the methods for preserving a PiT copy of the
// replicated data.
func (h *destinationVolumeHandler) PreserveImage(l logr.Logger) (bool, error) {
if h.Options.CopyMethod == scribev1alpha1.CopyMethodNone {
return reconcileBatch(l,
h.cleanupOldSnapshot,
h.recordPVC,
)
}
if h.Options.CopyMethod == scribev1alpha1.CopyMethodSnapshot {
return reconcileBatch(l,
h.createSnapshot,
h.cleanupOldSnapshot,
h.recordNewSnapshot,
h.removeSnapshotAnnotation,
)
}
return false, fmt.Errorf("unsupported copyMethod: %v -- must be None or Snapshot", h.Options.CopyMethod)
}
type sourceVolumeHandler struct {
ReplicationSourceReconciler
Ctx context.Context
Instance *scribev1alpha1.ReplicationSource
Options *scribev1alpha1.ReplicationSourceVolumeOptions
srcPVC *v1.PersistentVolumeClaim
srcSnap *snapv1.VolumeSnapshot
PVC *v1.PersistentVolumeClaim
resticCache *v1.PersistentVolumeClaim
}
// Cleans up the temporary PVC (and optional snapshot) after the synchronization
// iteration.
func (h *sourceVolumeHandler) CleanupPVC(l logr.Logger) (bool, error) {
if h.PVC != nil && h.PVC != h.srcPVC {
if err := h.Client.Delete(h.Ctx, h.PVC); err != nil {
l.Error(err, "unable to delete temporary PVC", "PVC", nameFor(h.PVC))
return false, err
}
}
if h.srcSnap != nil {
if err := h.Client.Delete(h.Ctx, h.srcSnap); err != nil {
l.Error(err, "unable to delete temporary snapshot", "snapshot", nameFor(h.srcSnap))
return false, err
}
}
return true, nil
}
// Ensures there is a source PVC to sync from, using whatever method is
// specified by CopyMethod.
func (h *sourceVolumeHandler) EnsurePVC(l logr.Logger) (bool, error) {
h.srcPVC = &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: h.Instance.Spec.SourcePVC,
Namespace: h.Instance.Namespace,
},
}
if err := h.Client.Get(h.Ctx, nameFor(h.srcPVC), h.srcPVC); err != nil {
l.Error(err, "unable to get source PVC", "PVC", nameFor(h.srcPVC))
return false, err
}
if h.Options.CopyMethod == scribev1alpha1.CopyMethodNone {
h.PVC = h.srcPVC
return true, nil
} else if h.Options.CopyMethod == scribev1alpha1.CopyMethodClone {
return h.ensureClone(l)
} else if h.Options.CopyMethod == scribev1alpha1.CopyMethodSnapshot {
return reconcileBatch(l,
h.snapshotSrc,
h.pvcFromSnap,
)
}
return false, fmt.Errorf("unsupported copyMethod: %v -- must be None, Clone, or Snapshot", h.Options.CopyMethod)
}
func (h *sourceVolumeHandler) pvcFromSnap(l logr.Logger) (bool, error) {
h.PVC = &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "scribe-src-" + h.Instance.Name,
Namespace: h.Instance.Namespace,
},
}
logger := l.WithValues("pvc", nameFor(h.PVC))
op, err := ctrlutil.CreateOrUpdate(h.Ctx, h.Client, h.PVC, func() error {
if err := ctrl.SetControllerReference(h.Instance, h.PVC, h.Scheme); err != nil {
logger.Error(err, "unable to set controller reference")
return err
}
if h.PVC.CreationTimestamp.IsZero() {
if h.Options.Capacity != nil {
h.PVC.Spec.Resources.Requests = corev1.ResourceList{
corev1.ResourceStorage: *h.Options.Capacity,
}
} else {
h.PVC.Spec.Resources.Requests = corev1.ResourceList{
corev1.ResourceStorage: *h.srcPVC.Spec.Resources.Requests.Storage(),
}
}
if h.Options.StorageClassName != nil {
h.PVC.Spec.StorageClassName = h.Options.StorageClassName
} else {
h.PVC.Spec.StorageClassName = h.srcPVC.Spec.StorageClassName
}
if h.Options.AccessModes != nil {
h.PVC.Spec.AccessModes = h.Options.AccessModes
} else {
h.PVC.Spec.AccessModes = h.srcPVC.Spec.AccessModes
}
h.PVC.Spec.DataSource = &v1.TypedLocalObjectReference{
APIGroup: &snapv1.SchemeGroupVersion.Group,
Kind: "VolumeSnapshot",
Name: h.srcSnap.Name,
}
}
return nil
})
if err != nil {
logger.Error(err, "reconcile failed")
return false, err
}
logger.V(1).Info("pvc from snap reconciled", "operation", op)
return true, nil
}
//nolint:funlen,dupl
func (h *sourceVolumeHandler) pvcForCache(l logr.Logger) (bool, error) {
h.resticCache = &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "scribe-rcs-" + h.Instance.Name,
Namespace: h.Instance.Namespace,
},
}
logger := l.WithValues("pvc", nameFor(h.resticCache))
capacity := resource.MustParse("1Gi")
if h.Instance.Spec.Restic != nil && h.Instance.Spec.Restic.CacheCapacity != nil {
capacity = *h.Instance.Spec.Restic.CacheCapacity
}
scName := h.srcPVC.Spec.StorageClassName
if h.Options.StorageClassName != nil {
scName = h.Options.StorageClassName
}
if h.Instance.Spec.Restic != nil && h.Instance.Spec.Restic.CacheStorageClassName != nil {
scName = h.Instance.Spec.Restic.CacheStorageClassName
}
aModes := h.srcPVC.Spec.AccessModes
if h.Options.AccessModes != nil {
aModes = h.Options.AccessModes
}
if h.Instance.Spec.Restic != nil && h.Instance.Spec.Restic.CacheAccessModes != nil {
aModes = h.Instance.Spec.Restic.CacheAccessModes
}
op, err := ctrlutil.CreateOrUpdate(h.Ctx, h.Client, h.resticCache, func() error {
if err := ctrl.SetControllerReference(h.Instance, h.resticCache, h.Scheme); err != nil {
logger.Error(err, "Unable to set controller refrenece")
return err
}
if h.resticCache.CreationTimestamp.IsZero() {
h.resticCache.Spec.Resources.Requests = corev1.ResourceList{
corev1.ResourceStorage: capacity,
}
h.resticCache.Spec.StorageClassName = scName
h.resticCache.Spec.AccessModes = aModes
}
return nil
})
if err != nil {
logger.Error(err, "reconcile failed")
return false, err
}
logger.V(1).Info("restic cache pvc reconciled", "operation", op)
return true, nil
}
//nolint:funlen,dupl
func (h *destinationVolumeHandler) pvcForCache(l logr.Logger) (bool, error) {
h.resticCache = &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "scribe-rcd-" + h.Instance.Name,
Namespace: h.Instance.Namespace,
},
}
logger := l.WithValues("pvc", nameFor(h.resticCache))
capacity := resource.MustParse("1Gi")
if h.Instance.Spec.Restic != nil && h.Instance.Spec.Restic.CacheCapacity != nil {
capacity = *h.Instance.Spec.Restic.CacheCapacity
}
scName := h.PVC.Spec.StorageClassName
if h.Options.StorageClassName != nil {
scName = h.Options.StorageClassName
}
if h.Instance.Spec.Restic != nil && h.Instance.Spec.Restic.CacheStorageClassName != nil {
scName = h.Instance.Spec.Restic.CacheStorageClassName
}
aModes := h.PVC.Spec.AccessModes
if h.Options.AccessModes != nil {
aModes = h.Options.AccessModes
}
if h.Instance.Spec.Restic != nil && h.Instance.Spec.Restic.CacheAccessModes != nil {
aModes = h.Instance.Spec.Restic.CacheAccessModes
}
op, err := ctrlutil.CreateOrUpdate(h.Ctx, h.Client, h.resticCache, func() error {
if err := ctrl.SetControllerReference(h.Instance, h.resticCache, h.Scheme); err != nil {
logger.Error(err, "Unable to set controller refrenece")
return err
}
if h.resticCache.CreationTimestamp.IsZero() {
h.resticCache.Spec.Resources.Requests = corev1.ResourceList{
corev1.ResourceStorage: capacity,
}
h.resticCache.Spec.StorageClassName = scName
h.resticCache.Spec.AccessModes = aModes
}
return nil
})
if err != nil {
logger.Error(err, "reconcile failed")
return false, err
}
logger.V(1).Info("restic cache pvc reconciled", "operation", op)
return true, nil
}
func (h *sourceVolumeHandler) snapshotSrc(l logr.Logger) (bool, error) {
h.srcSnap = &snapv1.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: "scribe-src-" + h.Instance.Name,
Namespace: h.Instance.Namespace,
},
}
logger := l.WithValues("SourceSnap", nameFor(h.srcSnap))
op, err := ctrlutil.CreateOrUpdate(h.Ctx, h.Client, h.srcSnap, func() error {
if err := ctrl.SetControllerReference(h.Instance, h.srcSnap, h.Scheme); err != nil {
logger.Error(err, "unable to set controller reference")
return err
}
if h.srcSnap.CreationTimestamp.IsZero() {
h.srcSnap.Spec.Source.PersistentVolumeClaimName = &h.srcPVC.Name
h.srcSnap.Spec.VolumeSnapshotClassName = h.Options.VolumeSnapshotClassName
}
return nil
})
if err != nil {
logger.Error(err, "reconcile failed")
return false, err
}
if h.srcSnap.Status == nil || h.srcSnap.Status.BoundVolumeSnapshotContentName == nil {
logger.V(1).Info("waiting for snapshot to be bound")
return false, nil
}
logger.V(1).Info("temporary snapshot reconciled", "operation", op)
return true, nil
}
func (h *sourceVolumeHandler) ensureClone(l logr.Logger) (bool, error) {
pvcName := types.NamespacedName{
Name: "scribe-src-" + h.Instance.Name,
Namespace: h.Instance.Namespace,
}
logger := l.WithValues("pvc", pvcName)
h.PVC = &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: pvcName.Name,
Namespace: pvcName.Namespace,
},
}
op, err := ctrlutil.CreateOrUpdate(h.Ctx, h.Client, h.PVC, func() error {
if err := ctrl.SetControllerReference(h.Instance, h.PVC, h.Scheme); err != nil {
logger.Error(err, "unable to set controller reference")
return err
}
if h.PVC.CreationTimestamp.IsZero() {
if h.Options.Capacity != nil {
h.PVC.Spec.Resources.Requests = corev1.ResourceList{
corev1.ResourceStorage: *h.Options.Capacity,
}
} else {
h.PVC.Spec.Resources.Requests = corev1.ResourceList{
corev1.ResourceStorage: *h.srcPVC.Spec.Resources.Requests.Storage(),
}
}
if h.Options.StorageClassName != nil {
h.PVC.Spec.StorageClassName = h.Options.StorageClassName
} else {
h.PVC.Spec.StorageClassName = h.srcPVC.Spec.StorageClassName
}
if h.Options.AccessModes != nil {
h.PVC.Spec.AccessModes = h.Options.AccessModes
} else {
h.PVC.Spec.AccessModes = h.srcPVC.Spec.AccessModes
}
h.PVC.Spec.DataSource = &v1.TypedLocalObjectReference{
APIGroup: nil,
Kind: "PersistentVolumeClaim",
Name: h.srcPVC.Name,
}
}
return nil
})
if err != nil {
logger.Error(err, "reconcile failed")
return false, err
}
if !h.PVC.DeletionTimestamp.IsZero() {
logger.V(1).Info("PVC is being deleted-- need to wait")
return false, nil
}
logger.V(1).Info("clone reconciled", "operation", op)
return true, nil
}