From 2334709b33b67e1b608a96e2955e18b66f1d306b Mon Sep 17 00:00:00 2001 From: Xun Jiang Date: Wed, 1 Nov 2023 23:28:06 +0800 Subject: [PATCH] Replace generated codec with runtime codec. Also replace generated scheme. Signed-off-by: Xun Jiang --- internal/storage/storagelocation_test.go | 4 ++-- pkg/persistence/object_store.go | 7 +++++-- pkg/uploader/provider/kopia_test.go | 6 +++--- pkg/uploader/provider/provider_test.go | 4 ++-- pkg/uploader/provider/restic_test.go | 6 +++--- pkg/util/encode/encode.go | 10 +++++++--- pkg/util/scheme.go | 19 +++++++++++++++++++ 7 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 pkg/util/scheme.go diff --git a/internal/storage/storagelocation_test.go b/internal/storage/storagelocation_test.go index bd5a94aa90e..650e7338ab0 100644 --- a/internal/storage/storagelocation_test.go +++ b/internal/storage/storagelocation_test.go @@ -26,8 +26,8 @@ import ( velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" "github.com/vmware-tanzu/velero/pkg/builder" - "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme" velerotest "github.com/vmware-tanzu/velero/pkg/test" + "github.com/vmware-tanzu/velero/pkg/util" ) func TestIsReadyToValidate(t *testing.T) { @@ -163,7 +163,7 @@ func TestListBackupStorageLocations(t *testing.T) { t.Run(tt.name, func(t *testing.T) { g := NewWithT(t) - client := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(tt.backupLocations).Build() + client := fake.NewClientBuilder().WithScheme(util.VeleroScheme).WithRuntimeObjects(tt.backupLocations).Build() if tt.expectError { _, err := ListBackupStorageLocations(context.Background(), client, "ns-1") g.Expect(err).NotTo(BeNil()) diff --git a/pkg/persistence/object_store.go b/pkg/persistence/object_store.go index d0fd9c1f166..4e0ddbe913f 100644 --- a/pkg/persistence/object_store.go +++ b/pkg/persistence/object_store.go @@ -27,13 +27,14 @@ import ( "github.com/pkg/errors" "github.com/sirupsen/logrus" + "k8s.io/apimachinery/pkg/runtime/serializer" kerrors "k8s.io/apimachinery/pkg/util/errors" "github.com/vmware-tanzu/velero/internal/credentials" velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" - "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme" "github.com/vmware-tanzu/velero/pkg/itemoperation" "github.com/vmware-tanzu/velero/pkg/plugin/velero" + "github.com/vmware-tanzu/velero/pkg/util" "github.com/vmware-tanzu/velero/pkg/volume" ) @@ -302,7 +303,9 @@ func (s *objectBackupStore) GetBackupMetadata(name string) (*velerov1api.Backup, return nil, errors.WithStack(err) } - decoder := scheme.Codecs.UniversalDecoder(velerov1api.SchemeGroupVersion) + codecFactory := serializer.NewCodecFactory(util.VeleroScheme) + + decoder := codecFactory.UniversalDecoder(velerov1api.SchemeGroupVersion) obj, _, err := decoder.Decode(data, nil, nil) if err != nil { return nil, errors.WithStack(err) diff --git a/pkg/uploader/provider/kopia_test.go b/pkg/uploader/provider/kopia_test.go index c38d370ce35..e9c4fb7ef59 100644 --- a/pkg/uploader/provider/kopia_test.go +++ b/pkg/uploader/provider/kopia_test.go @@ -35,12 +35,12 @@ import ( "github.com/vmware-tanzu/velero/internal/credentials" "github.com/vmware-tanzu/velero/internal/credentials/mocks" velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" - "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme" "github.com/vmware-tanzu/velero/pkg/repository" udmrepo "github.com/vmware-tanzu/velero/pkg/repository/udmrepo" udmrepomocks "github.com/vmware-tanzu/velero/pkg/repository/udmrepo/mocks" "github.com/vmware-tanzu/velero/pkg/uploader" "github.com/vmware-tanzu/velero/pkg/uploader/kopia" + "github.com/vmware-tanzu/velero/pkg/util" ) type FakeBackupProgressUpdater struct { @@ -64,7 +64,7 @@ func (f *FakeRestoreProgressUpdater) UpdateProgress(p *uploader.Progress) {} func TestRunBackup(t *testing.T) { var kp kopiaProvider kp.log = logrus.New() - updater := FakeBackupProgressUpdater{PodVolumeBackup: &velerov1api.PodVolumeBackup{}, Log: kp.log, Ctx: context.Background(), Cli: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()} + updater := FakeBackupProgressUpdater{PodVolumeBackup: &velerov1api.PodVolumeBackup{}, Log: kp.log, Ctx: context.Background(), Cli: fake.NewClientBuilder().WithScheme(util.VeleroScheme).Build()} testCases := []struct { name string @@ -121,7 +121,7 @@ func TestRunBackup(t *testing.T) { func TestRunRestore(t *testing.T) { var kp kopiaProvider kp.log = logrus.New() - updater := FakeRestoreProgressUpdater{PodVolumeRestore: &velerov1api.PodVolumeRestore{}, Log: kp.log, Ctx: context.Background(), Cli: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()} + updater := FakeRestoreProgressUpdater{PodVolumeRestore: &velerov1api.PodVolumeRestore{}, Log: kp.log, Ctx: context.Background(), Cli: fake.NewClientBuilder().WithScheme(util.VeleroScheme).Build()} testCases := []struct { name string diff --git a/pkg/uploader/provider/provider_test.go b/pkg/uploader/provider/provider_test.go index 19e6668628a..e04ff78b839 100644 --- a/pkg/uploader/provider/provider_test.go +++ b/pkg/uploader/provider/provider_test.go @@ -28,7 +28,7 @@ import ( "github.com/vmware-tanzu/velero/internal/credentials" "github.com/vmware-tanzu/velero/internal/credentials/mocks" velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" - "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme" + "github.com/vmware-tanzu/velero/pkg/util" ) type NewUploaderProviderTestCase struct { @@ -42,7 +42,7 @@ type NewUploaderProviderTestCase struct { func TestNewUploaderProvider(t *testing.T) { // Mock objects or dependencies ctx := context.Background() - client := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build() + client := fake.NewClientBuilder().WithScheme(util.VeleroScheme).Build() repoIdentifier := "repoIdentifier" bsl := &velerov1api.BackupStorageLocation{} backupRepo := &velerov1api.BackupRepository{} diff --git a/pkg/uploader/provider/restic_test.go b/pkg/uploader/provider/restic_test.go index 62f44d04f3f..038657cf936 100644 --- a/pkg/uploader/provider/restic_test.go +++ b/pkg/uploader/provider/restic_test.go @@ -33,9 +33,9 @@ import ( "github.com/vmware-tanzu/velero/internal/credentials" velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" "github.com/vmware-tanzu/velero/pkg/builder" - "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme" "github.com/vmware-tanzu/velero/pkg/restic" "github.com/vmware-tanzu/velero/pkg/uploader" + "github.com/vmware-tanzu/velero/pkg/util" "github.com/vmware-tanzu/velero/pkg/util/filesystem" ) @@ -148,7 +148,7 @@ func TestResticRunBackup(t *testing.T) { tc.volMode = uploader.PersistentVolumeFilesystem } if !tc.nilUpdater { - updater := FakeBackupProgressUpdater{PodVolumeBackup: &velerov1api.PodVolumeBackup{}, Log: tc.rp.log, Ctx: context.Background(), Cli: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()} + updater := FakeBackupProgressUpdater{PodVolumeBackup: &velerov1api.PodVolumeBackup{}, Log: tc.rp.log, Ctx: context.Background(), Cli: fake.NewClientBuilder().WithScheme(util.VeleroScheme).Build()} _, _, err = tc.rp.RunBackup(context.Background(), "var", "", map[string]string{}, false, parentSnapshot, tc.volMode, &updater) } else { _, _, err = tc.rp.RunBackup(context.Background(), "var", "", map[string]string{}, false, parentSnapshot, tc.volMode, nil) @@ -221,7 +221,7 @@ func TestResticRunRestore(t *testing.T) { } var err error if !tc.nilUpdater { - updater := FakeBackupProgressUpdater{PodVolumeBackup: &velerov1api.PodVolumeBackup{}, Log: tc.rp.log, Ctx: context.Background(), Cli: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()} + updater := FakeBackupProgressUpdater{PodVolumeBackup: &velerov1api.PodVolumeBackup{}, Log: tc.rp.log, Ctx: context.Background(), Cli: fake.NewClientBuilder().WithScheme(util.VeleroScheme).Build()} err = tc.rp.RunRestore(context.Background(), "", "var", tc.volMode, &updater) } else { err = tc.rp.RunRestore(context.Background(), "", "var", tc.volMode, nil) diff --git a/pkg/util/encode/encode.go b/pkg/util/encode/encode.go index 88703ad6ea6..9704c1a2e15 100644 --- a/pkg/util/encode/encode.go +++ b/pkg/util/encode/encode.go @@ -25,9 +25,10 @@ import ( "github.com/pkg/errors" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/serializer" v1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" - "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme" + "github.com/vmware-tanzu/velero/pkg/util" ) // Encode converts the provided object to the specified format @@ -56,8 +57,11 @@ func To(obj runtime.Object, format string, w io.Writer) error { // Only objects registered in the velero scheme, or objects with their TypeMeta set will have valid encoders. func EncoderFor(format string, obj runtime.Object) (runtime.Encoder, error) { var encoder runtime.Encoder + + codecFactory := serializer.NewCodecFactory(util.VeleroScheme) + desiredMediaType := fmt.Sprintf("application/%s", format) - serializerInfo, found := runtime.SerializerInfoForMediaType(scheme.Codecs.SupportedMediaTypes(), desiredMediaType) + serializerInfo, found := runtime.SerializerInfoForMediaType(codecFactory.SupportedMediaTypes(), desiredMediaType) if !found { return nil, errors.Errorf("unable to locate an encoder for %q", desiredMediaType) } @@ -69,7 +73,7 @@ func EncoderFor(format string, obj runtime.Object) (runtime.Encoder, error) { if !obj.GetObjectKind().GroupVersionKind().Empty() { return encoder, nil } - encoder = scheme.Codecs.EncoderForVersion(encoder, v1.SchemeGroupVersion) + encoder = codecFactory.EncoderForVersion(encoder, v1.SchemeGroupVersion) return encoder, nil } diff --git a/pkg/util/scheme.go b/pkg/util/scheme.go new file mode 100644 index 00000000000..7e5703fac73 --- /dev/null +++ b/pkg/util/scheme.go @@ -0,0 +1,19 @@ +package util + +import ( + "k8s.io/apimachinery/pkg/runtime" + utilruntime "k8s.io/apimachinery/pkg/util/runtime" + + v1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" + "github.com/vmware-tanzu/velero/pkg/apis/velero/v2alpha1" +) + +var VeleroScheme = runtime.NewScheme() + +func init() { + localSchemeBuilder := runtime.SchemeBuilder{ + v1.AddToScheme, + v2alpha1.AddToScheme, + } + utilruntime.Must(localSchemeBuilder.AddToScheme(VeleroScheme)) +}