Skip to content

Commit aa837d1

Browse files
committed
Fix detecting default annotation on VolumeGroupSnapshotClass
The `IsDefaultAnnotation()` function has been extended to check for the correct default annotation, taking the Kind of the object into consideration.
1 parent ca0b24b commit aa837d1

File tree

4 files changed

+107
-5
lines changed

4 files changed

+107
-5
lines changed

pkg/common-controller/groupsnapshot_controller_helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (ctrl *csiSnapshotCommonController) SetDefaultGroupSnapshotClass(groupSnaps
140140

141141
defaultClasses := []*crdv1alpha1.VolumeGroupSnapshotClass{}
142142
for _, groupSnapshotClass := range list {
143-
if utils.IsDefaultAnnotation(groupSnapshotClass.ObjectMeta) && pvDriver == groupSnapshotClass.Driver {
143+
if utils.IsDefaultAnnotation(groupSnapshotClass.TypeMeta, groupSnapshotClass.ObjectMeta) && pvDriver == groupSnapshotClass.Driver {
144144
defaultClasses = append(defaultClasses, groupSnapshotClass)
145145
klog.V(5).Infof("get defaultGroupClass added: %s, driver: %s", groupSnapshotClass.Name, pvDriver)
146146
}

pkg/common-controller/snapshot_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ func (ctrl *csiSnapshotCommonController) SetDefaultSnapshotClass(snapshot *crdv1
14161416

14171417
defaultClasses := []*crdv1.VolumeSnapshotClass{}
14181418
for _, class := range list {
1419-
if utils.IsDefaultAnnotation(class.ObjectMeta) && pvDriver == class.Driver {
1419+
if utils.IsDefaultAnnotation(class.TypeMeta, class.ObjectMeta) && pvDriver == class.Driver {
14201420
defaultClasses = append(defaultClasses, class)
14211421
klog.V(5).Infof("get defaultClass added: %s, driver: %s", class.Name, pvDriver)
14221422
}

pkg/utils/util.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,16 @@ func GetDynamicSnapshotContentNameForSnapshot(snapshot *crdv1.VolumeSnapshot) st
262262

263263
// IsDefaultAnnotation returns a boolean if
264264
// the annotation is set
265-
func IsDefaultAnnotation(obj metav1.ObjectMeta) bool {
266-
if obj.Annotations[IsDefaultSnapshotClassAnnotation] == "true" {
267-
return true
265+
func IsDefaultAnnotation(tm metav1.TypeMeta, obj metav1.ObjectMeta) bool {
266+
switch tm.Kind {
267+
case "VolumeSnapshotClass":
268+
if obj.Annotations[IsDefaultSnapshotClassAnnotation] == "true" {
269+
return true
270+
}
271+
case "VolumeGroupSnapshotClass":
272+
if obj.Annotations[IsDefaultGroupSnapshotClassAnnotation] == "true" {
273+
return true
274+
}
268275
}
269276

270277
return false

pkg/utils/util_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,98 @@ func TestRemovePrefixedCSIParams(t *testing.T) {
207207
}
208208
}
209209
}
210+
211+
func TestIsDefaultAnnotation(t *testing.T) {
212+
testcases := []struct {
213+
name string
214+
typeMeta metav1.TypeMeta
215+
objectMeta metav1.ObjectMeta
216+
isDefault bool
217+
}{
218+
{
219+
name: "no default annotation in snapshot class",
220+
typeMeta: metav1.TypeMeta{
221+
Kind: "VolumeSnapshotClass",
222+
},
223+
objectMeta: metav1.ObjectMeta{
224+
Annotations: map[string]string{},
225+
},
226+
isDefault: false,
227+
},
228+
{
229+
name: "with default annotation in snapshot class",
230+
typeMeta: metav1.TypeMeta{
231+
Kind: "VolumeSnapshotClass",
232+
},
233+
objectMeta: metav1.ObjectMeta{
234+
Annotations: map[string]string{
235+
IsDefaultSnapshotClassAnnotation: "true",
236+
},
237+
},
238+
isDefault: true,
239+
},
240+
{
241+
name: "with default=false annotation in snapshot class",
242+
typeMeta: metav1.TypeMeta{
243+
Kind: "VolumeSnapshotClass",
244+
},
245+
objectMeta: metav1.ObjectMeta{
246+
Annotations: map[string]string{
247+
IsDefaultSnapshotClassAnnotation: "false",
248+
},
249+
},
250+
isDefault: false,
251+
},
252+
{
253+
name: "no default annotation in group snapshot class",
254+
typeMeta: metav1.TypeMeta{
255+
Kind: "VolumeGroupSnapshotClass",
256+
},
257+
objectMeta: metav1.ObjectMeta{
258+
Annotations: map[string]string{},
259+
},
260+
isDefault: false,
261+
},
262+
{
263+
name: "with default annotation in group snapshot class",
264+
typeMeta: metav1.TypeMeta{
265+
Kind: "VolumeGroupSnapshotClass",
266+
},
267+
objectMeta: metav1.ObjectMeta{
268+
Annotations: map[string]string{
269+
IsDefaultGroupSnapshotClassAnnotation: "true",
270+
},
271+
},
272+
isDefault: true,
273+
},
274+
{
275+
name: "with default=false annotation in group snapshot class",
276+
typeMeta: metav1.TypeMeta{
277+
Kind: "VolumeGroupSnapshotClass",
278+
},
279+
objectMeta: metav1.ObjectMeta{
280+
Annotations: map[string]string{
281+
IsDefaultGroupSnapshotClassAnnotation: "false",
282+
},
283+
},
284+
isDefault: false,
285+
},
286+
{
287+
name: "unknown kind, not a snapshot or group snapshot class",
288+
typeMeta: metav1.TypeMeta{
289+
Kind: "PersistentVolume",
290+
},
291+
objectMeta: metav1.ObjectMeta{
292+
Annotations: map[string]string{},
293+
},
294+
isDefault: false,
295+
},
296+
}
297+
for _, tc := range testcases {
298+
t.Logf("test: %s", tc.name)
299+
isDefault := IsDefaultAnnotation(tc.typeMeta, tc.objectMeta)
300+
if tc.isDefault != isDefault {
301+
t.Fatalf("default annotation on class incorrectly detected: %v != %v", isDefault, tc.isDefault)
302+
}
303+
}
304+
}

0 commit comments

Comments
 (0)