Skip to content

Commit

Permalink
Replace generated codec with runtime codec.
Browse files Browse the repository at this point in the history
Signed-off-by: Xun Jiang <[email protected]>
  • Loading branch information
Xun Jiang committed Nov 2, 2023
1 parent d7ad365 commit 1b5449a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
16 changes: 14 additions & 2 deletions pkg/persistence/object_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ import (

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime"
"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"
velerov2alpha1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v2alpha1"
"github.com/vmware-tanzu/velero/pkg/itemoperation"
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
"github.com/vmware-tanzu/velero/pkg/volume"
Expand Down Expand Up @@ -302,7 +304,17 @@ func (s *objectBackupStore) GetBackupMetadata(name string) (*velerov1api.Backup,
return nil, errors.WithStack(err)
}

decoder := scheme.Codecs.UniversalDecoder(velerov1api.SchemeGroupVersion)
veleroScheme := runtime.NewScheme()
localSchemeBuilder := runtime.SchemeBuilder{
velerov1api.AddToScheme,
velerov2alpha1.AddToScheme,
}
if err := localSchemeBuilder.AddToScheme(veleroScheme); err != nil {
return nil, errors.WithStack(err)
}

Check warning on line 314 in pkg/persistence/object_store.go

View check run for this annotation

Codecov / codecov/patch

pkg/persistence/object_store.go#L313-L314

Added lines #L313 - L314 were not covered by tests
codecFactory := serializer.NewCodecFactory(veleroScheme)

decoder := codecFactory.UniversalDecoder(velerov1api.SchemeGroupVersion)
obj, _, err := decoder.Decode(data, nil, nil)
if err != nil {
return nil, errors.WithStack(err)
Expand Down
18 changes: 15 additions & 3 deletions pkg/util/encode/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
v2alpha1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v2alpha1"
)

// Encode converts the provided object to the specified format
Expand Down Expand Up @@ -56,8 +57,19 @@ 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

veleroScheme := runtime.NewScheme()
localSchemeBuilder := runtime.SchemeBuilder{
v1.AddToScheme,
v2alpha1.AddToScheme,
}
if err := localSchemeBuilder.AddToScheme(veleroScheme); err != nil {
return nil, errors.WithStack(err)
}
codecFactory := serializer.NewCodecFactory(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)
}
Expand All @@ -69,7 +81,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
}

Expand Down

0 comments on commit 1b5449a

Please sign in to comment.