diff --git a/internal/util/labels_annotations.go b/internal/util/labels_annotations.go index afd1bda8..8e1611dc 100644 --- a/internal/util/labels_annotations.go +++ b/internal/util/labels_annotations.go @@ -26,6 +26,10 @@ const ( CSIVSCDeletionPolicy = "velero.io/csi-vsc-deletion-policy" VolumeSnapshotClassSelectorLabel = "velero.io/csi-volumesnapshot-class" + // For some csi providers, the provisioner name of storageclass is no the same as driver name in snapshotclass + // We provide this annotation in snapshotclass to let velero detect the different name + VolumeSnapshotClassProvisionerAnnotation = "velero.io/csi-volumesnapshot-class-provisioner" + // There is no release w/ these constants exported. Using the strings for now. // CSI Labels volumesnapshotclass // https://github.com/kubernetes-csi/external-snapshotter/blob/master/pkg/utils/util.go#L59-L60 diff --git a/internal/util/util.go b/internal/util/util.go index c22f2451..4ed5bfa7 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -132,10 +132,18 @@ func GetVolumeSnapshotClassForStorageClass(provisioner string, snapshotClient sn // https://github.com/kubernetes-csi/external-snapshotter/blob/release-4.2/client/config/crd/snapshot.storage.k8s.io_volumesnapshotclasses.yaml for _, sc := range snapshotClasses.Items { _, hasLabelSelector := sc.Labels[VolumeSnapshotClassSelectorLabel] - if sc.Driver == provisioner && hasLabelSelector { - return &sc, nil + if hasLabelSelector { + if sc.Driver == provisioner { + return &sc, nil + } else { // check snapshotclass provisioner annotation exists + annotationValue, hasAnnotationsSelector := GetAnnotation(&sc.ObjectMeta, VolumeSnapshotClassProvisionerAnnotation) + if hasAnnotationsSelector && annotationValue == provisioner { + return &sc, nil + } + } } } + return nil, errors.Errorf("failed to get volumesnapshotclass for provisioner %s, ensure that the desired volumesnapshot class has the %s label", provisioner, VolumeSnapshotClassSelectorLabel) } @@ -244,6 +252,15 @@ func IsVolumeSnapshotHasVSCDeleteSecret(vs *snapshotv1api.VolumeSnapshot) bool { return nameExists && nsExists } +// GetAnnotation get the annotations on the object +func GetAnnotation(o *metav1.ObjectMeta, key string) (string, bool) { + if o.Annotations == nil { + return "", false + } + v, exist := o.Annotations[key] + return v, exist +} + // AddAnnotations adds the supplied key-values to the annotations on the object func AddAnnotations(o *metav1.ObjectMeta, vals map[string]string) { if o.Annotations == nil {