add backup name label to restored objects

Signed-off-by: Steve Kriss <steve@heptio.com>
pull/754/head
Steve Kriss 2018-08-08 16:51:33 -07:00
parent 0c3ac67b6d
commit 95b2b90006
2 changed files with 19 additions and 14 deletions

View File

@ -760,9 +760,10 @@ func (ctx *context) restoreResource(resource, namespace, resourcePath string) (a
obj.SetNamespace(namespace)
}
// label the resource with the restore's name for easy identification
// of all cluster resources created by this restore
addRestoreLabel(obj, ctx.restore.Name)
// label the resource with the restore's name and the restored backup's name
// for easy identification of all cluster resources created by this restore
// and which backup they came from
addRestoreLabels(obj, ctx.restore.Name, ctx.restore.Spec.BackupName)
ctx.infof("Restoring %s: %v", obj.GroupVersionKind().Kind, name)
createdObj, restoreErr := resourceClient.Create(obj)
@ -781,10 +782,10 @@ func (ctx *context) restoreResource(resource, namespace, resourcePath string) (a
continue
}
// We know the cluster won't have the restore name label, so
// copy it over from the backup
restoreName := obj.GetLabels()[api.RestoreNameLabel]
addRestoreLabel(fromCluster, restoreName)
// We know the object from the cluster won't have the backup/restore name labels, so
// copy them from the object we attempted to restore.
labels := obj.GetLabels()
addRestoreLabels(fromCluster, labels[api.RestoreNameLabel], labels[api.BackupNameLabel])
if !equality.Semantic.DeepEqual(fromCluster, obj) {
switch groupResource {
@ -998,14 +999,16 @@ func resetMetadataAndStatus(obj *unstructured.Unstructured) (*unstructured.Unstr
return obj, nil
}
// addRestoreLabel applies the specified key/value to an object as a label.
func addRestoreLabel(obj metav1.Object, restoreName string) {
// addRestoreLabels labels the provided object with the restore name and
// the restored backup's name.
func addRestoreLabels(obj metav1.Object, restoreName, backupName string) {
labels := obj.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[api.BackupNameLabel] = backupName
labels[api.RestoreNameLabel] = restoreName
// TODO(1.0): remove the below line, and remove the `RestoreLabelKey`

View File

@ -312,7 +312,7 @@ func TestNamespaceRemapping(t *testing.T) {
resourceClient := &arktest.FakeDynamicClient{}
for i := range expectedObjs {
addRestoreLabel(&expectedObjs[i], "")
addRestoreLabels(&expectedObjs[i], "", "")
resourceClient.On("Create", &expectedObjs[i]).Return(&expectedObjs[i], nil)
}
@ -594,7 +594,7 @@ func TestRestoreResourceForNamespace(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
resourceClient := &arktest.FakeDynamicClient{}
for i := range test.expectedObjs {
addRestoreLabel(&test.expectedObjs[i], "my-restore")
addRestoreLabels(&test.expectedObjs[i], "my-restore", "my-backup")
resourceClient.On("Create", &test.expectedObjs[i]).Return(&test.expectedObjs[i], nil)
}
@ -626,6 +626,7 @@ func TestRestoreResourceForNamespace(t *testing.T) {
},
Spec: api.RestoreSpec{
IncludeClusterResources: test.includeClusterResources,
BackupName: "my-backup",
},
},
backup: &api.Backup{},
@ -680,7 +681,7 @@ func TestRestoringExistingServiceAccount(t *testing.T) {
m[k] = v
}
fromBackupWithLabel := &unstructured.Unstructured{Object: m}
addRestoreLabel(fromBackupWithLabel, "my-restore")
addRestoreLabels(fromBackupWithLabel, "my-restore", "my-backup")
// resetMetadataAndStatus will strip the creationTimestamp before calling Create
fromBackupWithLabel.SetCreationTimestamp(metav1.Time{Time: time.Time{}})
@ -711,6 +712,7 @@ func TestRestoringExistingServiceAccount(t *testing.T) {
},
Spec: api.RestoreSpec{
IncludeClusterResources: nil,
BackupName: "my-backup",
},
},
backup: &api.Backup{},
@ -922,7 +924,7 @@ status:
}
resetMetadataAndStatus(unstructuredPV)
addRestoreLabel(unstructuredPV, ctx.restore.Name)
addRestoreLabels(unstructuredPV, ctx.restore.Name, ctx.restore.Spec.BackupName)
unstructuredPV.Object["foo"] = "bar"
if test.expectPVCreation {
@ -964,7 +966,7 @@ status:
unstructuredPVC := &unstructured.Unstructured{Object: unstructuredPVCMap}
resetMetadataAndStatus(unstructuredPVC)
addRestoreLabel(unstructuredPVC, ctx.restore.Name)
addRestoreLabels(unstructuredPVC, ctx.restore.Name, ctx.restore.Spec.BackupName)
createdPVC := unstructuredPVC.DeepCopy()
// just to ensure we have the data flowing correctly