From b03da3c0ed65b9b9da20471266ded53d47751dbe Mon Sep 17 00:00:00 2001 From: Steve Kriss Date: Wed, 27 Mar 2019 16:49:42 -0600 Subject: [PATCH] remove code referencing Ark API pkg Signed-off-by: Steve Kriss --- pkg/persistence/object_store.go | 87 +---------------------- pkg/persistence/object_store_layout.go | 5 -- pkg/persistence/object_store_test.go | 97 +------------------------- 3 files changed, 2 insertions(+), 187 deletions(-) diff --git a/pkg/persistence/object_store.go b/pkg/persistence/object_store.go index 03cec5a83..b3390e928 100644 --- a/pkg/persistence/object_store.go +++ b/pkg/persistence/object_store.go @@ -27,8 +27,6 @@ import ( "github.com/pkg/errors" uuid "github.com/satori/go.uuid" "github.com/sirupsen/logrus" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" kerrors "k8s.io/apimachinery/pkg/util/errors" velerov1api "github.com/heptio/velero/pkg/apis/velero/v1" @@ -208,48 +206,7 @@ func (s *objectBackupStore) PutBackup(name string, metadata, contents, log, volu } func (s *objectBackupStore) GetBackupMetadata(name string) (*velerov1api.Backup, error) { - // We need to determine whether the backup metadata file is the legacy ark.heptio.com - // one (named ark-backup.json) or the current velero.io one (named velero-backup.json). - // Listing all objects in the backup directory and searching for them is easiest, because - // GetObject() calls don't immediately return an error if the object is not found due to - // a bug related to the plugin infrastructure, and even if they did, it's difficult to - // distinguish between a 404 and a different error. - // - // TODO once the plugin/error-related bugs are fixed, simplify this code by just calling - // GetObject() to check existence of the metadata files. - keys, err := s.objectStore.ListObjects(s.bucket, s.layout.getBackupDir(name)) - if err != nil { - return nil, errors.WithStack(err) - } - - var ( - metadataKey = s.layout.getBackupMetadataKey(name) - legacyMetadataKey = s.layout.getLegacyBackupMetadataKey(name) - legacyMetadata bool - ) - - var found bool - for _, key := range keys { - switch key { - case metadataKey: - found = true - case legacyMetadataKey: - found = true - legacyMetadata = true - } - - if found { - break - } - } - - if legacyMetadata { - s.logger.WithField("backup", name).Debug("Legacy metadata file found, converting") - return s.getAndConvertLegacyBackupMetadata(legacyMetadataKey) - } - - // TODO(1.0): remove everything in this method from here up, except the metadataKey - // declaration. + metadataKey := s.layout.getBackupMetadataKey(name) res, err := s.objectStore.GetObject(s.bucket, metadataKey) if err != nil { @@ -276,48 +233,6 @@ func (s *objectBackupStore) GetBackupMetadata(name string) (*velerov1api.Backup, return backupObj, nil } -// TODO(1.0): remove -func (s *objectBackupStore) getAndConvertLegacyBackupMetadata(key string) (*velerov1api.Backup, error) { - obj, err := s.objectStore.GetObject(s.bucket, key) - if err != nil { - return nil, err - } - - data, err := ioutil.ReadAll(obj) - if err != nil { - return nil, errors.WithStack(err) - } - - res := new(unstructured.Unstructured) - if err := json.Unmarshal(data, &res); err != nil { - return nil, errors.WithStack(err) - } - - res.SetAPIVersion(velerov1api.SchemeGroupVersion.String()) - res.SetLabels(convertMapKeys(res.GetLabels(), "ark.heptio.com", "velero.io")) - res.SetLabels(convertMapKeys(res.GetLabels(), "ark-schedule", velerov1api.ScheduleNameLabel)) - res.SetAnnotations(convertMapKeys(res.GetAnnotations(), "ark.heptio.com", "velero.io")) - - backup := new(velerov1api.Backup) - if err := runtime.DefaultUnstructuredConverter.FromUnstructured(res.Object, backup); err != nil { - return nil, errors.WithStack(err) - } - - return backup, nil -} - -// TODO(1.0): remove -func convertMapKeys(m map[string]string, find, replace string) map[string]string { - for k, v := range m { - if updatedKey := strings.Replace(k, find, replace, -1); updatedKey != k { - m[updatedKey] = v - delete(m, k) - } - } - - return m -} - func keyExists(objectStore velero.ObjectStore, bucket, prefix, key string) (bool, error) { keys, err := objectStore.ListObjects(bucket, prefix) if err != nil { diff --git a/pkg/persistence/object_store_layout.go b/pkg/persistence/object_store_layout.go index b8e0526d5..55dc77f6a 100644 --- a/pkg/persistence/object_store_layout.go +++ b/pkg/persistence/object_store_layout.go @@ -75,11 +75,6 @@ func (l *ObjectStoreLayout) getBackupMetadataKey(backup string) string { return path.Join(l.subdirs["backups"], backup, "velero-backup.json") } -// TODO(1.0): remove -func (l *ObjectStoreLayout) getLegacyBackupMetadataKey(backup string) string { - return path.Join(l.subdirs["backups"], backup, "ark-backup.json") -} - func (l *ObjectStoreLayout) getBackupContentsKey(backup string) string { return path.Join(l.subdirs["backups"], backup, fmt.Sprintf("%s.tar.gz", backup)) } diff --git a/pkg/persistence/object_store_test.go b/pkg/persistence/object_store_test.go index 730d134e7..a7c093743 100644 --- a/pkg/persistence/object_store_test.go +++ b/pkg/persistence/object_store_test.go @@ -34,7 +34,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" - arkv1api "github.com/heptio/velero/pkg/apis/ark/v1" velerov1api "github.com/heptio/velero/pkg/apis/velero/v1" "github.com/heptio/velero/pkg/cloudprovider" cloudprovidermocks "github.com/heptio/velero/pkg/cloudprovider/mocks" @@ -316,22 +315,7 @@ func TestGetBackupMetadata(t *testing.T) { wantErr error }{ { - name: "legacy metadata file returns correctly", - backupName: "foo", - key: "backups/foo/ark-backup.json", - obj: &arkv1api.Backup{ - TypeMeta: metav1.TypeMeta{ - Kind: "Backup", - APIVersion: arkv1api.SchemeGroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Namespace: arkv1api.DefaultNamespace, - Name: "foo", - }, - }, - }, - { - name: "current metadata file returns correctly", + name: "metadata file returns correctly", backupName: "foo", key: "backups/foo/velero-backup.json", obj: &velerov1api.Backup{ @@ -376,85 +360,6 @@ func TestGetBackupMetadata(t *testing.T) { } } -func TestGetAndConvertLegacyBackupMetadata(t *testing.T) { - tests := []struct { - name string - key string - obj metav1.Object - want *velerov1api.Backup - wantErr error - }{ - { - name: "velerov1api group, labels and annotations all get converted", - key: "backups/foo/ark-backup.json", - obj: &arkv1api.Backup{ - TypeMeta: metav1.TypeMeta{ - Kind: "Backup", - APIVersion: arkv1api.SchemeGroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Namespace: arkv1api.DefaultNamespace, - Name: "foo", - Labels: map[string]string{ - "ark.heptio.com/foo": "bar", - "ark.heptio.com/tango": "foxtrot", - "prefix.ark.heptio.com/zaz": "zoo", - "non-matching": "no-change", - }, - Annotations: map[string]string{ - "ark.heptio.com/foo": "bar", - "ark.heptio.com/tango": "foxtrot", - "prefix.ark.heptio.com/zaz": "zoo", - "non-matching": "no-change", - }, - }, - }, - want: &velerov1api.Backup{ - TypeMeta: metav1.TypeMeta{ - Kind: "Backup", - APIVersion: velerov1api.SchemeGroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Namespace: arkv1api.DefaultNamespace, - Name: "foo", - Labels: map[string]string{ - "velero.io/foo": "bar", - "velero.io/tango": "foxtrot", - "prefix.velero.io/zaz": "zoo", - "non-matching": "no-change", - }, - Annotations: map[string]string{ - "velero.io/foo": "bar", - "velero.io/tango": "foxtrot", - "prefix.velero.io/zaz": "zoo", - "non-matching": "no-change", - }, - }, - }, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - harness := newObjectBackupStoreTestHarness("test-bucket", "") - - jsonBytes, err := json.Marshal(tc.obj) - require.NoError(t, err) - - require.NoError(t, harness.objectStore.PutObject(harness.bucket, tc.key, bytes.NewReader(jsonBytes))) - - res, err := harness.getAndConvertLegacyBackupMetadata(tc.key) - if tc.wantErr != nil { - assert.Equal(t, tc.wantErr, err) - } else { - require.NoError(t, err) - - assert.Equal(t, tc.want, res) - } - }) - } -} - func TestGetBackupVolumeSnapshots(t *testing.T) { harness := newObjectBackupStoreTestHarness("test-bucket", "")