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

Fix detecting default annotation on VolumeGroupSnapshotClass #1015

Merged
merged 1 commit into from
Feb 22, 2024
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
2 changes: 1 addition & 1 deletion pkg/common-controller/groupsnapshot_controller_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (ctrl *csiSnapshotCommonController) SetDefaultGroupSnapshotClass(groupSnaps

defaultClasses := []*crdv1alpha1.VolumeGroupSnapshotClass{}
for _, groupSnapshotClass := range list {
if utils.IsDefaultAnnotation(groupSnapshotClass.ObjectMeta) && pvDriver == groupSnapshotClass.Driver {
if utils.IsDefaultAnnotation(groupSnapshotClass.TypeMeta, groupSnapshotClass.ObjectMeta) && pvDriver == groupSnapshotClass.Driver {
defaultClasses = append(defaultClasses, groupSnapshotClass)
klog.V(5).Infof("get defaultGroupClass added: %s, driver: %s", groupSnapshotClass.Name, pvDriver)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/common-controller/snapshot_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ func (ctrl *csiSnapshotCommonController) SetDefaultSnapshotClass(snapshot *crdv1

defaultClasses := []*crdv1.VolumeSnapshotClass{}
for _, class := range list {
if utils.IsDefaultAnnotation(class.ObjectMeta) && pvDriver == class.Driver {
if utils.IsDefaultAnnotation(class.TypeMeta, class.ObjectMeta) && pvDriver == class.Driver {
defaultClasses = append(defaultClasses, class)
klog.V(5).Infof("get defaultClass added: %s, driver: %s", class.Name, pvDriver)
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,16 @@ func GetDynamicSnapshotContentNameForSnapshot(snapshot *crdv1.VolumeSnapshot) st

// IsDefaultAnnotation returns a boolean if
// the annotation is set
func IsDefaultAnnotation(obj metav1.ObjectMeta) bool {
if obj.Annotations[IsDefaultSnapshotClassAnnotation] == "true" {
return true
func IsDefaultAnnotation(tm metav1.TypeMeta, obj metav1.ObjectMeta) bool {
switch tm.Kind {
case "VolumeSnapshotClass":
if obj.Annotations[IsDefaultSnapshotClassAnnotation] == "true" {
return true
}
case "VolumeGroupSnapshotClass":
if obj.Annotations[IsDefaultGroupSnapshotClassAnnotation] == "true" {
return true
}
}

return false
Expand Down
95 changes: 95 additions & 0 deletions pkg/utils/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,98 @@ func TestRemovePrefixedCSIParams(t *testing.T) {
}
}
}

func TestIsDefaultAnnotation(t *testing.T) {
testcases := []struct {
name string
typeMeta metav1.TypeMeta
objectMeta metav1.ObjectMeta
isDefault bool
}{
{
name: "no default annotation in snapshot class",
typeMeta: metav1.TypeMeta{
Kind: "VolumeSnapshotClass",
},
objectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
},
isDefault: false,
},
{
name: "with default annotation in snapshot class",
typeMeta: metav1.TypeMeta{
Kind: "VolumeSnapshotClass",
},
objectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
IsDefaultSnapshotClassAnnotation: "true",
},
},
isDefault: true,
},
{
name: "with default=false annotation in snapshot class",
typeMeta: metav1.TypeMeta{
Kind: "VolumeSnapshotClass",
},
objectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
IsDefaultSnapshotClassAnnotation: "false",
},
},
isDefault: false,
},
{
name: "no default annotation in group snapshot class",
typeMeta: metav1.TypeMeta{
Kind: "VolumeGroupSnapshotClass",
},
objectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
},
isDefault: false,
},
{
name: "with default annotation in group snapshot class",
typeMeta: metav1.TypeMeta{
Kind: "VolumeGroupSnapshotClass",
},
objectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
IsDefaultGroupSnapshotClassAnnotation: "true",
},
},
isDefault: true,
},
{
name: "with default=false annotation in group snapshot class",
typeMeta: metav1.TypeMeta{
Kind: "VolumeGroupSnapshotClass",
},
objectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
IsDefaultGroupSnapshotClassAnnotation: "false",
},
},
isDefault: false,
},
{
name: "unknown kind, not a snapshot or group snapshot class",
typeMeta: metav1.TypeMeta{
Kind: "PersistentVolume",
},
objectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
},
isDefault: false,
},
}
for _, tc := range testcases {
t.Logf("test: %s", tc.name)
isDefault := IsDefaultAnnotation(tc.typeMeta, tc.objectMeta)
if tc.isDefault != isDefault {
t.Fatalf("default annotation on class incorrectly detected: %v != %v", isDefault, tc.isDefault)
}
}
}