From 95b2b9000673da8c2fa379dbf7e491b57bc2f605 Mon Sep 17 00:00:00 2001 From: Steve Kriss Date: Wed, 8 Aug 2018 16:51:33 -0700 Subject: [PATCH] add backup name label to restored objects Signed-off-by: Steve Kriss --- pkg/restore/restore.go | 21 ++++++++++++--------- pkg/restore/restore_test.go | 12 +++++++----- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/pkg/restore/restore.go b/pkg/restore/restore.go index 3b79d9757..af2f1d350 100644 --- a/pkg/restore/restore.go +++ b/pkg/restore/restore.go @@ -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` diff --git a/pkg/restore/restore_test.go b/pkg/restore/restore_test.go index bdb4dbbd8..3a0f50864 100644 --- a/pkg/restore/restore_test.go +++ b/pkg/restore/restore_test.go @@ -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