From f27db232c0c2ee426b724c1eb22167c5b13c4a15 Mon Sep 17 00:00:00 2001 From: hu-keyu Date: Mon, 24 Feb 2025 17:20:20 +0800 Subject: [PATCH 1/4] inherit k8s default volumeSnapshotClass --- pkg/apis/velero/v1/labels_annotations.go | 3 +++ pkg/util/csi/volume_snapshot.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/pkg/apis/velero/v1/labels_annotations.go b/pkg/apis/velero/v1/labels_annotations.go index c86b4e91bf..78d231ba96 100644 --- a/pkg/apis/velero/v1/labels_annotations.go +++ b/pkg/apis/velero/v1/labels_annotations.go @@ -127,6 +127,9 @@ const ( VolumeSnapshotClassDriverBackupAnnotationPrefix = "velero.io/csi-volumesnapshot-class" VolumeSnapshotClassDriverPVCAnnotation = "velero.io/csi-volumesnapshot-class" + // https://kubernetes.io/zh-cn/docs/concepts/storage/volume-snapshot-classes/ + VolumeSnapshotClassKubernetesAnnotation = "snapshot.storage.kubernetes.io/is-default-class" + // There is no release w/ these constants exported. Using the strings for now. // CSI Annotation volumesnapshotclass // https://github.com/kubernetes-csi/external-snapshotter/blob/master/pkg/utils/util.go#L59-L60 diff --git a/pkg/util/csi/volume_snapshot.go b/pkg/util/csi/volume_snapshot.go index 2cdc7a869d..6d54f88d75 100644 --- a/pkg/util/csi/volume_snapshot.go +++ b/pkg/util/csi/volume_snapshot.go @@ -424,12 +424,16 @@ func GetVolumeSnapshotClassForStorageClass( // values for the other fields in the spec. for _, sc := range snapshotClasses.Items { _, hasLabelSelector := sc.Labels[velerov1api.VolumeSnapshotClassSelectorLabel] + _, hasDefaultAnnotation := sc.Annotations[velerov1api.VolumeSnapshotClassKubernetesAnnotation] if sc.Driver == provisioner { n++ vsClass = sc if hasLabelSelector { return &sc, nil } + if hasDefaultAnnotation { + return &sc, nil + } } } // If there's only one volumesnapshotclass for the driver, return it. From c44507e0a9a618fa9a7519107cd4c74a5d30ea17 Mon Sep 17 00:00:00 2001 From: hu-keyu Date: Tue, 25 Feb 2025 10:38:34 +0800 Subject: [PATCH 2/4] add changelog --- changelogs/unreleased/8719-hu-keyu | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelogs/unreleased/8719-hu-keyu diff --git a/changelogs/unreleased/8719-hu-keyu b/changelogs/unreleased/8719-hu-keyu new file mode 100644 index 0000000000..eb6925ff22 --- /dev/null +++ b/changelogs/unreleased/8719-hu-keyu @@ -0,0 +1 @@ +Inherit k8s default volumeSnapshotClass. \ No newline at end of file From 48d5a20ca92b6b9adb29a82b9a6afdcb58eed10d Mon Sep 17 00:00:00 2001 From: hu-keyu Date: Tue, 25 Feb 2025 13:52:09 +0800 Subject: [PATCH 3/4] add changelog --- pkg/util/csi/volume_snapshot.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/util/csi/volume_snapshot.go b/pkg/util/csi/volume_snapshot.go index 6d54f88d75..623525cfc9 100644 --- a/pkg/util/csi/volume_snapshot.go +++ b/pkg/util/csi/volume_snapshot.go @@ -424,13 +424,19 @@ func GetVolumeSnapshotClassForStorageClass( // values for the other fields in the spec. for _, sc := range snapshotClasses.Items { _, hasLabelSelector := sc.Labels[velerov1api.VolumeSnapshotClassSelectorLabel] - _, hasDefaultAnnotation := sc.Annotations[velerov1api.VolumeSnapshotClassKubernetesAnnotation] if sc.Driver == provisioner { n++ vsClass = sc if hasLabelSelector { return &sc, nil } + } + } + // not found by label, pick by annotation + for _, sc := range snapshotClasses.Items { + _, hasDefaultAnnotation := sc.Annotations[velerov1api.VolumeSnapshotClassKubernetesAnnotation] + if sc.Driver == provisioner { + vsClass = sc if hasDefaultAnnotation { return &sc, nil } From b656f3ac86949f4c1c69af96ca2eea30feb7e0aa Mon Sep 17 00:00:00 2001 From: hu-keyu Date: Tue, 25 Feb 2025 13:56:43 +0800 Subject: [PATCH 4/4] add changelog --- pkg/util/csi/volume_snapshot.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/util/csi/volume_snapshot.go b/pkg/util/csi/volume_snapshot.go index 623525cfc9..1b372781c3 100644 --- a/pkg/util/csi/volume_snapshot.go +++ b/pkg/util/csi/volume_snapshot.go @@ -447,8 +447,8 @@ func GetVolumeSnapshotClassForStorageClass( return &vsClass, nil } return nil, fmt.Errorf( - "failed to get VolumeSnapshotClass for provisioner %s, ensure that the desired VolumeSnapshot class has the %s label", - provisioner, velerov1api.VolumeSnapshotClassSelectorLabel) + "failed to get VolumeSnapshotClass for provisioner %s, ensure that the desired VolumeSnapshot class has the %s label or %s annotation", + provisioner, velerov1api.VolumeSnapshotClassSelectorLabel, velerov1api.VolumeSnapshotClassKubernetesAnnotation) } // IsVolumeSnapshotClassHasListerSecret returns whether a volumesnapshotclass has a snapshotlister secret