diff --git a/changelogs/unreleased/7472-sbahar619 b/changelogs/unreleased/7472-sbahar619 new file mode 100644 index 0000000000..54ed1debff --- /dev/null +++ b/changelogs/unreleased/7472-sbahar619 @@ -0,0 +1,2 @@ +FS backup create PodVolumeBackup when the backup excluded PVC, +so I added logic to skip PVC volume type when PVC is not included in the backup resources to be backed up. \ No newline at end of file diff --git a/pkg/backup/item_backupper.go b/pkg/backup/item_backupper.go index 40589ad708..927b770dbf 100644 --- a/pkg/backup/item_backupper.go +++ b/pkg/backup/item_backupper.go @@ -203,7 +203,12 @@ func (ib *itemBackupper) backupItemInternal(logger logrus.FieldLogger, obj runti // Get the list of volumes to back up using pod volume backup from the pod's annotations. Remove from this list // any volumes that use a PVC that we've already backed up (this would be in a read-write-many scenario, // where it's been backed up from another pod), since we don't need >1 backup per PVC. - includedVolumes, optedOutVolumes := pdvolumeutil.GetVolumesByPod(pod, boolptr.IsSetToTrue(ib.backupRequest.Spec.DefaultVolumesToFsBackup)) + includedVolumes, optedOutVolumes := pdvolumeutil.GetVolumesByPod( + pod, + boolptr.IsSetToTrue(ib.backupRequest.Spec.DefaultVolumesToFsBackup), + !ib.backupRequest.ResourceIncludesExcludes.ShouldInclude(kuberesource.PersistentVolumeClaims.String()), + ) + for _, volume := range includedVolumes { // track the volumes that are PVCs using the PVC snapshot tracker, so that when we backup PVCs/PVs // via an item action in the next step, we don't snapshot PVs that will have their data backed up diff --git a/pkg/util/podvolume/pod_volume.go b/pkg/util/podvolume/pod_volume.go index 94e969b3b1..a4107e6c28 100644 --- a/pkg/util/podvolume/pod_volume.go +++ b/pkg/util/podvolume/pod_volume.go @@ -26,7 +26,7 @@ import ( ) // GetVolumesByPod returns a list of volume names to backup for the provided pod. -func GetVolumesByPod(pod *corev1api.Pod, defaultVolumesToFsBackup bool) ([]string, []string) { +func GetVolumesByPod(pod *corev1api.Pod, defaultVolumesToFsBackup, backupExcludePVC bool) ([]string, []string) { // tracks the volumes that have been explicitly opted out of backup via the annotation in the pod optedOutVolumes := make([]string, 0) @@ -58,6 +58,9 @@ func GetVolumesByPod(pod *corev1api.Pod, defaultVolumesToFsBackup bool) ([]strin if pv.DownwardAPI != nil { continue } + if pv.PersistentVolumeClaim != nil && backupExcludePVC { + continue + } // don't backup volumes that are included in the exclude list. if contains(volsToExclude, pv.Name) { optedOutVolumes = append(optedOutVolumes, pv.Name) diff --git a/pkg/util/podvolume/pod_volume_test.go b/pkg/util/podvolume/pod_volume_test.go index b4898e6b4a..19174e4340 100644 --- a/pkg/util/podvolume/pod_volume_test.go +++ b/pkg/util/podvolume/pod_volume_test.go @@ -80,6 +80,7 @@ func TestGetVolumesByPod(t *testing.T) { optedOut []string } defaultVolumesToFsBackup bool + backupExcludePVC bool }{ { name: "should get PVs from VolumesToBackupAnnotation when defaultVolumesToFsBackup is false", @@ -329,11 +330,43 @@ func TestGetVolumesByPod(t *testing.T) { optedOut: []string{}, }, }, + { + name: "should exclude PVC volume when backup excludes PVC resource", + defaultVolumesToFsBackup: true, + backupExcludePVC: true, + pod: &corev1api.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + velerov1api.VolumesToExcludeAnnotation: "nonPvbPV1,nonPvbPV2,nonPvbPV3", + }, + }, + Spec: corev1api.PodSpec{ + Volumes: []corev1api.Volume{ + {Name: "pvbPV1"}, {Name: "pvbPV2"}, {Name: "pvbPV3"}, + { + Name: "downwardAPI", + VolumeSource: corev1api.VolumeSource{ + PersistentVolumeClaim: &corev1api.PersistentVolumeClaimVolumeSource{ + ClaimName: "testPVC", + }, + }, + }, + }, + }, + }, + expected: struct { + included []string + optedOut []string + }{ + included: []string{"pvbPV1", "pvbPV2", "pvbPV3"}, + optedOut: []string{}, + }, + }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - actualIncluded, actualOptedOut := GetVolumesByPod(tc.pod, tc.defaultVolumesToFsBackup) + actualIncluded, actualOptedOut := GetVolumesByPod(tc.pod, tc.defaultVolumesToFsBackup, tc.backupExcludePVC) sort.Strings(tc.expected.included) sort.Strings(actualIncluded)