fix excluding additional items with the exclude-from-backup label (#1843)

* fix excluding additional items with the exclude-from-backup label

Signed-off-by: Adnan Abdulhussein <aadnan@vmware.com>
pull/1866/head
Adnan Abdulhussein 2019-09-10 11:45:55 -07:00 committed by KubeKween
parent 1e182e5837
commit 26e06dae53
4 changed files with 38 additions and 2 deletions

View File

@ -0,0 +1 @@
fix excluding additional items with the velero.io/exclude-from-backup=true label

View File

@ -1329,6 +1329,36 @@ func TestBackupActionAdditionalItems(t *testing.T) {
"resources/pods/namespaces/ns-2/pod-2.json",
},
},
{
name: "additional items with the velero.io/exclude-from-backup label are not backed up",
backup: defaultBackup().IncludedNamespaces("ns-1").Result(),
apiResources: []*test.APIResource{
test.Pods(
builder.ForPod("ns-1", "pod-1").Result(),
),
test.PVs(
builder.ForPersistentVolume("pv-1").ObjectMeta(builder.WithLabels("velero.io/exclude-from-backup", "true")).Result(),
builder.ForPersistentVolume("pv-2").Result(),
),
},
actions: []velero.BackupItemAction{
&pluggableAction{
executeFunc: func(item runtime.Unstructured, backup *velerov1.Backup) (runtime.Unstructured, []velero.ResourceIdentifier, error) {
additionalItems := []velero.ResourceIdentifier{
{GroupResource: kuberesource.PersistentVolumes, Name: "pv-1"},
{GroupResource: kuberesource.PersistentVolumes, Name: "pv-2"},
}
return item, additionalItems, nil
},
},
},
want: []string{
"resources/pods/namespaces/ns-1/pod-1.json",
"resources/persistentvolumes/cluster/pv-2.json",
},
},
{
name: "if there's an error backing up additional items, the item the action was run for isn't backed up",
backup: defaultBackup().Result(),

View File

@ -123,6 +123,11 @@ func (ib *defaultItemBackupper) backupItem(logger logrus.FieldLogger, obj runtim
log = log.WithField("namespace", namespace)
}
if metadata.GetLabels()["velero.io/exclude-from-backup"] == "true" {
log.Info("Excluding item because it has label velero.io/exclude-from-backup=true")
return nil
}
// NOTE: we have to re-check namespace & resource includes/excludes because it's possible that
// backupItem can be invoked by a custom action.
if namespace != "" && !ib.backupRequest.NamespaceIncludesExcludes.ShouldInclude(namespace) {

View File

@ -216,9 +216,9 @@ func (rb *defaultResourceBackupper) backupResource(group *metav1.APIResourceList
continue
}
labelSelector := "velero.io/exclude-from-backup!=true"
var labelSelector string
if selector := rb.backupRequest.Spec.LabelSelector; selector != nil {
labelSelector = labelSelector + "," + metav1.FormatLabelSelector(selector)
labelSelector = metav1.FormatLabelSelector(selector)
}
log.Info("Listing items")