2018-06-18 17:54:07 +00:00
|
|
|
/*
|
2021-02-18 18:30:52 +00:00
|
|
|
Copyright the Velero contributors.
|
2018-06-18 17:54:07 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
2020-03-24 21:50:48 +00:00
|
|
|
"os"
|
2018-06-18 17:54:07 +00:00
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-10-23 14:36:11 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-06-18 17:54:07 +00:00
|
|
|
corev1api "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
2019-09-30 21:26:56 +00:00
|
|
|
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
|
|
|
"github.com/vmware-tanzu/velero/pkg/builder"
|
|
|
|
"github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/fake"
|
|
|
|
informers "github.com/vmware-tanzu/velero/pkg/generated/informers/externalversions"
|
|
|
|
velerotest "github.com/vmware-tanzu/velero/pkg/test"
|
2018-06-18 17:54:07 +00:00
|
|
|
)
|
|
|
|
|
2019-08-27 22:49:23 +00:00
|
|
|
func TestGetVolumeBackupsForPod(t *testing.T) {
|
2018-06-18 17:54:07 +00:00
|
|
|
tests := []struct {
|
2019-08-27 22:49:23 +00:00
|
|
|
name string
|
|
|
|
podVolumeBackups []*velerov1api.PodVolumeBackup
|
2021-06-17 18:00:37 +00:00
|
|
|
podVolumes []corev1api.Volume
|
2019-08-27 22:49:23 +00:00
|
|
|
podAnnotations map[string]string
|
|
|
|
podName string
|
2021-02-22 19:16:00 +00:00
|
|
|
sourcePodNs string
|
2019-08-27 22:49:23 +00:00
|
|
|
expected map[string]string
|
2018-06-18 17:54:07 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-02-22 19:16:00 +00:00
|
|
|
name: "nil annotations results in no volume backups returned",
|
2019-08-27 22:49:23 +00:00
|
|
|
podAnnotations: nil,
|
|
|
|
expected: nil,
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
|
|
|
{
|
2021-02-22 19:16:00 +00:00
|
|
|
name: "empty annotations results in no volume backups returned",
|
2019-08-27 22:49:23 +00:00
|
|
|
podAnnotations: make(map[string]string),
|
|
|
|
expected: nil,
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
|
|
|
{
|
2021-02-22 19:16:00 +00:00
|
|
|
name: "pod annotations with no snapshot annotation prefix results in no volume backups returned",
|
2019-08-27 22:49:23 +00:00
|
|
|
podAnnotations: map[string]string{"foo": "bar"},
|
|
|
|
expected: nil,
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
|
|
|
{
|
2021-02-22 19:16:00 +00:00
|
|
|
name: "pod annotation with only snapshot annotation prefix, results in volume backup with empty volume key",
|
|
|
|
podAnnotations: map[string]string{podAnnotationPrefix: "snapshotID"},
|
|
|
|
expected: map[string]string{"": "snapshotID"},
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
|
|
|
{
|
2021-02-22 19:16:00 +00:00
|
|
|
name: "pod annotation with snapshot annotation prefix results in volume backup with volume name and snapshot ID",
|
|
|
|
podAnnotations: map[string]string{podAnnotationPrefix + "volume": "snapshotID"},
|
|
|
|
expected: map[string]string{"volume": "snapshotID"},
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
|
|
|
{
|
2021-02-22 19:16:00 +00:00
|
|
|
name: "only pod annotations with snapshot annotation prefix are considered",
|
|
|
|
podAnnotations: map[string]string{"x": "y", podAnnotationPrefix + "volume1": "snapshot1", podAnnotationPrefix + "volume2": "snapshot2"},
|
|
|
|
expected: map[string]string{"volume1": "snapshot1", "volume2": "snapshot2"},
|
2019-08-27 22:49:23 +00:00
|
|
|
},
|
|
|
|
{
|
2021-02-22 19:16:00 +00:00
|
|
|
name: "pod annotations are not considered if PVBs are provided",
|
2019-08-27 22:49:23 +00:00
|
|
|
podVolumeBackups: []*velerov1api.PodVolumeBackup{
|
2021-02-22 19:16:00 +00:00
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-1").PodName("TestPod").PodNamespace("TestNS").SnapshotID("snapshot1").Volume("pvbtest1-foo").Result(),
|
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-2").PodName("TestPod").PodNamespace("TestNS").SnapshotID("snapshot2").Volume("pvbtest2-abc").Result(),
|
2019-08-27 22:49:23 +00:00
|
|
|
},
|
|
|
|
podName: "TestPod",
|
2021-02-22 19:16:00 +00:00
|
|
|
sourcePodNs: "TestNS",
|
2019-08-27 22:49:23 +00:00
|
|
|
podAnnotations: map[string]string{"x": "y", podAnnotationPrefix + "foo": "bar", podAnnotationPrefix + "abc": "123"},
|
2021-02-22 19:16:00 +00:00
|
|
|
expected: map[string]string{"pvbtest1-foo": "snapshot1", "pvbtest2-abc": "snapshot2"},
|
2019-08-27 22:49:23 +00:00
|
|
|
},
|
|
|
|
{
|
2021-02-22 19:16:00 +00:00
|
|
|
name: "volume backups are returned even if no pod annotations are present",
|
2019-08-27 22:49:23 +00:00
|
|
|
podVolumeBackups: []*velerov1api.PodVolumeBackup{
|
2021-02-22 19:16:00 +00:00
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-1").PodName("TestPod").PodNamespace("TestNS").SnapshotID("snapshot1").Volume("pvbtest1-foo").Result(),
|
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-2").PodName("TestPod").PodNamespace("TestNS").SnapshotID("snapshot2").Volume("pvbtest2-abc").Result(),
|
2019-08-27 22:49:23 +00:00
|
|
|
},
|
2021-02-22 19:16:00 +00:00
|
|
|
podName: "TestPod",
|
|
|
|
sourcePodNs: "TestNS",
|
|
|
|
expected: map[string]string{"pvbtest1-foo": "snapshot1", "pvbtest2-abc": "snapshot2"},
|
2019-08-27 22:49:23 +00:00
|
|
|
},
|
2019-11-04 23:18:08 +00:00
|
|
|
{
|
2021-02-22 19:16:00 +00:00
|
|
|
name: "only volumes from PVBs with snapshot IDs are returned",
|
2019-11-04 23:18:08 +00:00
|
|
|
podVolumeBackups: []*velerov1api.PodVolumeBackup{
|
2021-02-22 19:16:00 +00:00
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-1").PodName("TestPod").PodNamespace("TestNS").SnapshotID("snapshot1").Volume("pvbtest1-foo").Result(),
|
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-2").PodName("TestPod").PodNamespace("TestNS").SnapshotID("snapshot2").Volume("pvbtest2-abc").Result(),
|
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-3").PodName("TestPod").PodNamespace("TestNS").Volume("pvbtest3-foo").Result(),
|
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-4").PodName("TestPod").PodNamespace("TestNS").Volume("pvbtest4-abc").Result(),
|
2019-11-04 23:18:08 +00:00
|
|
|
},
|
2021-02-22 19:16:00 +00:00
|
|
|
podName: "TestPod",
|
|
|
|
sourcePodNs: "TestNS",
|
|
|
|
expected: map[string]string{"pvbtest1-foo": "snapshot1", "pvbtest2-abc": "snapshot2"},
|
2019-11-04 23:18:08 +00:00
|
|
|
},
|
2019-08-27 22:49:23 +00:00
|
|
|
{
|
2021-02-22 19:16:00 +00:00
|
|
|
name: "only volumes from PVBs for the given pod are returned",
|
2019-08-27 22:49:23 +00:00
|
|
|
podVolumeBackups: []*velerov1api.PodVolumeBackup{
|
2021-02-22 19:16:00 +00:00
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-1").PodName("TestPod").PodNamespace("TestNS").SnapshotID("snapshot1").Volume("pvbtest1-foo").Result(),
|
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-2").PodName("TestPod").PodNamespace("TestNS").SnapshotID("snapshot2").Volume("pvbtest2-abc").Result(),
|
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-3").PodName("TestAnotherPod").SnapshotID("snapshot3").Volume("pvbtest3-xyz").Result(),
|
2019-08-27 22:49:23 +00:00
|
|
|
},
|
2021-02-22 19:16:00 +00:00
|
|
|
podName: "TestPod",
|
|
|
|
sourcePodNs: "TestNS",
|
|
|
|
expected: map[string]string{"pvbtest1-foo": "snapshot1", "pvbtest2-abc": "snapshot2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "only volumes from PVBs which match the pod name and source pod namespace are returned",
|
|
|
|
podVolumeBackups: []*velerov1api.PodVolumeBackup{
|
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-1").PodName("TestPod").PodNamespace("TestNS").SnapshotID("snapshot1").Volume("pvbtest1-foo").Result(),
|
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-2").PodName("TestAnotherPod").PodNamespace("TestNS").SnapshotID("snapshot2").Volume("pvbtest2-abc").Result(),
|
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-3").PodName("TestPod").PodNamespace("TestAnotherNS").SnapshotID("snapshot3").Volume("pvbtest3-xyz").Result(),
|
|
|
|
},
|
|
|
|
podName: "TestPod",
|
|
|
|
sourcePodNs: "TestNS",
|
|
|
|
expected: map[string]string{"pvbtest1-foo": "snapshot1"},
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
2021-06-17 18:00:37 +00:00
|
|
|
{
|
|
|
|
name: "volumes from PVBs that correspond to a pod volume from a projected source are not returned",
|
|
|
|
podVolumeBackups: []*velerov1api.PodVolumeBackup{
|
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-1").PodName("TestPod").PodNamespace("TestNS").SnapshotID("snapshot1").Volume("pvb-non-projected").Result(),
|
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-1").PodName("TestPod").PodNamespace("TestNS").SnapshotID("snapshot2").Volume("pvb-projected").Result(),
|
|
|
|
},
|
|
|
|
podVolumes: []corev1api.Volume{
|
|
|
|
{
|
|
|
|
Name: "pvb-non-projected",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
PersistentVolumeClaim: &corev1api.PersistentVolumeClaimVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pvb-projected",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
Projected: &corev1api.ProjectedVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
podName: "TestPod",
|
|
|
|
sourcePodNs: "TestNS",
|
|
|
|
expected: map[string]string{"pvb-non-projected": "snapshot1"},
|
|
|
|
},
|
2021-09-01 05:51:44 +00:00
|
|
|
{
|
|
|
|
name: "volumes from PVBs that correspond to a pod volume from a DownwardAPI source are not returned",
|
|
|
|
podVolumeBackups: []*velerov1api.PodVolumeBackup{
|
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-1").PodName("TestPod").PodNamespace("TestNS").SnapshotID("snapshot1").Volume("pvb-non-downwardapi").Result(),
|
|
|
|
builder.ForPodVolumeBackup("velero", "pvb-1").PodName("TestPod").PodNamespace("TestNS").SnapshotID("snapshot2").Volume("pvb-downwardapi").Result(),
|
|
|
|
},
|
|
|
|
podVolumes: []corev1api.Volume{
|
|
|
|
{
|
|
|
|
Name: "pvb-non-downwardapi",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
PersistentVolumeClaim: &corev1api.PersistentVolumeClaimVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pvb-downwardapi",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
DownwardAPI: &corev1api.DownwardAPIVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
podName: "TestPod",
|
|
|
|
sourcePodNs: "TestNS",
|
|
|
|
expected: map[string]string{"pvb-non-downwardapi": "snapshot1"},
|
|
|
|
},
|
2018-06-18 17:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
pod := &corev1api.Pod{}
|
2019-08-27 22:49:23 +00:00
|
|
|
pod.Annotations = test.podAnnotations
|
|
|
|
pod.Name = test.podName
|
2021-06-17 18:00:37 +00:00
|
|
|
pod.Spec.Volumes = test.podVolumes
|
2019-08-27 22:49:23 +00:00
|
|
|
|
2021-02-22 19:16:00 +00:00
|
|
|
res := GetVolumeBackupsForPod(test.podVolumeBackups, pod, test.sourcePodNs)
|
2019-08-27 22:49:23 +00:00
|
|
|
assert.Equal(t, test.expected, res)
|
2018-06-18 17:54:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetVolumesToBackup(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
annotations map[string]string
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "nil annotations",
|
|
|
|
annotations: nil,
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no volumes to backup",
|
|
|
|
annotations: map[string]string{"foo": "bar"},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "one volume to backup",
|
2020-02-19 19:37:40 +00:00
|
|
|
annotations: map[string]string{"foo": "bar", VolumesToBackupAnnotation: "volume-1"},
|
2018-06-18 17:54:07 +00:00
|
|
|
expected: []string{"volume-1"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple volumes to backup",
|
2020-02-19 19:37:40 +00:00
|
|
|
annotations: map[string]string{"foo": "bar", VolumesToBackupAnnotation: "volume-1,volume-2,volume-3"},
|
2018-06-18 17:54:07 +00:00
|
|
|
expected: []string{"volume-1", "volume-2", "volume-3"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
pod := &corev1api.Pod{}
|
|
|
|
pod.Annotations = test.annotations
|
|
|
|
|
|
|
|
res := GetVolumesToBackup(pod)
|
|
|
|
|
|
|
|
// sort to ensure good compare of slices
|
|
|
|
sort.Strings(test.expected)
|
|
|
|
sort.Strings(res)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSnapshotsInBackup(t *testing.T) {
|
|
|
|
tests := []struct {
|
2019-04-23 23:58:59 +00:00
|
|
|
name string
|
|
|
|
podVolumeBackups []velerov1api.PodVolumeBackup
|
|
|
|
expected []SnapshotIdentifier
|
|
|
|
longBackupNameEnabled bool
|
2018-06-18 17:54:07 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no pod volume backups",
|
|
|
|
podVolumeBackups: nil,
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no pod volume backups with matching label",
|
2019-01-25 03:33:07 +00:00
|
|
|
podVolumeBackups: []velerov1api.PodVolumeBackup{
|
2018-06-18 17:54:07 +00:00
|
|
|
{
|
2019-01-25 03:33:07 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "foo", Labels: map[string]string{velerov1api.BackupNameLabel: "non-matching-backup-1"}},
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-18 17:54:07 +00:00
|
|
|
Pod: corev1api.ObjectReference{Name: "pod-1", Namespace: "ns-1"},
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: "snap-1"},
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-25 03:33:07 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "bar", Labels: map[string]string{velerov1api.BackupNameLabel: "non-matching-backup-2"}},
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-18 17:54:07 +00:00
|
|
|
Pod: corev1api.ObjectReference{Name: "pod-2", Namespace: "ns-2"},
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: "snap-2"},
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "some pod volume backups with matching label",
|
2019-01-25 03:33:07 +00:00
|
|
|
podVolumeBackups: []velerov1api.PodVolumeBackup{
|
2018-06-18 17:54:07 +00:00
|
|
|
{
|
2019-01-25 03:33:07 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "foo", Labels: map[string]string{velerov1api.BackupNameLabel: "non-matching-backup-1"}},
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-18 17:54:07 +00:00
|
|
|
Pod: corev1api.ObjectReference{Name: "pod-1", Namespace: "ns-1"},
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: "snap-1"},
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-25 03:33:07 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "bar", Labels: map[string]string{velerov1api.BackupNameLabel: "non-matching-backup-2"}},
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-18 17:54:07 +00:00
|
|
|
Pod: corev1api.ObjectReference{Name: "pod-2", Namespace: "ns-2"},
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: "snap-2"},
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-25 03:33:07 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "completed-pvb", Labels: map[string]string{velerov1api.BackupNameLabel: "backup-1"}},
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-18 17:54:07 +00:00
|
|
|
Pod: corev1api.ObjectReference{Name: "pod-1", Namespace: "ns-1"},
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: "snap-3"},
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-25 03:33:07 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "completed-pvb-2", Labels: map[string]string{velerov1api.BackupNameLabel: "backup-1"}},
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-18 17:54:07 +00:00
|
|
|
Pod: corev1api.ObjectReference{Name: "pod-1", Namespace: "ns-1"},
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: "snap-4"},
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-25 03:33:07 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "incomplete-or-failed-pvb", Labels: map[string]string{velerov1api.BackupNameLabel: "backup-1"}},
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-18 17:54:07 +00:00
|
|
|
Pod: corev1api.ObjectReference{Name: "pod-1", Namespace: "ns-2"},
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: ""},
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []SnapshotIdentifier{
|
|
|
|
{
|
2018-09-25 20:20:58 +00:00
|
|
|
VolumeNamespace: "ns-1",
|
|
|
|
SnapshotID: "snap-3",
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
|
|
|
{
|
2018-09-25 20:20:58 +00:00
|
|
|
VolumeNamespace: "ns-1",
|
|
|
|
SnapshotID: "snap-4",
|
2018-06-18 17:54:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-04-23 23:58:59 +00:00
|
|
|
{
|
|
|
|
name: "some pod volume backups with matching label and backup name greater than 63 chars",
|
|
|
|
longBackupNameEnabled: true,
|
|
|
|
podVolumeBackups: []velerov1api.PodVolumeBackup{
|
|
|
|
{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "foo", Labels: map[string]string{velerov1api.BackupNameLabel: "non-matching-backup-1"}},
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
|
|
|
Pod: corev1api.ObjectReference{Name: "pod-1", Namespace: "ns-1"},
|
|
|
|
},
|
|
|
|
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: "snap-1"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "bar", Labels: map[string]string{velerov1api.BackupNameLabel: "non-matching-backup-2"}},
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
|
|
|
Pod: corev1api.ObjectReference{Name: "pod-2", Namespace: "ns-2"},
|
|
|
|
},
|
|
|
|
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: "snap-2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "completed-pvb", Labels: map[string]string{velerov1api.BackupNameLabel: "the-really-long-backup-name-that-is-much-more-than-63-cha6ca4bc"}},
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
|
|
|
Pod: corev1api.ObjectReference{Name: "pod-1", Namespace: "ns-1"},
|
|
|
|
},
|
|
|
|
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: "snap-3"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "completed-pvb-2", Labels: map[string]string{velerov1api.BackupNameLabel: "backup-1"}},
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
|
|
|
Pod: corev1api.ObjectReference{Name: "pod-1", Namespace: "ns-1"},
|
|
|
|
},
|
|
|
|
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: "snap-4"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "incomplete-or-failed-pvb", Labels: map[string]string{velerov1api.BackupNameLabel: "backup-1"}},
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
|
|
|
Pod: corev1api.ObjectReference{Name: "pod-1", Namespace: "ns-2"},
|
|
|
|
},
|
|
|
|
Status: velerov1api.PodVolumeBackupStatus{SnapshotID: ""},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []SnapshotIdentifier{
|
|
|
|
{
|
|
|
|
VolumeNamespace: "ns-1",
|
|
|
|
SnapshotID: "snap-3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-06-18 17:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
client = fake.NewSimpleClientset()
|
|
|
|
sharedInformers = informers.NewSharedInformerFactory(client, 0)
|
2019-01-25 03:33:07 +00:00
|
|
|
pvbInformer = sharedInformers.Velero().V1().PodVolumeBackups()
|
|
|
|
veleroBackup = &velerov1api.Backup{}
|
2018-06-18 17:54:07 +00:00
|
|
|
)
|
|
|
|
|
2019-01-25 03:33:07 +00:00
|
|
|
veleroBackup.Name = "backup-1"
|
2018-06-18 17:54:07 +00:00
|
|
|
|
2019-04-23 23:58:59 +00:00
|
|
|
if test.longBackupNameEnabled {
|
|
|
|
veleroBackup.Name = "the-really-long-backup-name-that-is-much-more-than-63-characters"
|
|
|
|
}
|
|
|
|
|
2018-06-18 17:54:07 +00:00
|
|
|
for _, pvb := range test.podVolumeBackups {
|
|
|
|
require.NoError(t, pvbInformer.Informer().GetStore().Add(pvb.DeepCopy()))
|
|
|
|
}
|
|
|
|
|
2019-01-25 03:33:07 +00:00
|
|
|
res, err := GetSnapshotsInBackup(veleroBackup, pvbInformer.Lister())
|
2018-06-18 17:54:07 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// sort to ensure good compare of slices
|
|
|
|
less := func(snapshots []SnapshotIdentifier) func(i, j int) bool {
|
|
|
|
return func(i, j int) bool {
|
2018-09-25 20:20:58 +00:00
|
|
|
if snapshots[i].VolumeNamespace == snapshots[j].VolumeNamespace {
|
2018-07-05 20:08:05 +00:00
|
|
|
return snapshots[i].SnapshotID < snapshots[j].SnapshotID
|
|
|
|
}
|
2018-09-25 20:20:58 +00:00
|
|
|
return snapshots[i].VolumeNamespace < snapshots[j].VolumeNamespace
|
2018-06-18 17:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-07-05 20:08:05 +00:00
|
|
|
|
2018-06-18 17:54:07 +00:00
|
|
|
sort.Slice(test.expected, less(test.expected))
|
|
|
|
sort.Slice(res, less(res))
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 21:50:48 +00:00
|
|
|
func TestTempCACertFile(t *testing.T) {
|
|
|
|
var (
|
Use Credential from BSL for restic commands (#3489)
* Use Credential from BSL for restic commands
This change introduces support for restic to make use of per-BSL
credentials. It makes use of the `credentials.FileStore` introduced in
PR #3442 to write the BSL credentials to disk. To support per-BSL
credentials for restic, the environment for the restic commands needs to
be modified for each provider to ensure that the credentials are
provided via the correct provider specific environment variables.
This change introduces a new function `restic.CmdEnv` to check the BSL
provider and create the correct mapping of environment variables for
each provider.
Previously, AWS and GCP could rely on the environment variables in the
Velero deployments to obtain the credentials file, but now these
environment variables need to be set with the path to the serialized
credentials file if a credential is set on the BSL.
For Azure, the credentials file in the environment was loaded and parsed
to set the environment variables for restic. Now, we check if the BSL
has a credential, and if it does, load and parse that file instead.
This change also introduces a few other small improvements. Now that we
are fetching the BSL to check for the `Credential` field, we can use the
BSL directly to get the `CACert` which means that we can remove the
`GetCACert` function. Also, now that we have a way to serialize secrets
to disk, we can use the `credentials.FileStore` to get a temp file for
the restic repo password and remove the `restic.TempCredentialsFile`
function.
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Add documentation for per-BSL credentials
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Address review feedback
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Address review comments
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
2021-03-11 18:10:51 +00:00
|
|
|
fs = velerotest.NewFakeFileSystem()
|
|
|
|
caCertData = []byte("cacert")
|
2020-03-24 21:50:48 +00:00
|
|
|
)
|
|
|
|
|
Use Credential from BSL for restic commands (#3489)
* Use Credential from BSL for restic commands
This change introduces support for restic to make use of per-BSL
credentials. It makes use of the `credentials.FileStore` introduced in
PR #3442 to write the BSL credentials to disk. To support per-BSL
credentials for restic, the environment for the restic commands needs to
be modified for each provider to ensure that the credentials are
provided via the correct provider specific environment variables.
This change introduces a new function `restic.CmdEnv` to check the BSL
provider and create the correct mapping of environment variables for
each provider.
Previously, AWS and GCP could rely on the environment variables in the
Velero deployments to obtain the credentials file, but now these
environment variables need to be set with the path to the serialized
credentials file if a credential is set on the BSL.
For Azure, the credentials file in the environment was loaded and parsed
to set the environment variables for restic. Now, we check if the BSL
has a credential, and if it does, load and parse that file instead.
This change also introduces a few other small improvements. Now that we
are fetching the BSL to check for the `Credential` field, we can use the
BSL directly to get the `CACert` which means that we can remove the
`GetCACert` function. Also, now that we have a way to serialize secrets
to disk, we can use the `credentials.FileStore` to get a temp file for
the restic repo password and remove the `restic.TempCredentialsFile`
function.
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Add documentation for per-BSL credentials
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Address review feedback
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Address review comments
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
2021-03-11 18:10:51 +00:00
|
|
|
fileName, err := TempCACertFile(caCertData, "default", fs)
|
2020-03-24 21:50:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
contents, err := fs.ReadFile(fileName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
Use Credential from BSL for restic commands (#3489)
* Use Credential from BSL for restic commands
This change introduces support for restic to make use of per-BSL
credentials. It makes use of the `credentials.FileStore` introduced in
PR #3442 to write the BSL credentials to disk. To support per-BSL
credentials for restic, the environment for the restic commands needs to
be modified for each provider to ensure that the credentials are
provided via the correct provider specific environment variables.
This change introduces a new function `restic.CmdEnv` to check the BSL
provider and create the correct mapping of environment variables for
each provider.
Previously, AWS and GCP could rely on the environment variables in the
Velero deployments to obtain the credentials file, but now these
environment variables need to be set with the path to the serialized
credentials file if a credential is set on the BSL.
For Azure, the credentials file in the environment was loaded and parsed
to set the environment variables for restic. Now, we check if the BSL
has a credential, and if it does, load and parse that file instead.
This change also introduces a few other small improvements. Now that we
are fetching the BSL to check for the `Credential` field, we can use the
BSL directly to get the `CACert` which means that we can remove the
`GetCACert` function. Also, now that we have a way to serialize secrets
to disk, we can use the `credentials.FileStore` to get a temp file for
the restic repo password and remove the `restic.TempCredentialsFile`
function.
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Add documentation for per-BSL credentials
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Address review feedback
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Address review comments
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
2021-03-11 18:10:51 +00:00
|
|
|
assert.Equal(t, string(caCertData), string(contents))
|
2020-03-24 21:50:48 +00:00
|
|
|
|
|
|
|
os.Remove(fileName)
|
|
|
|
}
|
2020-06-04 00:15:59 +00:00
|
|
|
|
|
|
|
func TestGetPodVolumesUsingRestic(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2020-06-15 22:26:44 +00:00
|
|
|
name string
|
|
|
|
pod *corev1api.Pod
|
|
|
|
expected []string
|
|
|
|
defaultVolumesToRestic bool
|
2020-06-04 00:15:59 +00:00
|
|
|
}{
|
|
|
|
{
|
2020-06-15 22:26:44 +00:00
|
|
|
name: "should get PVs from VolumesToBackupAnnotation when defaultVolumesToRestic is false",
|
|
|
|
defaultVolumesToRestic: false,
|
2020-06-04 00:15:59 +00:00
|
|
|
pod: &corev1api.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Annotations: map[string]string{
|
|
|
|
VolumesToBackupAnnotation: "resticPV1,resticPV2,resticPV3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []string{"resticPV1", "resticPV2", "resticPV3"},
|
|
|
|
},
|
|
|
|
{
|
2020-06-15 22:26:44 +00:00
|
|
|
name: "should get all pod volumes when defaultVolumesToRestic is true and no PVs are excluded",
|
|
|
|
defaultVolumesToRestic: true,
|
2020-06-04 00:15:59 +00:00
|
|
|
pod: &corev1api.Pod{
|
|
|
|
Spec: corev1api.PodSpec{
|
|
|
|
Volumes: []corev1api.Volume{
|
|
|
|
// Restic Volumes
|
|
|
|
{Name: "resticPV1"}, {Name: "resticPV2"}, {Name: "resticPV3"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []string{"resticPV1", "resticPV2", "resticPV3"},
|
|
|
|
},
|
|
|
|
{
|
2020-06-15 22:26:44 +00:00
|
|
|
name: "should get all pod volumes except ones excluded when defaultVolumesToRestic is true",
|
|
|
|
defaultVolumesToRestic: true,
|
2020-06-04 00:15:59 +00:00
|
|
|
pod: &corev1api.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Annotations: map[string]string{
|
|
|
|
VolumesToExcludeAnnotation: "nonResticPV1,nonResticPV2,nonResticPV3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: corev1api.PodSpec{
|
|
|
|
Volumes: []corev1api.Volume{
|
|
|
|
// Restic Volumes
|
|
|
|
{Name: "resticPV1"}, {Name: "resticPV2"}, {Name: "resticPV3"},
|
|
|
|
/// Excluded from restic through annotation
|
|
|
|
{Name: "nonResticPV1"}, {Name: "nonResticPV2"}, {Name: "nonResticPV3"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []string{"resticPV1", "resticPV2", "resticPV3"},
|
|
|
|
},
|
|
|
|
{
|
2020-06-15 22:26:44 +00:00
|
|
|
name: "should exclude default service account token from restic backup",
|
|
|
|
defaultVolumesToRestic: true,
|
2020-06-04 00:15:59 +00:00
|
|
|
pod: &corev1api.Pod{
|
|
|
|
Spec: corev1api.PodSpec{
|
|
|
|
Volumes: []corev1api.Volume{
|
|
|
|
// Restic Volumes
|
|
|
|
{Name: "resticPV1"}, {Name: "resticPV2"}, {Name: "resticPV3"},
|
|
|
|
/// Excluded from restic because colume mounting default service account token
|
|
|
|
{Name: "default-token-5xq45"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []string{"resticPV1", "resticPV2", "resticPV3"},
|
|
|
|
},
|
|
|
|
{
|
2020-06-15 22:26:44 +00:00
|
|
|
name: "should exclude host path volumes from restic backups",
|
|
|
|
defaultVolumesToRestic: true,
|
2020-06-04 00:15:59 +00:00
|
|
|
pod: &corev1api.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Annotations: map[string]string{
|
|
|
|
VolumesToExcludeAnnotation: "nonResticPV1,nonResticPV2,nonResticPV3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: corev1api.PodSpec{
|
|
|
|
Volumes: []corev1api.Volume{
|
|
|
|
// Restic Volumes
|
|
|
|
{Name: "resticPV1"}, {Name: "resticPV2"}, {Name: "resticPV3"},
|
|
|
|
/// Excluded from restic through annotation
|
|
|
|
{Name: "nonResticPV1"}, {Name: "nonResticPV2"}, {Name: "nonResticPV3"},
|
|
|
|
// Excluded from restic because hostpath
|
|
|
|
{Name: "hostPath1", VolumeSource: corev1api.VolumeSource{HostPath: &corev1api.HostPathVolumeSource{Path: "/hostpathVol"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []string{"resticPV1", "resticPV2", "resticPV3"},
|
|
|
|
},
|
2020-07-28 03:27:49 +00:00
|
|
|
{
|
|
|
|
name: "should exclude volumes mounting secrets",
|
|
|
|
defaultVolumesToRestic: true,
|
|
|
|
pod: &corev1api.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Annotations: map[string]string{
|
|
|
|
VolumesToExcludeAnnotation: "nonResticPV1,nonResticPV2,nonResticPV3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: corev1api.PodSpec{
|
|
|
|
Volumes: []corev1api.Volume{
|
|
|
|
// Restic Volumes
|
|
|
|
{Name: "resticPV1"}, {Name: "resticPV2"}, {Name: "resticPV3"},
|
|
|
|
/// Excluded from restic through annotation
|
|
|
|
{Name: "nonResticPV1"}, {Name: "nonResticPV2"}, {Name: "nonResticPV3"},
|
|
|
|
// Excluded from restic because hostpath
|
|
|
|
{Name: "superSecret", VolumeSource: corev1api.VolumeSource{Secret: &corev1api.SecretVolumeSource{SecretName: "super-secret"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []string{"resticPV1", "resticPV2", "resticPV3"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "should exclude volumes mounting config maps",
|
|
|
|
defaultVolumesToRestic: true,
|
|
|
|
pod: &corev1api.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Annotations: map[string]string{
|
|
|
|
VolumesToExcludeAnnotation: "nonResticPV1,nonResticPV2,nonResticPV3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: corev1api.PodSpec{
|
|
|
|
Volumes: []corev1api.Volume{
|
|
|
|
// Restic Volumes
|
|
|
|
{Name: "resticPV1"}, {Name: "resticPV2"}, {Name: "resticPV3"},
|
|
|
|
/// Excluded from restic through annotation
|
|
|
|
{Name: "nonResticPV1"}, {Name: "nonResticPV2"}, {Name: "nonResticPV3"},
|
|
|
|
// Excluded from restic because hostpath
|
|
|
|
{Name: "appCOnfig", VolumeSource: corev1api.VolumeSource{ConfigMap: &corev1api.ConfigMapVolumeSource{LocalObjectReference: corev1api.LocalObjectReference{Name: "app-config"}}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []string{"resticPV1", "resticPV2", "resticPV3"},
|
|
|
|
},
|
2021-06-10 07:39:39 +00:00
|
|
|
{
|
|
|
|
name: "should exclude projected volumes",
|
|
|
|
defaultVolumesToRestic: true,
|
|
|
|
pod: &corev1api.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Annotations: map[string]string{
|
|
|
|
VolumesToExcludeAnnotation: "nonResticPV1,nonResticPV2,nonResticPV3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: corev1api.PodSpec{
|
|
|
|
Volumes: []corev1api.Volume{
|
|
|
|
{Name: "resticPV1"}, {Name: "resticPV2"}, {Name: "resticPV3"},
|
|
|
|
{
|
|
|
|
Name: "projected",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
Projected: &corev1api.ProjectedVolumeSource{
|
|
|
|
Sources: []corev1api.VolumeProjection{{
|
|
|
|
Secret: &corev1api.SecretProjection{
|
|
|
|
LocalObjectReference: corev1api.LocalObjectReference{},
|
|
|
|
Items: nil,
|
|
|
|
Optional: nil,
|
|
|
|
},
|
|
|
|
DownwardAPI: nil,
|
|
|
|
ConfigMap: nil,
|
|
|
|
ServiceAccountToken: nil,
|
|
|
|
}},
|
|
|
|
DefaultMode: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []string{"resticPV1", "resticPV2", "resticPV3"},
|
|
|
|
},
|
2021-09-01 05:51:44 +00:00
|
|
|
{
|
|
|
|
name: "should exclude DownwardAPI volumes",
|
|
|
|
defaultVolumesToRestic: true,
|
|
|
|
pod: &corev1api.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Annotations: map[string]string{
|
|
|
|
VolumesToExcludeAnnotation: "nonResticPV1,nonResticPV2,nonResticPV3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: corev1api.PodSpec{
|
|
|
|
Volumes: []corev1api.Volume{
|
|
|
|
{Name: "resticPV1"}, {Name: "resticPV2"}, {Name: "resticPV3"},
|
|
|
|
{
|
|
|
|
Name: "downwardAPI",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
DownwardAPI: &corev1api.DownwardAPIVolumeSource{
|
|
|
|
Items: []corev1api.DownwardAPIVolumeFile{
|
|
|
|
{
|
|
|
|
Path: "labels",
|
|
|
|
FieldRef: &corev1api.ObjectFieldSelector{
|
|
|
|
APIVersion: "v1",
|
|
|
|
FieldPath: "metadata.labels",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []string{"resticPV1", "resticPV2", "resticPV3"},
|
|
|
|
},
|
2020-06-04 00:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2020-06-15 22:26:44 +00:00
|
|
|
actual := GetPodVolumesUsingRestic(tc.pod, tc.defaultVolumesToRestic)
|
2020-06-04 00:15:59 +00:00
|
|
|
|
|
|
|
sort.Strings(tc.expected)
|
|
|
|
sort.Strings(actual)
|
|
|
|
assert.Equal(t, tc.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 16:55:18 +00:00
|
|
|
|
2020-11-10 16:36:49 +00:00
|
|
|
func TestIsPVBMatchPod(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2021-02-22 19:16:00 +00:00
|
|
|
name string
|
|
|
|
pvb velerov1api.PodVolumeBackup
|
|
|
|
podName string
|
|
|
|
sourcePodNs string
|
|
|
|
expected bool
|
2020-11-10 16:36:49 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "should match PVB and pod",
|
|
|
|
pvb: velerov1api.PodVolumeBackup{
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
|
|
|
Pod: corev1api.ObjectReference{
|
|
|
|
Name: "matching-pod",
|
|
|
|
Namespace: "matching-namespace",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-22 19:16:00 +00:00
|
|
|
podName: "matching-pod",
|
|
|
|
sourcePodNs: "matching-namespace",
|
|
|
|
expected: true,
|
2020-11-10 16:36:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "should not match PVB and pod, pod name mismatch",
|
|
|
|
pvb: velerov1api.PodVolumeBackup{
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
|
|
|
Pod: corev1api.ObjectReference{
|
|
|
|
Name: "matching-pod",
|
|
|
|
Namespace: "matching-namespace",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-22 19:16:00 +00:00
|
|
|
podName: "not-matching-pod",
|
|
|
|
sourcePodNs: "matching-namespace",
|
|
|
|
expected: false,
|
2020-11-10 16:36:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "should not match PVB and pod, pod namespace mismatch",
|
|
|
|
pvb: velerov1api.PodVolumeBackup{
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
|
|
|
Pod: corev1api.ObjectReference{
|
|
|
|
Name: "matching-pod",
|
|
|
|
Namespace: "matching-namespace",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-22 19:16:00 +00:00
|
|
|
podName: "matching-pod",
|
|
|
|
sourcePodNs: "not-matching-namespace",
|
|
|
|
expected: false,
|
2020-11-10 16:36:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "should not match PVB and pod, pod name and namespace mismatch",
|
|
|
|
pvb: velerov1api.PodVolumeBackup{
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
|
|
|
Pod: corev1api.ObjectReference{
|
|
|
|
Name: "matching-pod",
|
|
|
|
Namespace: "matching-namespace",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-22 19:16:00 +00:00
|
|
|
podName: "not-matching-pod",
|
|
|
|
sourcePodNs: "not-matching-namespace",
|
|
|
|
expected: false,
|
2020-11-10 16:36:49 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-02-22 19:16:00 +00:00
|
|
|
actual := isPVBMatchPod(&tc.pvb, tc.podName, tc.sourcePodNs)
|
2020-11-10 16:36:49 +00:00
|
|
|
assert.Equal(t, tc.expected, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 18:00:37 +00:00
|
|
|
|
2021-09-01 05:51:44 +00:00
|
|
|
func TestVolumeHasNonRestorableSource(t *testing.T) {
|
2021-06-17 18:00:37 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
volumeName string
|
|
|
|
podVolumes []corev1api.Volume
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "volume name not in list of volumes",
|
|
|
|
volumeName: "missing-volume",
|
|
|
|
podVolumes: []corev1api.Volume{
|
|
|
|
{
|
2021-09-01 05:51:44 +00:00
|
|
|
Name: "restorable",
|
2021-06-17 18:00:37 +00:00
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
PersistentVolumeClaim: &corev1api.PersistentVolumeClaimVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "projected",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
Projected: &corev1api.ProjectedVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
2021-09-01 05:51:44 +00:00
|
|
|
{
|
|
|
|
Name: "downwardapi",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
DownwardAPI: &corev1api.DownwardAPIVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
2021-06-17 18:00:37 +00:00
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
2021-09-01 05:51:44 +00:00
|
|
|
name: "volume name in list of volumes but not projected or DownwardAPI",
|
|
|
|
volumeName: "restorable",
|
2021-06-17 18:00:37 +00:00
|
|
|
podVolumes: []corev1api.Volume{
|
|
|
|
{
|
2021-09-01 05:51:44 +00:00
|
|
|
Name: "restorable",
|
2021-06-17 18:00:37 +00:00
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
PersistentVolumeClaim: &corev1api.PersistentVolumeClaimVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "projected",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
Projected: &corev1api.ProjectedVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
2021-09-01 05:51:44 +00:00
|
|
|
{
|
|
|
|
Name: "downwardapi",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
DownwardAPI: &corev1api.DownwardAPIVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
2021-06-17 18:00:37 +00:00
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "volume name in list of volumes and projected",
|
|
|
|
volumeName: "projected",
|
|
|
|
podVolumes: []corev1api.Volume{
|
|
|
|
{
|
2021-09-01 05:51:44 +00:00
|
|
|
Name: "restorable",
|
2021-06-17 18:00:37 +00:00
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
PersistentVolumeClaim: &corev1api.PersistentVolumeClaimVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "projected",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
Projected: &corev1api.ProjectedVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
2021-09-01 05:51:44 +00:00
|
|
|
{
|
|
|
|
Name: "downwardapi",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
DownwardAPI: &corev1api.DownwardAPIVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "volume name in list of volumes and is a DownwardAPI volume",
|
|
|
|
volumeName: "downwardapi",
|
|
|
|
podVolumes: []corev1api.Volume{
|
|
|
|
{
|
|
|
|
Name: "restorable",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
PersistentVolumeClaim: &corev1api.PersistentVolumeClaimVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "projected",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
Projected: &corev1api.ProjectedVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "downwardapi",
|
|
|
|
VolumeSource: corev1api.VolumeSource{
|
|
|
|
DownwardAPI: &corev1api.DownwardAPIVolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
2021-06-17 18:00:37 +00:00
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-09-01 05:51:44 +00:00
|
|
|
actual := volumeHasNonRestorableSource(tc.volumeName, tc.podVolumes)
|
2021-06-17 18:00:37 +00:00
|
|
|
assert.Equal(t, tc.expected, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|