forked from libopenstorage/stork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshotter_csi.go
1261 lines (1111 loc) · 41.3 KB
/
snapshotter_csi.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package snapshotter
import (
"context"
"encoding/json"
"fmt"
"io"
"path/filepath"
"sort"
"strings"
"time"
kSnapshotv1beta1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1beta1"
kSnapshotClient "github.com/kubernetes-csi/external-snapshotter/client/v4/clientset/versioned"
storkapi "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1"
"github.com/libopenstorage/stork/pkg/crypto"
"github.com/libopenstorage/stork/pkg/k8sutils"
"github.com/libopenstorage/stork/pkg/objectstore"
"github.com/portworx/kdmp/pkg/drivers/utils"
"github.com/portworx/sched-ops/k8s/batch"
"github.com/portworx/sched-ops/k8s/core"
"github.com/portworx/sched-ops/k8s/storage"
"github.com/portworx/sched-ops/task"
"github.com/sirupsen/logrus"
"gocloud.dev/blob"
"gocloud.dev/gcerrors"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
storagev1 "k8s.io/api/storage/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
k8shelper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
)
const (
csiProviderName = "csi"
// snapshotClassNamePrefix is the prefix for snapshot classes per CSI driver
snapshotClassNamePrefix = "stork-csi-snapshot-class-"
annPVBindCompleted = "pv.kubernetes.io/bind-completed"
annPVBoundByController = "pv.kubernetes.io/bound-by-controller"
skipResourceAnnotation = "stork.libopenstorage.org/skip-resource"
// snapshotTimeout represents the duration to wait before timing out on snapshot completion
snapshotTimeout = time.Minute * 5
// restoreTimeout is the duration to wait before timing out the restore
restoreTimeout = time.Minute * 5
// localCSIRetention is the max number of csi snapshots those can be kept locally
localCSIRetention = 1
// snapDeleteAnnotation needs to be set if volume snapshot is scheduled for deletion
snapDeleteAnnotation = "snapshotScheduledForDeletion"
// snapRestoreAnnotation needs to be set if volume snapshot is scheduled for restore
snapRestoreAnnotation = "snapshotScheduledForRestore"
// pvcNameLenLimitForJob is the max length of PVC name that the bound job
// will incorporate in their names
pvcNameLenLimitForJob = 48
// shortRetryTimeout gets used for retry timeout
shortRetryTimeout = 30 * time.Second
// shortRetryTimeout gets used for retry timeout interval
shortRetryTimeoutInterval = 2 * time.Second
)
type csiBackupObject struct {
VolumeSnapshots map[string]*kSnapshotv1beta1.VolumeSnapshot `json:"volumeSnapshots"`
VolumeSnapshotContents map[string]*kSnapshotv1beta1.VolumeSnapshotContent `json:"volumeSnapshotContents"`
VolumeSnapshotClasses map[string]*kSnapshotv1beta1.VolumeSnapshotClass `json:"volumeSnapshotClasses"`
}
// NewCSIDriver returns the csi implementation of Driver object
func NewCSIDriver() (Driver, error) {
cs := &csiDriver{}
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
snapClient, err := kSnapshotClient.NewForConfig(config)
if err != nil {
return nil, err
}
cs.snapshotClient = snapClient
cs.snapshotClassCreatedForDriver = make(map[string]bool)
return cs, nil
}
// csiDriver is the csi implementation of the snapshotter.Driver interface
type csiDriver struct {
snapshotClient *kSnapshotClient.Clientset
snapshotClassCreatedForDriver map[string]bool
}
func (c *csiDriver) CreateSnapshot(opts ...Option) (string, string, string, error) {
o := Options{}
for _, opt := range opts {
if opt != nil {
if err := opt(&o); err != nil {
return "", "", "", err
}
}
}
// get snapshotclass name based on pv provisioner
pvc, err := core.Instance().GetPersistentVolumeClaim(o.PVCName, o.PVCNamespace)
if err != nil {
return "", "", "", fmt.Errorf("error getting PVC object for (%v/%v): %v", o.PVCName, o.PVCNamespace, err)
}
pvName, err := core.Instance().GetVolumeForPersistentVolumeClaim(pvc)
if err != nil {
return "", "", "", fmt.Errorf("error getting PV name for PVC (%v/%v): %v", pvc.Namespace, pvc.Name, err)
}
pv, err := core.Instance().GetPersistentVolume(pvName)
if err != nil {
return "", "", "", fmt.Errorf("error getting pv %v: %v", pvName, err)
}
// In case the PV does not contain CSI secion itself, we will error out.
if pv.Spec.CSI == nil {
return "", "", "", fmt.Errorf("pv [%v] does not contain CSI section", pv.Name)
}
if o.SnapshotClassName == "" {
return "", "", "", fmt.Errorf("snapshot class cannot be empty, use 'default' to choose the default snapshot class")
}
if o.SnapshotClassName == "default" || o.SnapshotClassName == "Default" {
// Let kubernetes choose the default snapshot class to use
// for this snapshot. If none is set then the volume snapshot will fail
o.SnapshotClassName = ""
} else {
// For other snapshot class names ensure the volume snapshot class has
// been created
if err := c.ensureVolumeSnapshotClassCreated(pv.Spec.CSI.Driver, o.SnapshotClassName); err != nil {
return "", "", "", err
}
}
snapClassPtr := stringPtr(o.SnapshotClassName)
if o.SnapshotClassName == "" {
snapClassPtr = nil
}
vs := &kSnapshotv1beta1.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: o.Name,
Namespace: o.PVCNamespace,
Annotations: o.Annotations,
Labels: o.Labels,
},
Spec: kSnapshotv1beta1.VolumeSnapshotSpec{
VolumeSnapshotClassName: snapClassPtr,
Source: kSnapshotv1beta1.VolumeSnapshotSource{
PersistentVolumeClaimName: stringPtr(o.PVCName),
},
},
}
if _, err := c.snapshotClient.SnapshotV1beta1().VolumeSnapshots(o.PVCNamespace).Create(
context.TODO(),
vs,
metav1.CreateOptions{},
); err != nil {
if k8s_errors.IsAlreadyExists(err) {
return o.Name, o.PVCNamespace, pv.Spec.CSI.Driver, nil
}
return "", "", "", err
}
return o.Name, o.PVCNamespace, pv.Spec.CSI.Driver, nil
}
func (c *csiDriver) DeleteSnapshot(name, namespace string, retain bool) error {
vs, err := c.snapshotClient.SnapshotV1beta1().VolumeSnapshots(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
if k8s_errors.IsNotFound(err) {
return nil
}
return fmt.Errorf("failed to get volumesnapshot object %v/%v: %v", namespace, name, err)
}
if vs.Status == nil || vs.Status.BoundVolumeSnapshotContentName == nil {
return fmt.Errorf("failed to find get status for snapshot: %s/%s", vs.Namespace, vs.Name)
}
vsc, err := c.snapshotClient.SnapshotV1beta1().VolumeSnapshotContents().Get(context.TODO(), *vs.Status.BoundVolumeSnapshotContentName, metav1.GetOptions{})
if err != nil {
if k8s_errors.IsNotFound(err) {
return nil
}
return err
}
// ensure all vscontent have the desired delete policy
desiredRetainPolicy := kSnapshotv1beta1.VolumeSnapshotContentRetain
if !retain {
desiredRetainPolicy = kSnapshotv1beta1.VolumeSnapshotContentDelete
}
if vsc.Spec.DeletionPolicy != desiredRetainPolicy {
vsc.UID = ""
vsc.Spec.DeletionPolicy = desiredRetainPolicy
if _, err := c.snapshotClient.SnapshotV1beta1().VolumeSnapshotContents().Update(context.TODO(), vsc, metav1.UpdateOptions{}); err != nil {
return err
}
}
err = c.snapshotClient.SnapshotV1beta1().VolumeSnapshots(vs.Namespace).Delete(context.TODO(), vs.Name, metav1.DeleteOptions{})
if err != nil && !k8s_errors.IsNotFound(err) {
return err
}
err = c.snapshotClient.SnapshotV1beta1().VolumeSnapshotContents().Delete(context.TODO(), vsc.Name, metav1.DeleteOptions{})
if err != nil && !k8s_errors.IsNotFound(err) {
return err
}
logrus.Debugf("started clean up of %v/%v snapshots and %v snapshotcontents in namespace %v", vs.Namespace, vs.Name, vsc.Name, namespace)
return nil
}
func (c *csiDriver) SnapshotStatus(name, namespace string) (SnapshotInfo, error) {
var snapshotInfo SnapshotInfo
snapshot, err := c.snapshotClient.SnapshotV1beta1().VolumeSnapshots(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
snapshotInfo.Status = StatusFailed
snapshotInfo.Reason = fmt.Sprintf("snapshot %s lost during backup: %v", name, err)
return snapshotInfo, err
}
snapshotInfo.SnapshotRequest = snapshot
var snapshotClassName string
var snapshotClass *kSnapshotv1beta1.VolumeSnapshotClass
if snapshot.Spec.VolumeSnapshotClassName != nil {
snapshotClassName = *snapshot.Spec.VolumeSnapshotClassName
}
if len(snapshotClassName) > 0 {
snapshotClass, err = c.snapshotClient.SnapshotV1beta1().VolumeSnapshotClasses().Get(context.TODO(), snapshotClassName, metav1.GetOptions{})
if err != nil {
snapshotInfo.Status = StatusFailed
snapshotInfo.Reason = fmt.Sprintf("snapshot class %s lost during backup: %v", snapshotClassName, err)
return snapshotInfo, err
}
}
volumeSnapshotReady := c.snapshotReady(snapshot)
var volumeSnapshotContentReady bool
var snapshotContent *kSnapshotv1beta1.VolumeSnapshotContent
var contentName string
if volumeSnapshotReady && snapshot.Status.BoundVolumeSnapshotContentName != nil {
snapshotContentName := *snapshot.Status.BoundVolumeSnapshotContentName
snapshotContent, err = c.snapshotClient.SnapshotV1beta1().VolumeSnapshotContents().Get(context.TODO(), snapshotContentName, metav1.GetOptions{})
if err != nil {
snapshotInfo.Status = StatusFailed
snapshotInfo.Reason = fmt.Sprintf("snapshot content %s lost during backup: %v", snapshotClassName, err)
return snapshotInfo, err
}
snapshotInfo.Content = snapshotContent
snapshotInfo.Class = snapshotClass
volumeSnapshotContentReady = c.snapshotContentReady(snapshotContent)
contentName = snapshotContent.Name
}
// Evaluate current status of the backup for this PVC. Get all metadata and decide if finished.
var vsError string
if snapshot.Status != nil && snapshot.Status.Error != nil && snapshot.Status.Error.Message != nil {
vsError = *snapshot.Status.Error.Message
}
var pvcName string
if snapshot.Spec.Source.PersistentVolumeClaimName != nil {
pvcName = *snapshot.Spec.Source.PersistentVolumeClaimName
}
size := getSnapshotSize(snapshot)
if size == 0 && len(pvcName) != 0 {
// if restoreSize is empty, report PVC size
pvc, err := core.Instance().GetPersistentVolumeClaim(
pvcName,
namespace,
)
if err == nil {
size = getPVCSize(pvc)
}
}
var vscError string
if contentName != "" {
vscError = c.getSnapshotContentError(contentName)
}
switch {
case volumeSnapshotReady && volumeSnapshotContentReady:
snapshotInfo.Status = StatusReady
snapshotInfo.Reason = "Snapshot successful for volume"
snapshotInfo.Size = uint64(size)
case time.Now().After(snapshot.CreationTimestamp.Add(snapshotTimeout)):
snapshotInfo.Status = StatusFailed
snapshotInfo.Reason = formatReasonErrorMessage(fmt.Sprintf("snapshot timeout out after %s", snapshotTimeout.String()), vsError, vscError)
default:
if len(vsError) > 0 {
snapshotInfo.Status = StatusFailed
snapshotInfo.Reason = formatReasonErrorMessage(
fmt.Sprintf("volume snapshot failed for PVC %s", pvcName), vsError, vscError)
} else {
snapshotInfo.Status = StatusInProgress
snapshotInfo.Reason = formatReasonErrorMessage(
fmt.Sprintf("volume snapshot in progress for PVC %s", pvcName), vsError, vscError)
}
snapshotInfo.Size = uint64(size)
}
return snapshotInfo, nil
}
func (c *csiDriver) RestoreVolumeClaim(opts ...Option) (*v1.PersistentVolumeClaim, error) {
o := Options{}
for _, opt := range opts {
if opt != nil {
if err := opt(&o); err != nil {
return nil, err
}
}
}
pvc := &o.PVC
pvc = c.cleanK8sPVCAnnotations(pvc)
pvc.Namespace = o.RestoreNamespace
pvc.ResourceVersion = ""
pvc.Spec.VolumeName = ""
snapshot, err := c.snapshotClient.SnapshotV1beta1().VolumeSnapshots(o.RestoreNamespace).Get(context.TODO(), o.RestoreSnapshotName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("failed to get volumesnapshot %s/%s", o.RestoreNamespace, o.RestoreSnapshotName)
}
// Make the pvc size same as the restore size from the volumesnapshot
if snapshot.Status.RestoreSize != nil && !snapshot.Status.RestoreSize.IsZero() {
quantity, err := resource.ParseQuantity(snapshot.Status.RestoreSize.String())
if err != nil {
return nil, err
}
logrus.Debugf("setting size of pvc %s/%s same as snapshot size %s", pvc.Namespace, pvc.Name, quantity.String())
pvc.Spec.Resources.Requests[v1.ResourceStorage] = quantity
}
pvc.Spec.DataSource = &v1.TypedLocalObjectReference{
APIGroup: stringPtr("snapshot.storage.k8s.io"),
Kind: "VolumeSnapshot",
Name: o.RestoreSnapshotName,
}
pvc.Status = v1.PersistentVolumeClaimStatus{
Phase: v1.ClaimPending,
}
pvc, err = core.Instance().CreatePersistentVolumeClaim(pvc)
if err != nil {
if k8s_errors.IsAlreadyExists(err) {
return pvc, nil
}
return nil, fmt.Errorf("failed to create PVC %s: %s", pvc.Name, err.Error())
}
return pvc, nil
}
func (c *csiDriver) RestoreStatus(pvcName, namespace string) (RestoreInfo, error) {
var restoreInfo RestoreInfo
// Check on PVC status
pvc, err := core.Instance().GetPersistentVolumeClaim(pvcName, namespace)
if err != nil {
return restoreInfo, err
}
// Try to get VS. May not exist yet or may be cleaned up already.
vsName := pvc.Spec.DataSource.Name
var vsContentName string
var restoreSize uint64
var vsError string
if vs, err := c.snapshotClient.SnapshotV1beta1().VolumeSnapshots(namespace).Get(context.TODO(), vsName, metav1.GetOptions{}); err == nil && vs != nil {
// Leave vs as inline to avoid accessing volumesnapshot when it could be nil
restoreSize = getSnapshotSize(vs)
if vs.Status != nil && vs.Status.Error != nil && vs.Status.Error.Message != nil {
vsError = *vs.Status.Error.Message
}
if vs.Status != nil && vs.Status.BoundVolumeSnapshotContentName != nil {
vsContentName = *vs.Status.BoundVolumeSnapshotContentName
}
} else {
logrus.Warnf("did not find volume snapshot %s: %v", vsName, err)
}
// Try to get VSContent error message
var vscError string
if vsContentName != "" {
vscError = c.getSnapshotContentError(vsContentName)
}
// Use PVC size by default, but replace with restoreSize once it is ready
size := getPVCSize(pvc)
if restoreSize != 0 {
size = restoreSize
}
switch {
case c.pvcBindFinished(pvc):
err := c.deletejob(pvc, namespace)
if err != nil {
restoreInfo.Reason = fmt.Sprintf("Delete job for volume bind for PVC %s failed: %v", pvc.Name, err)
}
restoreInfo.VolumeName = pvc.Spec.VolumeName
restoreInfo.Size = size
restoreInfo.Status = StatusReady
case pvc.Status.Phase == v1.ClaimLost:
restoreInfo.Status = StatusFailed
restoreInfo.Reason = fmt.Sprintf("Volume restore failed: PVC %s is lost", pvc.Name)
case pvc.Status.Phase == v1.ClaimPending:
restoreInfo.Size = size
restoreInfo.Status = StatusInProgress
restoreInfo.Reason = formatReasonErrorMessage(fmt.Sprintf("Volume restore in progress: PVC %s is pending", pvc.Name), vsError, vscError)
if c.pvcWaitingForFirstConsumer(pvc) {
_, err := c.createJob(pvc, namespace)
if err != nil {
restoreInfo.Reason = fmt.Sprintf("Create job for volume bind for PVC %s failed: %v", pvc.Name, err)
}
}
}
if time.Now().After(pvc.CreationTimestamp.Add(restoreTimeout)) {
restoreInfo.Status = StatusFailed
restoreInfo.Reason = formatReasonErrorMessage(fmt.Sprintf("PVC restore timeout out after %s", restoreTimeout.String()), vsError, vscError)
}
return restoreInfo, nil
}
func (c *csiDriver) CancelRestore(pvcName, namespace string) error {
return core.Instance().DeletePersistentVolumeClaim(pvcName, namespace)
}
func (c *csiDriver) cleanK8sPVCAnnotations(pvc *v1.PersistentVolumeClaim) *v1.PersistentVolumeClaim {
if pvc.Annotations != nil {
newAnnotations := make(map[string]string)
// we will remove the following annotations to prevent controller confusion:
// - pv.kubernetes.io/bind-completed
// - pv.kubernetes.io/bound-by-controller
for key, val := range pvc.Annotations {
if key != annPVBindCompleted && key != annPVBoundByController {
newAnnotations[key] = val
}
}
pvc.Annotations = newAnnotations
}
return pvc
}
func (c *csiDriver) getVolumeSnapshotClass(snapshotClassName string) (*kSnapshotv1beta1.VolumeSnapshotClass, error) {
return c.snapshotClient.SnapshotV1beta1().VolumeSnapshotClasses().Get(context.TODO(), snapshotClassName, metav1.GetOptions{})
}
// GetVolumeSnapshotClass retrieves a backed up volume snapshot class
func (cbo *csiBackupObject) GetVolumeSnapshotClass(snapshotID string) (*kSnapshotv1beta1.VolumeSnapshotClass, error) {
vs, ok := cbo.VolumeSnapshots[snapshotID]
if !ok {
return nil, fmt.Errorf("failed to retrieve volume snapshot for snapshotID %s", snapshotID)
}
if vs.Spec.VolumeSnapshotClassName == nil {
return nil, fmt.Errorf("failed to retrieve volume snapshot class for snapshot %s. Volume snapshot class is undefined", snapshotID)
}
vsClassName := *vs.Spec.VolumeSnapshotClassName
vsClass, ok := cbo.VolumeSnapshotClasses[vsClassName]
if !ok {
return nil, fmt.Errorf("failed to retrieve volume snapshot class for snapshotID %s", snapshotID)
}
return vsClass, nil
}
func (c *csiDriver) createVolumeSnapshotClass(snapshotClassName, driverName string) (*kSnapshotv1beta1.VolumeSnapshotClass, error) {
return c.snapshotClient.SnapshotV1beta1().VolumeSnapshotClasses().Create(context.TODO(), &kSnapshotv1beta1.VolumeSnapshotClass{
ObjectMeta: metav1.ObjectMeta{
Name: snapshotClassName,
},
Driver: driverName,
DeletionPolicy: kSnapshotv1beta1.VolumeSnapshotContentRetain,
}, metav1.CreateOptions{})
}
func (c *csiDriver) ensureVolumeSnapshotClassCreated(csiDriverName, snapshotClassName string) error {
if !c.snapshotClassCreatedForDriver[csiDriverName] {
vsClass, err := c.getVolumeSnapshotClass(snapshotClassName)
if k8s_errors.IsNotFound(err) {
_, err = c.createVolumeSnapshotClass(snapshotClassName, csiDriverName)
if err != nil {
return err
}
logrus.Debugf("volumesnapshotclass created: %v", snapshotClassName)
} else if err != nil {
return err
}
// If we've found a vsClass, but it doesn't have a RetainPolicy, update to Retain.
// This is essential for the storage backend to not delete the snapshot.
// Some CSI drivers require specific VolumeSnapshotClass parameters, so we will leave those as is.
if vsClass.DeletionPolicy == kSnapshotv1beta1.VolumeSnapshotContentDelete {
vsClass.DeletionPolicy = kSnapshotv1beta1.VolumeSnapshotContentRetain
_, err = c.snapshotClient.SnapshotV1beta1().VolumeSnapshotClasses().Update(context.TODO(), vsClass, metav1.UpdateOptions{})
if err != nil {
return err
}
}
c.snapshotClassCreatedForDriver[snapshotClassName] = true
}
return nil
}
func (c *csiDriver) snapshotReady(vs *kSnapshotv1beta1.VolumeSnapshot) bool {
return vs.Status != nil && vs.Status.ReadyToUse != nil && *vs.Status.ReadyToUse
}
func (c *csiDriver) snapshotContentReady(vscontent *kSnapshotv1beta1.VolumeSnapshotContent) bool {
return vscontent.Status != nil && vscontent.Status.ReadyToUse != nil && *vscontent.Status.ReadyToUse
}
func formatReasonErrorMessage(reason, vsError, vscError string) string {
var snapshotError string
switch {
case vsError != "" && vscError != "" && vsError != vscError:
snapshotError = fmt.Sprintf("snapshot error: %s, snapshotContent error: %s", vsError, vscError)
case vsError != "":
snapshotError = fmt.Sprintf("snapshot error: %s", vsError)
case vscError != "":
snapshotError = fmt.Sprintf("snapshotContent error: %s", vscError)
}
if snapshotError != "" {
return fmt.Sprintf("%s: %s", reason, snapshotError)
}
return reason
}
func getPVCSize(pvc *v1.PersistentVolumeClaim) uint64 {
size := int64(0)
reqSize, ok := pvc.Spec.Resources.Requests[v1.ResourceStorage]
if !ok {
logrus.Errorf("failed to get PVC size from spec: %s", pvc.Name)
} else {
size, ok = reqSize.AsInt64()
if !ok {
logrus.Errorf("failed to convert PVC size: %s", pvc.Name)
}
}
return uint64(size)
}
func getSnapshotSize(vs *kSnapshotv1beta1.VolumeSnapshot) uint64 {
if vs.Status != nil && vs.Status.RestoreSize != nil {
size, _ := vs.Status.RestoreSize.AsInt64()
return uint64(size)
}
return 0
}
func (c *csiDriver) pvcBindFinished(pvc *v1.PersistentVolumeClaim) bool {
bindCompleted := pvc.Annotations[annPVBindCompleted]
boundByController := pvc.Annotations[annPVBoundByController]
return pvc.Status.Phase == v1.ClaimBound && bindCompleted == "yes" && boundByController == "yes"
}
func (c *csiDriver) pvcWaitingForFirstConsumer(pvc *v1.PersistentVolumeClaim) bool {
var sc *storagev1.StorageClass
var err error
storageClassName := k8shelper.GetPersistentVolumeClaimClass(pvc)
if storageClassName != "" {
sc, err = storage.Instance().GetStorageClass(storageClassName)
if err != nil {
logrus.Warnf("did not get the storageclass %s for pvc %s/%s, err: %v", storageClassName, pvc.Namespace, pvc.Name, err)
return false
}
return *sc.VolumeBindingMode == storagev1.VolumeBindingWaitForFirstConsumer
}
return false
}
func (c *csiDriver) getSnapshotContentError(vscName string) string {
if vsc, err := c.snapshotClient.SnapshotV1beta1().VolumeSnapshotContents().Get(context.TODO(), vscName, metav1.GetOptions{}); err == nil && vsc != nil {
if vsc.Status != nil && vsc.Status.Error != nil && vsc.Status.Error.Message != nil {
return *vsc.Status.Error.Message
}
} else {
logrus.Warnf("did not find volume snapshot content %s: %v", vscName, err)
}
return ""
}
func stringPtr(s string) *string {
return &s
}
// uploadObject uploads the given data to the backup location
func (c *csiDriver) UploadSnapshotObjects(
backupLocation *storkapi.BackupLocation,
snapshotInfoList []SnapshotInfo,
objectPath, objectName string,
) error {
vsMap := make(map[string]*kSnapshotv1beta1.VolumeSnapshot)
vsContentMap := make(map[string]*kSnapshotv1beta1.VolumeSnapshotContent)
vsClassMap := make(map[string]*kSnapshotv1beta1.VolumeSnapshotClass)
for _, snapshotInfo := range snapshotInfoList {
snapID := snapshotInfo.SnapshotRequest.(*kSnapshotv1beta1.VolumeSnapshot).Name
vsMap[snapID] = snapshotInfo.SnapshotRequest.(*kSnapshotv1beta1.VolumeSnapshot)
vsContentMap[snapID] = snapshotInfo.Content.(*kSnapshotv1beta1.VolumeSnapshotContent)
snapshotClassName := snapshotInfo.Class.(*kSnapshotv1beta1.VolumeSnapshotClass).Name
vsClassMap[snapshotClassName] = snapshotInfo.Class.(*kSnapshotv1beta1.VolumeSnapshotClass)
}
csiBackup := csiBackupObject{
VolumeSnapshots: vsMap,
VolumeSnapshotContents: vsContentMap,
VolumeSnapshotClasses: vsClassMap,
}
data, err := json.Marshal(csiBackup)
if err != nil {
return err
}
bucket, err := objectstore.GetBucket(backupLocation)
if err != nil {
return err
}
if backupLocation.Location.EncryptionKey != "" {
if data, err = crypto.Encrypt(data, backupLocation.Location.EncryptionKey); err != nil {
return err
}
}
writer, err := bucket.NewWriter(context.TODO(), filepath.Join(objectPath, objectName), nil)
if err != nil {
return err
}
_, err = writer.Write(data)
if err != nil {
closeErr := writer.Close()
if closeErr != nil {
logrus.Errorf("error closing writer for objectstore: %v", closeErr)
}
return err
}
err = writer.Close()
if err != nil {
logrus.Errorf("error closing writer for objectstore: %v", err)
return err
}
return nil
}
func (c *csiDriver) DownloadSnapshotObjects(
backupLocation *storkapi.BackupLocation,
objectPath string,
) ([]SnapshotInfo, error) {
var snapshotInfoList []SnapshotInfo
bucket, err := objectstore.GetBucket(backupLocation)
if err != nil {
return snapshotInfoList, err
}
exists, err := bucket.Exists(context.TODO(), objectPath)
if err != nil || !exists {
return snapshotInfoList, err
}
data, err := bucket.ReadAll(context.TODO(), objectPath)
if err != nil {
return snapshotInfoList, err
}
if backupLocation.Location.EncryptionKey != "" {
if data, err = crypto.Decrypt(data, backupLocation.Location.EncryptionKey); err != nil {
return snapshotInfoList, err
}
}
cbo := &csiBackupObject{}
err = json.Unmarshal(data, cbo)
if err != nil {
return snapshotInfoList, err
}
for snapID := range cbo.VolumeSnapshots {
var snapshotInfo SnapshotInfo
snapshotInfo.SnapshotRequest = cbo.VolumeSnapshots[snapID]
snapshotInfo.Content = cbo.VolumeSnapshotContents[snapID]
snapshotInfo.Class, err = cbo.GetVolumeSnapshotClass(snapID)
if err != nil {
return snapshotInfoList, err
}
snapshotInfoList = append(snapshotInfoList, snapshotInfo)
}
return snapshotInfoList, nil
}
func (c *csiDriver) DeleteSnapshotObject(
backupLocation *storkapi.BackupLocation,
objectPath string,
) error {
bucket, err := objectstore.GetBucket(backupLocation)
if err != nil {
return err
}
if objectPath != "" {
if err = bucket.Delete(context.TODO(), objectPath); err != nil && gcerrors.Code(err) != gcerrors.NotFound {
return fmt.Errorf("error deleting object %s: %v", objectPath, err)
}
}
return nil
}
func (c *csiDriver) restoreVolumeSnapshotClass(vsClass *kSnapshotv1beta1.VolumeSnapshotClass) (*kSnapshotv1beta1.VolumeSnapshotClass, error) {
vsClass.ResourceVersion = ""
vsClass.UID = ""
newVSClass, err := c.snapshotClient.SnapshotV1beta1().VolumeSnapshotClasses().Create(context.TODO(), vsClass, metav1.CreateOptions{})
if err != nil {
if k8s_errors.IsAlreadyExists(err) {
return vsClass, nil
}
return nil, err
}
return newVSClass, nil
}
func (c *csiDriver) restoreVolumeSnapshot(
namespace string,
vs *kSnapshotv1beta1.VolumeSnapshot,
vsc *kSnapshotv1beta1.VolumeSnapshotContent,
) (*kSnapshotv1beta1.VolumeSnapshot, error) {
vs.ResourceVersion = ""
vs.Spec.Source.PersistentVolumeClaimName = nil
vs.Spec.Source.VolumeSnapshotContentName = &vsc.Name
vs.Namespace = namespace
newVS, err := c.snapshotClient.SnapshotV1beta1().VolumeSnapshots(namespace).Create(context.TODO(), vs, metav1.CreateOptions{})
if err != nil {
if k8s_errors.IsAlreadyExists(err) {
return vs, nil
}
return nil, err
}
return newVS, nil
}
func (c *csiDriver) restoreVolumeSnapshotContent(
namespace string,
vs *kSnapshotv1beta1.VolumeSnapshot,
vsc *kSnapshotv1beta1.VolumeSnapshotContent,
retain bool,
) (*kSnapshotv1beta1.VolumeSnapshotContent, error) {
snapshotHandle := *vsc.Status.SnapshotHandle
vsc.ResourceVersion = ""
vsc.Spec.Source.VolumeHandle = nil
vsc.Spec.Source.SnapshotHandle = &snapshotHandle
vsc.Spec.VolumeSnapshotRef.Name = vs.Name
vsc.Spec.VolumeSnapshotRef.Namespace = namespace
vsc.Spec.VolumeSnapshotRef.UID = vs.UID
// ensure all vscontent have the desired delete policy
desiredRetainPolicy := kSnapshotv1beta1.VolumeSnapshotContentRetain
if !retain {
desiredRetainPolicy = kSnapshotv1beta1.VolumeSnapshotContentDelete
}
vsc.Spec.DeletionPolicy = desiredRetainPolicy
newVSC, err := c.snapshotClient.SnapshotV1beta1().VolumeSnapshotContents().Create(context.TODO(), vsc, metav1.CreateOptions{})
if err != nil {
if k8s_errors.IsAlreadyExists(err) {
return vsc, nil
}
return nil, err
}
return newVSC, nil
}
func (c *csiDriver) RecreateSnapshotResources(
snapshotInfo SnapshotInfo,
snapshotDriverName string,
namespace string,
retain bool,
) (SnapshotInfo, error) {
var err error
var newSnapshotInfo SnapshotInfo
// Get VSC and VS
vsc := snapshotInfo.Content.(*kSnapshotv1beta1.VolumeSnapshotContent)
vs := snapshotInfo.SnapshotRequest.(*kSnapshotv1beta1.VolumeSnapshot)
vsClass := snapshotInfo.Class.(*kSnapshotv1beta1.VolumeSnapshotClass)
logrus.Infof("recreating snapshot resources for %s/%s", namespace, vs.Name)
// make sure snapshot class is created for this object.
// if we have already created it in this batch, do not check if created already.
err = c.ensureVolumeSnapshotClassCreated(snapshotDriverName, vsClass.Name)
if err != nil {
return newSnapshotInfo, err
}
// Create vsClass
newVSClass, err := c.restoreVolumeSnapshotClass(vsClass)
if err != nil {
return newSnapshotInfo, fmt.Errorf("failed to restore VolumeSnapshotClass for deletion: %s", err.Error())
}
newSnapshotInfo.Class = newVSClass
logrus.Debugf("created volume snapshot class %s", vsClass.Name)
// Create VS, bound to VSC
newVS, err := c.restoreVolumeSnapshot(namespace, vs, vsc)
if err != nil {
return newSnapshotInfo, fmt.Errorf("failed to restore VolumeSnapshot for deletion: %s", err.Error())
}
newSnapshotInfo.SnapshotRequest = newVS
logrus.Debugf("created volume snapshot %s/%s", vs.Namespace, vs.Name)
// Create VSC
newVSC, err := c.restoreVolumeSnapshotContent(namespace, newVS, vsc, retain)
if err != nil {
return newSnapshotInfo, err
}
newSnapshotInfo.Content = newVSC
logrus.Debugf("created volume snapshot content %s for snapshot %s/%s", vsc.Name, vs.Namespace, vs.Name)
return newSnapshotInfo, nil
}
func getBackupInfoFromObjectKey(objKey string) (string, string) {
var backupUID, timestamp string
keySplits := strings.Split(objKey, "/")
fileName := keySplits[len(keySplits)-1]
fileSplits := strings.Split(fileName, "-")
backupUID = strings.Join(fileSplits[0:len(fileSplits)-1], "-")
timestamp = strings.Split(fileSplits[len(fileSplits)-1], ".")[0]
return backupUID, timestamp
}
func (c *csiDriver) getCSISnapshotsCRList(backupLocation *storkapi.BackupLocation, pvcUID, objectPath string) ([]string, error) {
var vsList []string
var timestamps []string
timestampBackupMapping := make(map[string]string)
bucket, err := objectstore.GetBucket(backupLocation)
if err != nil {
return vsList, err
}
iterator := bucket.List(&blob.ListOptions{
Prefix: fmt.Sprintf("%s/", objectPath),
})
for {
object, err := iterator.Next(context.TODO())
if err == io.EOF {
break
}
if err != nil {
return vsList, err
}
if object.IsDir {
continue
}
backupUID, timestamp := getBackupInfoFromObjectKey(object.Key)
logrus.Debugf("volumes snapshots file: %s, backupUID: %s, timestamp: %s", object.Key, backupUID, timestamp)
timestamps = append(timestamps, timestamp)
timestampBackupMapping[timestamp] = object.Key
}
sort.Strings(timestamps)
for _, timestamp := range timestamps {
vsList = append(vsList, timestampBackupMapping[timestamp])
}
return vsList, nil
}
func (c *csiDriver) RetainLocalSnapshots(
backupLocation *storkapi.BackupLocation,
snapshotDriverName string,
snapshotClassName string,
namespace string,
pvcUID string,
objectPath string,
retain bool,
) error {
vsCRList, err := c.getCSISnapshotsCRList(backupLocation, pvcUID, objectPath)
if err != nil {
return fmt.Errorf("failed in getting list of older volumesnapshot CRs from objectstore : %v", err)
}
for index, volumeSnapshotCR := range vsCRList {
snapshotInfoList, err := c.DownloadSnapshotObjects(backupLocation, volumeSnapshotCR)
if err != nil {
return err
}
for _, snapshotInfo := range snapshotInfoList {
vs := snapshotInfo.SnapshotRequest.(*kSnapshotv1beta1.VolumeSnapshot)
snapName := vs.Name
vs.Annotations[snapDeleteAnnotation] = "true"
snapshotInfo.SnapshotRequest = vs
newSnapInfo, err := c.RecreateSnapshotResources(snapshotInfo, snapshotDriverName, namespace, retain)
if err != nil {
return fmt.Errorf("recreating snapshot resources failed for snapshot %s/%s: %v", namespace, snapName, err)
}
logrus.Debugf("successfully recreated snapshot resources for snapshot %s/%s", namespace, snapName)
// Check if the snapshot is scheduled for restore, then don't delete it
vs = newSnapInfo.SnapshotRequest.(*kSnapshotv1beta1.VolumeSnapshot)
if restoreAnnotation, ok := vs.Annotations[snapRestoreAnnotation]; ok {
if restoreAnnotation == "true" {
logrus.Debugf("volumesnapshot %s is set for restore, hence not deleting it", vs.Name)
continue
}
}
// Waiting for vs to get bound with vsc as in cleanup path by the time deletesnapshot is running,
// vsc may not be bound to vs.
err = c.waitForVolumeSnapshotBound(vs, namespace)
if err == nil {
if index < len(vsCRList)-localCSIRetention {
retain = false
} else {
retain = true
}
err = c.DeleteSnapshot(
vs.Name,
namespace,
retain, // retain snapshot content
)
}
if err != nil {
logrus.Errorf("failed to delete the old snapshot %s: %v", vs.Name, err)
}
logrus.Debugf("successfully deleted the recreated snapshot resources for snapshot %s/%s", namespace, snapName)
if !retain {
// Delete the CRs from objectstore
err = c.DeleteSnapshotObject(backupLocation, volumeSnapshotCR)
if err != nil {
logrus.Errorf("deleting the CRs from objectstore failed for snapshot %s/%s: %v", namespace, snapName, err)
}
}
logrus.Debugf("successfully deleted snapshot resources in objectstore for snapshot %s/%s", namespace, snapName)
}
}
return nil
}
func (c *csiDriver) getLocalSnapshot(backupLocation *storkapi.BackupLocation, pvcUID, backupUID, objectPath string) (SnapshotInfo, error) {
var snapshotInfo SnapshotInfo
var found bool
var vsCRPath string
vsCRList, err := c.getCSISnapshotsCRList(backupLocation, pvcUID, objectPath)
if err != nil {
return snapshotInfo, fmt.Errorf("failed in getting list of older volumesnapshot CRs from objectstore : %v", err)
}
for _, volumeSnapshotCR := range vsCRList {
tempBackupUID, _ := getBackupInfoFromObjectKey(volumeSnapshotCR)
if tempBackupUID == backupUID {
logrus.Debugf("resources of local snapshot for backup %s are present in the objectpath %s", backupUID, objectPath)
vsCRPath = volumeSnapshotCR
found = true
break
}
}
if !found {
msg := fmt.Sprintf("local snapshot resources for backup %s are not found", backupUID)
logrus.Debugf(msg)
return snapshotInfo, nil
}
snapshotInfoList, err := c.DownloadSnapshotObjects(backupLocation, vsCRPath)
if err != nil {
return snapshotInfo, err
}
if len(snapshotInfoList) == 0 {