Skip to content

Commit

Permalink
feature annotation for snapshotclass provisioner
Browse files Browse the repository at this point in the history
Signed-off-by: big-appled<[email protected]>
  • Loading branch information
big-appled committed Apr 13, 2022
1 parent bec108a commit 3839eba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions internal/util/labels_annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3839eba

Please sign in to comment.