Skip to content

Commit 5160f96

Browse files
authored
issue8294: inherit k8s default volumeSnapshotClass (#8721)
* inherit k8s default volumeSnapshotClass Signed-off-by: hu-keyu <hzldd999@gmail.com> * add doc change for issue8294 Signed-off-by: hu-keyu <hzldd999@gmail.com> --------- Signed-off-by: hu-keyu <hzldd999@gmail.com>
1 parent 8934b2c commit 5160f96

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

changelogs/unreleased/8719-hu-keyu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Inherit k8s default volumeSnapshotClass.

pkg/apis/velero/v1/labels_annotations.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ const (
127127
VolumeSnapshotClassDriverBackupAnnotationPrefix = "velero.io/csi-volumesnapshot-class"
128128
VolumeSnapshotClassDriverPVCAnnotation = "velero.io/csi-volumesnapshot-class"
129129

130+
// https://kubernetes.io/zh-cn/docs/concepts/storage/volume-snapshot-classes/
131+
VolumeSnapshotClassKubernetesAnnotation = "snapshot.storage.kubernetes.io/is-default-class"
132+
130133
// There is no release w/ these constants exported. Using the strings for now.
131134
// CSI Annotation volumesnapshotclass
132135
// https://github.com/kubernetes-csi/external-snapshotter/blob/master/pkg/utils/util.go#L59-L60

pkg/util/csi/volume_snapshot.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,13 +432,23 @@ func GetVolumeSnapshotClassForStorageClass(
432432
}
433433
}
434434
}
435+
// not found by label, pick by annotation
436+
for _, sc := range snapshotClasses.Items {
437+
_, hasDefaultAnnotation := sc.Annotations[velerov1api.VolumeSnapshotClassKubernetesAnnotation]
438+
if sc.Driver == provisioner {
439+
vsClass = sc
440+
if hasDefaultAnnotation {
441+
return &sc, nil
442+
}
443+
}
444+
}
435445
// If there's only one volumesnapshotclass for the driver, return it.
436446
if n == 1 {
437447
return &vsClass, nil
438448
}
439449
return nil, fmt.Errorf(
440-
"failed to get VolumeSnapshotClass for provisioner %s, ensure that the desired VolumeSnapshot class has the %s label",
441-
provisioner, velerov1api.VolumeSnapshotClassSelectorLabel)
450+
"failed to get VolumeSnapshotClass for provisioner %s, ensure that the desired VolumeSnapshot class has the %s label or %s annotation",
451+
provisioner, velerov1api.VolumeSnapshotClassSelectorLabel, velerov1api.VolumeSnapshotClassKubernetesAnnotation)
442452
}
443453

444454
// IsVolumeSnapshotClassHasListerSecret returns whether a volumesnapshotclass has a snapshotlister secret

site/content/docs/main/csi.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,26 @@ This section documents some of the choices made during implementing the CSI snap
4545
1. VolumeSnapshots created by the Velero CSI plugins are retained only for the lifetime of the backup even if the `DeletionPolicy` on the VolumeSnapshotClass is set to `Retain`. To accomplish this, during deletion of the backup the prior to deleting the VolumeSnapshot, VolumeSnapshotContent object is patched to set its `DeletionPolicy` to `Delete`. Deleting the VolumeSnapshot object will result in cascade delete of the VolumeSnapshotContent and the snapshot in the storage provider.
4646
2. VolumeSnapshotContent objects created during a `velero backup` that are dangling, unbound to a VolumeSnapshot object, will be discovered, using labels, and deleted on backup deletion.
4747
3. The Velero CSI plugins, to backup CSI backed PVCs, will choose the VolumeSnapshotClass in the cluster based on the following logic:
48-
1. **Default Behavior:**
48+
1. **Default Behavior Based On Annotation:**
49+
You can specify a default VolumeSnapshotClass for VolumeSnapshots that don't request any particular class to bind to by adding the snapshot.storage.kubernetes.io/is-default-class: "true" annotation.
50+
For example, if you want to create a VolumeSnapshotClass for the CSI driver `disk.csi.cloud.com` for taking snapshots of disks created with `disk.csi.cloud.com` based storage classes, you can create a VolumeSnapshotClass like this:
51+
```yaml
52+
apiVersion: snapshot.storage.k8s.io/v1
53+
kind: VolumeSnapshotClass
54+
metadata:
55+
name: test-snapclass-by-annotation
56+
annotations:
57+
snapshot.storage.kubernetes.io/is-default-class: "true"
58+
driver: disk.csi.cloud.com
59+
```
60+
Note: If multiple CSI drivers exist, a default VolumeSnapshotClass can be specified for each of them.
61+
2. **Default Behavior Based On Label:**
4962
You can simply create a VolumeSnapshotClass for a particular driver and put a label on it to indicate that it is the default VolumeSnapshotClass for that driver. For example, if you want to create a VolumeSnapshotClass for the CSI driver `disk.csi.cloud.com` for taking snapshots of disks created with `disk.csi.cloud.com` based storage classes, you can create a VolumeSnapshotClass like this:
5063
```yaml
5164
apiVersion: snapshot.storage.k8s.io/v1
5265
kind: VolumeSnapshotClass
5366
metadata:
54-
name: test-snapclass
67+
name: test-snapclass-by-label
5568
labels:
5669
velero.io/csi-volumesnapshot-class: "true"
5770
driver: disk.csi.cloud.com

0 commit comments

Comments
 (0)