Skip to content

Commit

Permalink
Fix segmentation violation (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
panigs7 authored Nov 28, 2023
1 parent ec5257e commit 4af665d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/k8s/volume_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func (f VolumeFinder) GetPersistentVolumes() ([]VolumeInfo, error) {
continue
}

// Check added to skip PV s which do not have any PVC s
if volume.Spec.ClaimRef == nil {
f.Logger.Debugf("The PV, %s , do not have a claim \n", volume.Name)
continue
}

info := VolumeInfo{
Namespace: claim.Namespace,
PersistentVolumeClaim: string(claim.UID),
Expand Down
91 changes: 91 additions & 0 deletions internal/k8s/volume_finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/dell/karavi-metrics-powerflex/internal/k8s"
"github.com/dell/karavi-metrics-powerflex/internal/k8s/mocks"
"github.com/sirupsen/logrus"

corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -386,6 +387,96 @@ func Test_K8sPersistentVolumeFinder(t *testing.T) {
},
})), ctrl
},
"success skipping volumes that has no pvc claim": func(*testing.T) (k8s.VolumeFinder, []checkFn, *gomock.Controller) {
ctrl := gomock.NewController(t)
api := mocks.NewMockVolumeGetter(ctrl)

t1, err := time.Parse(time.RFC3339, "2020-07-28T20:00:00+00:00")
assert.Nil(t, err)

volumes := &corev1.PersistentVolumeList{
Items: []corev1.PersistentVolume{
{
ObjectMeta: metav1.ObjectMeta{
Name: "persistent-volume-name1",
CreationTimestamp: metav1.Time{Time: t1},
},
Spec: corev1.PersistentVolumeSpec{
Capacity: map[corev1.ResourceName]resource.Quantity{
v1.ResourceStorage: resource.MustParse("16Gi"),
},
PersistentVolumeSource: corev1.PersistentVolumeSource{
CSI: &corev1.CSIPersistentVolumeSource{
Driver: "csi-vxflexos.dellemc.com",
VolumeAttributes: map[string]string{
"Name": "storage-system-volume-name1",
"StorageSystemID": "storagesystemid1",
},
VolumeHandle: "storagesystemid1-volumeid1",
},
},
ClaimRef: &corev1.ObjectReference{
Name: "pvc-name1",
Namespace: "namespace-1",
UID: "pvc-uid",
},
StorageClassName: "storage-class-name",
},
Status: corev1.PersistentVolumeStatus{
Phase: "Bound",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "persistent-volume-name-2",
CreationTimestamp: metav1.Time{Time: t1},
},
Spec: corev1.PersistentVolumeSpec{
Capacity: map[corev1.ResourceName]resource.Quantity{
v1.ResourceStorage: resource.MustParse("8Gi"),
},
PersistentVolumeSource: corev1.PersistentVolumeSource{
CSI: &corev1.CSIPersistentVolumeSource{
Driver: "csi-vxflexos.dellemc.com",
VolumeAttributes: map[string]string{
"Name": "persistent-volume-name-2",
"systemId": "storagesystemid1",
},
VolumeHandle: "storagesystemid1-volumeid2",
},
},
ClaimRef: nil,
StorageClassName: "storage-class-name-2",
},
Status: corev1.PersistentVolumeStatus{
Phase: "Available",
},
},
},
}

api.EXPECT().GetPersistentVolumes().Times(1).Return(volumes, nil)

ids := make([]k8s.StorageSystemID, 1)
ids[0] = k8s.StorageSystemID{ID: "storagesystemid1", DriverNames: []string{"csi-vxflexos.dellemc.com"}}

finder := k8s.VolumeFinder{API: api, StorageSystemID: ids, Logger: logrus.New()}
return finder, check(hasNoError, checkExpectedOutput([]k8s.VolumeInfo{
{
Namespace: "namespace-1",
PersistentVolumeClaim: "pvc-uid",
PersistentVolumeStatus: "Bound",
VolumeClaimName: "pvc-name1",
PersistentVolume: "persistent-volume-name1",
StorageClass: "storage-class-name",
Driver: "csi-vxflexos.dellemc.com",
ProvisionedSize: "16Gi",
StorageSystemVolumeName: "storage-system-volume-name1",
StorageSystemID: "storagesystemid1",
CreatedTime: t1.String(),
},
})), ctrl
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
Expand Down

0 comments on commit 4af665d

Please sign in to comment.