Skip to content

Commit 4f6440c

Browse files
committed
Correctly detect newly created Volume[Group]SnapshotClasses
The controller is using an informer to get VolumeGroupSnapshotClasses and VolumeSnapshotClasses. Unfortunately, objects returned by that will not have the API Kind info set. This prevented the IsDefaultAnnotation function from working correctly. This patch avoid relying on the object Kind to know whether a VolumeGroupSnapshotClass or a VolumeSnapshotClass is set as default. See: kubernetes/client-go#308 Signed-off-by: Leonardo Cecchi <[email protected]>
1 parent a50e4b8 commit 4f6440c

File tree

4 files changed

+31
-49
lines changed

4 files changed

+31
-49
lines changed

pkg/common-controller/groupsnapshot_controller_helper.go

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

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

pkg/common-controller/snapshot_controller.go

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

14011401
defaultClasses := []*crdv1.VolumeSnapshotClass{}
14021402
for _, class := range list {
1403-
if utils.IsDefaultAnnotation(class.TypeMeta, class.ObjectMeta) && pvDriver == class.Driver {
1403+
if utils.IsVolumeSnapshotClassDefaultAnnotation(class.ObjectMeta) && pvDriver == class.Driver {
14041404
defaultClasses = append(defaultClasses, class)
14051405
klog.V(5).Infof("get defaultClass added: %s, driver: %s", class.Name, pvDriver)
14061406
}

pkg/utils/util.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,21 +274,16 @@ func GetDynamicSnapshotContentNameForSnapshot(snapshot *crdv1.VolumeSnapshot) st
274274
return "snapcontent-" + string(snapshot.UID)
275275
}
276276

277-
// IsDefaultAnnotation returns a boolean if
278-
// the annotation is set
279-
func IsDefaultAnnotation(tm metav1.TypeMeta, obj metav1.ObjectMeta) bool {
280-
switch tm.Kind {
281-
case "VolumeSnapshotClass":
282-
if obj.Annotations[IsDefaultSnapshotClassAnnotation] == "true" {
283-
return true
284-
}
285-
case "VolumeGroupSnapshotClass":
286-
if obj.Annotations[IsDefaultGroupSnapshotClassAnnotation] == "true" {
287-
return true
288-
}
289-
}
277+
// IsVolumeSnapshotClassDefaultAnnotation returns a true boolean if
278+
// a VolumeSnapshotClass is marked as the default one
279+
func IsVolumeSnapshotClassDefaultAnnotation(obj metav1.ObjectMeta) bool {
280+
return obj.Annotations[IsDefaultSnapshotClassAnnotation] == "true"
281+
}
290282

291-
return false
283+
// IsVolumeGroupSnapshotClassDefaultAnnotation returns a true boolean if
284+
// a VolumeGroupSnapshotClass is marked as the default one
285+
func IsVolumeGroupSnapshotClassDefaultAnnotation(obj metav1.ObjectMeta) bool {
286+
return obj.Annotations[IsDefaultGroupSnapshotClassAnnotation] == "true"
292287
}
293288

294289
// verifyAndGetSecretNameAndNamespaceTemplate gets the values (templates) associated

pkg/utils/util_test.go

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -201,28 +201,21 @@ func TestRemovePrefixedCSIParams(t *testing.T) {
201201
}
202202
}
203203

204-
func TestIsDefaultAnnotation(t *testing.T) {
204+
func TestIsVolumeSnapshotClassDefaultAnnotation(t *testing.T) {
205205
testcases := []struct {
206206
name string
207-
typeMeta metav1.TypeMeta
208207
objectMeta metav1.ObjectMeta
209208
isDefault bool
210209
}{
211210
{
212211
name: "no default annotation in snapshot class",
213-
typeMeta: metav1.TypeMeta{
214-
Kind: "VolumeSnapshotClass",
215-
},
216212
objectMeta: metav1.ObjectMeta{
217-
Annotations: map[string]string{},
213+
Annotations: nil,
218214
},
219215
isDefault: false,
220216
},
221217
{
222218
name: "with default annotation in snapshot class",
223-
typeMeta: metav1.TypeMeta{
224-
Kind: "VolumeSnapshotClass",
225-
},
226219
objectMeta: metav1.ObjectMeta{
227220
Annotations: map[string]string{
228221
IsDefaultSnapshotClassAnnotation: "true",
@@ -232,31 +225,38 @@ func TestIsDefaultAnnotation(t *testing.T) {
232225
},
233226
{
234227
name: "with default=false annotation in snapshot class",
235-
typeMeta: metav1.TypeMeta{
236-
Kind: "VolumeSnapshotClass",
237-
},
238228
objectMeta: metav1.ObjectMeta{
239229
Annotations: map[string]string{
240230
IsDefaultSnapshotClassAnnotation: "false",
241231
},
242232
},
243233
isDefault: false,
244234
},
235+
}
236+
for _, tc := range testcases {
237+
t.Logf("test: %s", tc.name)
238+
isDefault := IsVolumeSnapshotClassDefaultAnnotation(tc.objectMeta)
239+
if tc.isDefault != isDefault {
240+
t.Fatalf("default annotation on class incorrectly detected: %v != %v", isDefault, tc.isDefault)
241+
}
242+
}
243+
}
244+
245+
func TestIsVolumeGroupSnapshotClassDefaultAnnotation(t *testing.T) {
246+
testcases := []struct {
247+
name string
248+
objectMeta metav1.ObjectMeta
249+
isDefault bool
250+
}{
245251
{
246252
name: "no default annotation in group snapshot class",
247-
typeMeta: metav1.TypeMeta{
248-
Kind: "VolumeGroupSnapshotClass",
249-
},
250253
objectMeta: metav1.ObjectMeta{
251-
Annotations: map[string]string{},
254+
Annotations: nil,
252255
},
253256
isDefault: false,
254257
},
255258
{
256259
name: "with default annotation in group snapshot class",
257-
typeMeta: metav1.TypeMeta{
258-
Kind: "VolumeGroupSnapshotClass",
259-
},
260260
objectMeta: metav1.ObjectMeta{
261261
Annotations: map[string]string{
262262
IsDefaultGroupSnapshotClassAnnotation: "true",
@@ -266,30 +266,17 @@ func TestIsDefaultAnnotation(t *testing.T) {
266266
},
267267
{
268268
name: "with default=false annotation in group snapshot class",
269-
typeMeta: metav1.TypeMeta{
270-
Kind: "VolumeGroupSnapshotClass",
271-
},
272269
objectMeta: metav1.ObjectMeta{
273270
Annotations: map[string]string{
274271
IsDefaultGroupSnapshotClassAnnotation: "false",
275272
},
276273
},
277274
isDefault: false,
278275
},
279-
{
280-
name: "unknown kind, not a snapshot or group snapshot class",
281-
typeMeta: metav1.TypeMeta{
282-
Kind: "PersistentVolume",
283-
},
284-
objectMeta: metav1.ObjectMeta{
285-
Annotations: map[string]string{},
286-
},
287-
isDefault: false,
288-
},
289276
}
290277
for _, tc := range testcases {
291278
t.Logf("test: %s", tc.name)
292-
isDefault := IsDefaultAnnotation(tc.typeMeta, tc.objectMeta)
279+
isDefault := IsVolumeGroupSnapshotClassDefaultAnnotation(tc.objectMeta)
293280
if tc.isDefault != isDefault {
294281
t.Fatalf("default annotation on class incorrectly detected: %v != %v", isDefault, tc.isDefault)
295282
}

0 commit comments

Comments
 (0)