allow exclusion of resources using standard label (#1588)

* allow exclusion of resources using standard label

excludes any resources with the velero.io/exclude-from-backup=true label

Signed-off-by: Adnan Abdulhussein <aadnan@vmware.com>
This commit is contained in:
Adnan Abdulhussein
2019-06-19 10:58:02 -04:00
committed by Nolan Brubaker
parent a2e88c4d3f
commit 1f4139a5bf
3 changed files with 85 additions and 4 deletions
+1
View File
@@ -0,0 +1 @@
allows excluding resources from backups with the velero.io/exclude-from-backup=true label
+82 -2
View File
@@ -218,6 +218,81 @@ func TestBackupResourceFiltering(t *testing.T) {
"resources/persistentvolumes/cluster/bar.json",
},
},
{
name: "resources with velero.io/exclude-from-backup=true label are not included",
backup: defaultBackup().
Backup(),
apiResources: []*apiResource{
pods(
withLabel(newPod("foo", "bar"), "velero.io/exclude-from-backup", "true"),
newPod("zoo", "raz"),
),
deployments(
newDeployment("foo", "bar"),
withLabel(newDeployment("zoo", "raz"), "velero.io/exclude-from-backup", "true"),
),
pvs(
withLabel(newPV("bar"), "a", "b"),
withLabel(newPV("baz"), "velero.io/exclude-from-backup", "true"),
),
},
want: []string{
"resources/pods/namespaces/zoo/raz.json",
"resources/deployments.apps/namespaces/foo/bar.json",
"resources/persistentvolumes/cluster/bar.json",
},
},
{
name: "resources with velero.io/exclude-from-backup=true label are not included even if matching label selector",
backup: defaultBackup().
LabelSelector(&metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}}).
Backup(),
apiResources: []*apiResource{
pods(
withLabel(newPod("foo", "bar"), "velero.io/exclude-from-backup", "true", "a", "b"),
withLabel(newPod("zoo", "raz"), "a", "b"),
),
deployments(
newDeployment("foo", "bar"),
withLabel(newDeployment("zoo", "raz"), "velero.io/exclude-from-backup", "true", "a", "b"),
),
pvs(
withLabel(newPV("bar"), "a", "b"),
withLabel(newPV("baz"), "a", "b", "velero.io/exclude-from-backup", "true"),
),
},
want: []string{
"resources/pods/namespaces/zoo/raz.json",
"resources/persistentvolumes/cluster/bar.json",
},
},
{
name: "resources with velero.io/exclude-from-backup label specified but not 'true' are included",
backup: defaultBackup().
Backup(),
apiResources: []*apiResource{
pods(
withLabel(newPod("foo", "bar"), "velero.io/exclude-from-backup", "false"),
newPod("zoo", "raz"),
),
deployments(
newDeployment("foo", "bar"),
withLabel(newDeployment("zoo", "raz"), "velero.io/exclude-from-backup", "1"),
),
pvs(
withLabel(newPV("bar"), "a", "b"),
withLabel(newPV("baz"), "velero.io/exclude-from-backup", ""),
),
},
want: []string{
"resources/pods/namespaces/foo/bar.json",
"resources/pods/namespaces/zoo/raz.json",
"resources/deployments.apps/namespaces/foo/bar.json",
"resources/deployments.apps/namespaces/zoo/raz.json",
"resources/persistentvolumes/cluster/bar.json",
"resources/persistentvolumes/cluster/baz.json",
},
},
{
name: "should include cluster-scoped resources if backing up subset of namespaces and IncludeClusterResources=true",
backup: defaultBackup().
@@ -1794,12 +1869,17 @@ func newHarness(t *testing.T) *harness {
}
}
func withLabel(obj metav1.Object, key, val string) metav1.Object {
func withLabel(obj metav1.Object, labelPairs ...string) metav1.Object {
if len(labelPairs)%2 != 0 {
panic("withLabel requires a series of key-value pairs")
}
labels := obj.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[key] = val
for i := 0; i < len(labelPairs); i += 2 {
labels[labelPairs[i]] = labelPairs[i+1]
}
obj.SetLabels(labels)
return obj
+2 -2
View File
@@ -221,9 +221,9 @@ func (rb *defaultResourceBackupper) backupResource(group *metav1.APIResourceList
continue
}
var labelSelector string
labelSelector := "velero.io/exclude-from-backup!=true"
if selector := rb.backupRequest.Spec.LabelSelector; selector != nil {
labelSelector = metav1.FormatLabelSelector(selector)
labelSelector = labelSelector + "," + metav1.FormatLabelSelector(selector)
}
log.Info("Listing items")