Skip to content

Commit ab96efb

Browse files
committed
inherit k8s default volumeSnapshotClass
Signed-off-by: hu-keyu <[email protected]>
1 parent 0f81772 commit ab96efb

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
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

0 commit comments

Comments
 (0)