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

Check whether the volume's source is PVC before fetching its PV. #7967

Merged
merged 1 commit into from
Jul 3, 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
1 change: 1 addition & 0 deletions changelogs/unreleased/7967-blackpiglet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Check whether the volume's source is PVC before fetching its PV.
26 changes: 15 additions & 11 deletions internal/volumehelper/volume_policy_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,24 @@
}

if v.volumePolicy != nil {
pvc, err := kubeutil.GetPVCForPodVolume(&volume, &pod, v.client)
if err != nil {
v.logger.WithError(err).Errorf("fail to get PVC for pod %s", pod.Namespace+"/"+pod.Name)
return false, err
}
pv, err := kubeutil.GetPVForPVC(pvc, v.client)
if err != nil {
v.logger.WithError(err).Errorf("fail to get PV for PVC %s", pvc.Namespace+"/"+pvc.Name)
return false, err
var resource interface{}
resource = &volume
if volume.VolumeSource.PersistentVolumeClaim != nil {
pvc, err := kubeutil.GetPVCForPodVolume(&volume, &pod, v.client)
if err != nil {
v.logger.WithError(err).Errorf("fail to get PVC for pod %s", pod.Namespace+"/"+pod.Name)
return false, err

Check warning on line 152 in internal/volumehelper/volume_policy_helper.go

View check run for this annotation

Codecov / codecov/patch

internal/volumehelper/volume_policy_helper.go#L151-L152

Added lines #L151 - L152 were not covered by tests
}
resource, err = kubeutil.GetPVForPVC(pvc, v.client)
if err != nil {
v.logger.WithError(err).Errorf("fail to get PV for PVC %s", pvc.Namespace+"/"+pvc.Name)
return false, err

Check warning on line 157 in internal/volumehelper/volume_policy_helper.go

View check run for this annotation

Codecov / codecov/patch

internal/volumehelper/volume_policy_helper.go#L156-L157

Added lines #L156 - L157 were not covered by tests
}
}

action, err := v.volumePolicy.GetMatchAction(pv)
action, err := v.volumePolicy.GetMatchAction(resource)
if err != nil {
v.logger.WithError(err).Errorf("fail to get VolumePolicy match action for PV %s", pv.Name)
v.logger.WithError(err).Error("fail to get VolumePolicy match action for volume")

Check warning on line 163 in internal/volumehelper/volume_policy_helper.go

View check run for this annotation

Codecov / codecov/patch

internal/volumehelper/volume_policy_helper.go#L163

Added line #L163 was not covered by tests
return false, err
}

Expand Down
26 changes: 26 additions & 0 deletions internal/volumehelper/volume_policy_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,32 @@ func TestVolumeHelperImpl_ShouldPerformFSBackup(t *testing.T) {
shouldFSBackup: true,
expectedErr: false,
},
{
name: "Volume source is emptyDir, VolumePolicy match, return true and no error",
pod: builder.ForPod("ns", "pod-1").
Volumes(
&corev1.Volume{
Name: "",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
}).Result(),
resourcePolicies: &resourcepolicies.ResourcePolicies{
Version: "v1",
VolumePolicies: []resourcepolicies.VolumePolicy{
{
Conditions: map[string]interface{}{
"volumeTypes": []string{"emptyDir"},
},
Action: resourcepolicies.Action{
Type: resourcepolicies.FSBackup,
},
},
},
},
shouldFSBackup: true,
expectedErr: false,
},
{
name: "VolumePolicy match, action type is not fs-backup, return false and no error",
pod: builder.ForPod("ns", "pod-1").
Expand Down
Loading