Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the logic to get VSClass for CSI snapshot #173

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,26 @@ func GetVolumeSnapshotClassForStorageClass(provisioner string, snapshotClient sn
if err != nil {
return nil, errors.Wrap(err, "error listing volumesnapshot classes")
}
n := 0
var vsclass snapshotv1api.VolumeSnapshotClass
// We pick the volumesnapshotclass that matches the CSI driver name and has a 'velero.io/csi-volumesnapshot-class'
// label. This allows multiple VolumesnapshotClasses for the same driver with different values for the
// other fields in the spec.
// 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 sc.Driver == provisioner {
n += 1
vsclass = sc
if hasLabelSelector {
return &sc, nil
}
}
}
// If there's only one volumesnapshotclass for the driver, return it.
if n == 1 {
return &vsclass, 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
30 changes: 25 additions & 5 deletions internal/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,21 @@ func TestGetVolumeSnapshotCalssForStorageClass(t *testing.T) {
Driver: "baz.csi.k8s.io",
}

objs := []runtime.Object{hostpathClass, fooClass, barClass, bazClass}
ambClass1 := &snapshotv1api.VolumeSnapshotClass{
ObjectMeta: metav1.ObjectMeta{
Name: "amb1",
},
Driver: "amb.csi.k8s.io",
}

ambClass2 := &snapshotv1api.VolumeSnapshotClass{
ObjectMeta: metav1.ObjectMeta{
Name: "amb2",
},
Driver: "amb.csi.k8s.io",
}

objs := []runtime.Object{hostpathClass, fooClass, barClass, bazClass, ambClass1, ambClass2}
fakeClient := snapshotFake.NewSimpleClientset(objs...)

testCases := []struct {
Expand All @@ -681,15 +695,21 @@ func TestGetVolumeSnapshotCalssForStorageClass(t *testing.T) {
expectError: false,
},
{
name: "should find var volumesnapshotclass",
name: "should find bar volumesnapshotclass",
driverName: "bar.csi.k8s.io",
expectedVSC: barClass,
expectError: false,
},
{
name: "should not find foo volumesnapshotclass without \"velero.io/csi-volumesnapshot-class\" label",
name: "should find baz volumesnapshotclass without \"velero.io/csi-volumesnapshot-class\" label, b/c there's only one vsclass matching the driver name",
driverName: "baz.csi.k8s.io",
expectedVSC: bazClass,
expectError: false,
},
{
name: "should not find amb volumesnapshotclass without \"velero.io/csi-volumesnapshot-class\" label, b/c there're more than one vsclass matching the driver name",
driverName: "amb.csi.k8s.io",
expectedVSC: nil,
expectError: true,
},
{
Expand All @@ -710,8 +730,8 @@ func TestGetVolumeSnapshotCalssForStorageClass(t *testing.T) {
return
}

assert.Equalf(t, tc.expectedVSC.Name, actualVSC.Name, "unexpected volumesnapshotclass name returned. Want: %s; Got:%s", tc.name, tc.expectedVSC.Name, actualVSC.Name)
assert.Equalf(t, tc.expectedVSC.Driver, actualVSC.Driver, "unexpected driver name returned. Want: %s; Got:%s", tc.name, tc.expectedVSC.Driver, actualVSC.Driver)
assert.Equalf(t, tc.expectedVSC.Name, actualVSC.Name, "unexpected volumesnapshotclass name returned. Want: %s; Got:%s", tc.expectedVSC.Name, actualVSC.Name)
assert.Equalf(t, tc.expectedVSC.Driver, actualVSC.Driver, "unexpected driver name returned. Want: %s; Got:%s", tc.expectedVSC.Driver, actualVSC.Driver)
})
}
}
Expand Down