Skip to content

Commit 0e79412

Browse files
Add nil check for claimRef to avoid segmentation violation error (#38)
Add nil check 4 claims to avoid seg violation error
1 parent 6db2d06 commit 0e79412

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

internal/k8s/volume_finder.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ func (f VolumeFinder) GetPersistentVolumes(_ context.Context) ([]VolumeInfo, err
6969
f.Logger.Debugf("The PV, %s , is not provisioned by a CSI driver\n", volume.GetName())
7070
continue
7171
}
72+
73+
// Check added to skip PV s which do not have any PVC s
74+
if volume.Spec.ClaimRef == nil {
75+
f.Logger.Debugf("The PV, %s , do not have a claim \n", volume.GetName())
76+
continue
77+
}
78+
7279
if contains(f.DriverNames, volume.Spec.CSI.Driver) {
7380
capacity := volume.Spec.Capacity[corev1.ResourceStorage]
7481
claim := volume.Spec.ClaimRef

internal/k8s/volume_finder_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,106 @@ func Test_K8sPersistentVolumeFinder(t *testing.T) {
364364
},
365365
})), ctrl
366366
},
367+
"success filtering the persistent volumes which do not have claims": func(*testing.T) (k8s.VolumeFinder, []checkFn, *gomock.Controller) {
368+
ctrl := gomock.NewController(t)
369+
api := mocks.NewMockVolumeGetter(ctrl)
370+
371+
t1, err := time.Parse(time.RFC3339, "2022-06-06T20:00:00+00:00")
372+
assert.Nil(t, err)
373+
374+
volumes := &corev1.PersistentVolumeList{
375+
Items: []corev1.PersistentVolume{
376+
{
377+
ObjectMeta: metav1.ObjectMeta{
378+
Name: "k8s-7242537ae1",
379+
CreationTimestamp: metav1.Time{Time: t1},
380+
},
381+
Spec: corev1.PersistentVolumeSpec{
382+
Capacity: map[corev1.ResourceName]resource.Quantity{
383+
corev1.ResourceStorage: resource.MustParse("16Gi"),
384+
},
385+
PersistentVolumeSource: corev1.PersistentVolumeSource{
386+
CSI: &corev1.CSIPersistentVolumeSource{
387+
Driver: "csi-isilon.dellemc.com",
388+
VolumeAttributes: map[string]string{
389+
"AccessZone": "System",
390+
"AzServiceIP": "127.0.0.1",
391+
"ClusterName": "pieisi93x",
392+
"ID": "19",
393+
"Name": "k8s-7242537ae1",
394+
"Path": "/ifs/data/csi/k8s-7242537ae1",
395+
"RootClientEnabled": "false",
396+
"storage.kubernetes.io/csiProvisionerIdentity": "1652862466458-8081-csi-isilon.dellemc.com",
397+
},
398+
VolumeHandle: "k8s-7242537ae1=_=_=19=_=_=System=_=_=pieisi93x",
399+
},
400+
},
401+
ClaimRef: &corev1.ObjectReference{
402+
Name: "pvc-name",
403+
Namespace: "namespace-1",
404+
UID: "pvc-uid",
405+
},
406+
StorageClassName: "storage-class-name",
407+
},
408+
Status: corev1.PersistentVolumeStatus{
409+
Phase: "Bound",
410+
},
411+
},
412+
{
413+
ObjectMeta: metav1.ObjectMeta{
414+
Name: "k8s-7242537ae2",
415+
CreationTimestamp: metav1.Time{Time: t1},
416+
},
417+
Spec: corev1.PersistentVolumeSpec{
418+
Capacity: map[corev1.ResourceName]resource.Quantity{
419+
corev1.ResourceStorage: resource.MustParse("16Gi"),
420+
},
421+
PersistentVolumeSource: corev1.PersistentVolumeSource{
422+
CSI: &corev1.CSIPersistentVolumeSource{
423+
Driver: "csi-isilon.dellemc.com",
424+
VolumeAttributes: map[string]string{
425+
"AccessZone": "System",
426+
"AzServiceIP": "127.0.0.1",
427+
"ClusterName": "pieisi93x",
428+
"ID": "19",
429+
"Name": "k8s-7242537ae2",
430+
"Path": "/ifs/data/csi/k8s-7242537ae2",
431+
"RootClientEnabled": "false",
432+
"storage.kubernetes.io/csiProvisionerIdentity": "1652862466458-8081-csi-isilon.dellemc.com",
433+
},
434+
VolumeHandle: "k8s-7242537ae2=_=_=19=_=_=System=_=_=pieisi93x",
435+
},
436+
},
437+
ClaimRef: nil,
438+
StorageClassName: "storage-class-name",
439+
},
440+
Status: corev1.PersistentVolumeStatus{
441+
Phase: "Available",
442+
},
443+
},
444+
},
445+
}
446+
447+
api.EXPECT().GetPersistentVolumes().Times(1).Return(volumes, nil)
448+
449+
finder := k8s.VolumeFinder{API: api, DriverNames: []string{"csi-isilon.dellemc.com"}, Logger: logrus.New()}
450+
return finder, check(hasNoError, checkExpectedOutput([]k8s.VolumeInfo{
451+
{
452+
Namespace: "namespace-1",
453+
PersistentVolumeClaim: "pvc-uid",
454+
PersistentVolumeStatus: "Bound",
455+
VolumeClaimName: "pvc-name",
456+
PersistentVolume: "k8s-7242537ae1",
457+
StorageClass: "storage-class-name",
458+
Driver: "csi-isilon.dellemc.com",
459+
ProvisionedSize: "16Gi",
460+
VolumeHandle: "k8s-7242537ae1=_=_=19=_=_=System=_=_=pieisi93x",
461+
IsiPath: "/ifs/data/csi",
462+
CreatedTime: t1.String(),
463+
},
464+
})), ctrl
465+
},
466+
367467
"error calling k8s": func(*testing.T) (k8s.VolumeFinder, []checkFn, *gomock.Controller) {
368468
ctrl := gomock.NewController(t)
369469
api := mocks.NewMockVolumeGetter(ctrl)

0 commit comments

Comments
 (0)