Unverified Commit dc05e1e4 authored by Kubernetes Prow Robot's avatar Kubernetes Prow Robot Committed by GitHub

Merge pull request #76824 from wojtek-t/cleanup_protobuf_contenttype

Cleanup content-type protobuf constants
parents 5c4a59f9 b48cc43e
...@@ -121,7 +121,7 @@ func TestProtobufRoundTrip(t *testing.T) { ...@@ -121,7 +121,7 @@ func TestProtobufRoundTrip(t *testing.T) {
func BenchmarkEncodeCodecProtobuf(b *testing.B) { func BenchmarkEncodeCodecProtobuf(b *testing.B) {
items := benchmarkItems(b) items := benchmarkItems(b)
width := len(items) width := len(items)
s := protobuf.NewSerializer(nil, nil, "application/arbitrary.content.type") s := protobuf.NewSerializer(nil, nil)
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if _, err := runtime.Encode(s, &items[i%width]); err != nil { if _, err := runtime.Encode(s, &items[i%width]); err != nil {
...@@ -142,7 +142,7 @@ func BenchmarkEncodeCodecFromInternalProtobuf(b *testing.B) { ...@@ -142,7 +142,7 @@ func BenchmarkEncodeCodecFromInternalProtobuf(b *testing.B) {
b.Fatal(err) b.Fatal(err)
} }
} }
s := protobuf.NewSerializer(nil, nil, "application/arbitrary.content.type") s := protobuf.NewSerializer(nil, nil)
codec := legacyscheme.Codecs.EncoderForVersion(s, v1.SchemeGroupVersion) codec := legacyscheme.Codecs.EncoderForVersion(s, v1.SchemeGroupVersion)
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
...@@ -170,7 +170,7 @@ func BenchmarkEncodeProtobufGeneratedMarshal(b *testing.B) { ...@@ -170,7 +170,7 @@ func BenchmarkEncodeProtobufGeneratedMarshal(b *testing.B) {
func BenchmarkDecodeCodecToInternalProtobuf(b *testing.B) { func BenchmarkDecodeCodecToInternalProtobuf(b *testing.B) {
items := benchmarkItems(b) items := benchmarkItems(b)
width := len(items) width := len(items)
s := protobuf.NewSerializer(legacyscheme.Scheme, legacyscheme.Scheme, "application/arbitrary.content.type") s := protobuf.NewSerializer(legacyscheme.Scheme, legacyscheme.Scheme)
encoder := legacyscheme.Codecs.EncoderForVersion(s, v1.SchemeGroupVersion) encoder := legacyscheme.Codecs.EncoderForVersion(s, v1.SchemeGroupVersion)
var encoded [][]byte var encoded [][]byte
for i := range items { for i := range items {
......
...@@ -231,7 +231,7 @@ func roundTripToAllExternalVersions(t *testing.T, scheme *runtime.Scheme, codecF ...@@ -231,7 +231,7 @@ func roundTripToAllExternalVersions(t *testing.T, scheme *runtime.Scheme, codecF
// TODO remove this hack after we're past the intermediate steps // TODO remove this hack after we're past the intermediate steps
if !skipProtobuf && externalGVK.Group != "kubeadm.k8s.io" { if !skipProtobuf && externalGVK.Group != "kubeadm.k8s.io" {
s := protobuf.NewSerializer(scheme, scheme, "application/arbitrary.content.type") s := protobuf.NewSerializer(scheme, scheme)
protobufCodec := codecFactory.CodecForVersions(s, s, externalGVK.GroupVersion(), nil) protobufCodec := codecFactory.CodecForVersions(s, s, externalGVK.GroupVersion(), nil)
roundTrip(t, scheme, protobufCodec, object) roundTrip(t, scheme, protobufCodec, object)
} }
...@@ -260,7 +260,7 @@ func roundTripOfExternalType(t *testing.T, scheme *runtime.Scheme, codecFactory ...@@ -260,7 +260,7 @@ func roundTripOfExternalType(t *testing.T, scheme *runtime.Scheme, codecFactory
// TODO remove this hack after we're past the intermediate steps // TODO remove this hack after we're past the intermediate steps
if !skipProtobuf { if !skipProtobuf {
roundTrip(t, scheme, protobuf.NewSerializer(scheme, scheme, "application/protobuf"), object) roundTrip(t, scheme, protobuf.NewSerializer(scheme, scheme), object)
} }
} }
......
...@@ -32,7 +32,6 @@ go_library( ...@@ -32,7 +32,6 @@ go_library(
srcs = [ srcs = [
"codec_factory.go", "codec_factory.go",
"negotiated_codec.go", "negotiated_codec.go",
"protobuf_extension.go",
], ],
importmap = "k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/runtime/serializer", importmap = "k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/runtime/serializer",
importpath = "k8s.io/apimachinery/pkg/runtime/serializer", importpath = "k8s.io/apimachinery/pkg/runtime/serializer",
......
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer/json" "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/runtime/serializer/protobuf"
"k8s.io/apimachinery/pkg/runtime/serializer/recognizer" "k8s.io/apimachinery/pkg/runtime/serializer/recognizer"
"k8s.io/apimachinery/pkg/runtime/serializer/versioning" "k8s.io/apimachinery/pkg/runtime/serializer/versioning"
) )
...@@ -51,6 +52,8 @@ func newSerializersForScheme(scheme *runtime.Scheme, mf json.MetaFactory) []seri ...@@ -51,6 +52,8 @@ func newSerializersForScheme(scheme *runtime.Scheme, mf json.MetaFactory) []seri
jsonSerializer := json.NewSerializer(mf, scheme, scheme, false) jsonSerializer := json.NewSerializer(mf, scheme, scheme, false)
jsonPrettySerializer := json.NewSerializer(mf, scheme, scheme, true) jsonPrettySerializer := json.NewSerializer(mf, scheme, scheme, true)
yamlSerializer := json.NewYAMLSerializer(mf, scheme, scheme) yamlSerializer := json.NewYAMLSerializer(mf, scheme, scheme)
serializer := protobuf.NewSerializer(scheme, scheme)
raw := protobuf.NewRawSerializer(scheme, scheme)
serializers := []serializerType{ serializers := []serializerType{
{ {
...@@ -71,6 +74,15 @@ func newSerializersForScheme(scheme *runtime.Scheme, mf json.MetaFactory) []seri ...@@ -71,6 +74,15 @@ func newSerializersForScheme(scheme *runtime.Scheme, mf json.MetaFactory) []seri
EncodesAsText: true, EncodesAsText: true,
Serializer: yamlSerializer, Serializer: yamlSerializer,
}, },
{
AcceptContentTypes: []string{runtime.ContentTypeProtobuf},
ContentType: runtime.ContentTypeProtobuf,
FileExtensions: []string{"pb"},
Serializer: serializer,
Framer: protobuf.LengthDelimitedFramer,
StreamSerializer: raw,
},
} }
for _, fn := range serializerExtensions { for _, fn := range serializerExtensions {
......
...@@ -71,12 +71,11 @@ func IsNotMarshalable(err error) bool { ...@@ -71,12 +71,11 @@ func IsNotMarshalable(err error) bool {
// as-is (any type info passed with the object will be used). // as-is (any type info passed with the object will be used).
// //
// This encoding scheme is experimental, and is subject to change at any time. // This encoding scheme is experimental, and is subject to change at any time.
func NewSerializer(creater runtime.ObjectCreater, typer runtime.ObjectTyper, defaultContentType string) *Serializer { func NewSerializer(creater runtime.ObjectCreater, typer runtime.ObjectTyper) *Serializer {
return &Serializer{ return &Serializer{
prefix: protoEncodingPrefix, prefix: protoEncodingPrefix,
creater: creater, creater: creater,
typer: typer, typer: typer,
contentType: defaultContentType,
} }
} }
...@@ -84,7 +83,6 @@ type Serializer struct { ...@@ -84,7 +83,6 @@ type Serializer struct {
prefix []byte prefix []byte
creater runtime.ObjectCreater creater runtime.ObjectCreater
typer runtime.ObjectTyper typer runtime.ObjectTyper
contentType string
} }
var _ runtime.Serializer = &Serializer{} var _ runtime.Serializer = &Serializer{}
...@@ -138,7 +136,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i ...@@ -138,7 +136,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
if intoUnknown, ok := into.(*runtime.Unknown); ok && intoUnknown != nil { if intoUnknown, ok := into.(*runtime.Unknown); ok && intoUnknown != nil {
*intoUnknown = unk *intoUnknown = unk
if ok, _, _ := s.RecognizesData(bytes.NewBuffer(unk.Raw)); ok { if ok, _, _ := s.RecognizesData(bytes.NewBuffer(unk.Raw)); ok {
intoUnknown.ContentType = s.contentType intoUnknown.ContentType = runtime.ContentTypeProtobuf
} }
return intoUnknown, &actual, nil return intoUnknown, &actual, nil
} }
...@@ -303,11 +301,10 @@ func estimateUnknownSize(unk *runtime.Unknown, byteSize uint64) uint64 { ...@@ -303,11 +301,10 @@ func estimateUnknownSize(unk *runtime.Unknown, byteSize uint64) uint64 {
// encoded object, and thus is not self describing (callers must know what type is being described in order to decode). // encoded object, and thus is not self describing (callers must know what type is being described in order to decode).
// //
// This encoding scheme is experimental, and is subject to change at any time. // This encoding scheme is experimental, and is subject to change at any time.
func NewRawSerializer(creater runtime.ObjectCreater, typer runtime.ObjectTyper, defaultContentType string) *RawSerializer { func NewRawSerializer(creater runtime.ObjectCreater, typer runtime.ObjectTyper) *RawSerializer {
return &RawSerializer{ return &RawSerializer{
creater: creater, creater: creater,
typer: typer, typer: typer,
contentType: defaultContentType,
} }
} }
...@@ -316,7 +313,6 @@ func NewRawSerializer(creater runtime.ObjectCreater, typer runtime.ObjectTyper, ...@@ -316,7 +313,6 @@ func NewRawSerializer(creater runtime.ObjectCreater, typer runtime.ObjectTyper,
type RawSerializer struct { type RawSerializer struct {
creater runtime.ObjectCreater creater runtime.ObjectCreater
typer runtime.ObjectTyper typer runtime.ObjectTyper
contentType string
} }
var _ runtime.Serializer = &RawSerializer{} var _ runtime.Serializer = &RawSerializer{}
...@@ -358,7 +354,7 @@ func (s *RawSerializer) Decode(originalData []byte, gvk *schema.GroupVersionKind ...@@ -358,7 +354,7 @@ func (s *RawSerializer) Decode(originalData []byte, gvk *schema.GroupVersionKind
if intoUnknown, ok := into.(*runtime.Unknown); ok && intoUnknown != nil { if intoUnknown, ok := into.(*runtime.Unknown); ok && intoUnknown != nil {
intoUnknown.Raw = data intoUnknown.Raw = data
intoUnknown.ContentEncoding = "" intoUnknown.ContentEncoding = ""
intoUnknown.ContentType = s.contentType intoUnknown.ContentType = runtime.ContentTypeProtobuf
intoUnknown.SetGroupVersionKind(*actual) intoUnknown.SetGroupVersionKind(*actual)
return intoUnknown, actual, nil return intoUnknown, actual, nil
} }
......
/*
Copyright 2014 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package serializer
import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer/protobuf"
)
const (
// contentTypeProtobuf is the protobuf type exposed for Kubernetes. It is private to prevent others from
// depending on it unintentionally.
// TODO: potentially move to pkg/api (since it's part of the Kube public API) and pass it in to the
// CodecFactory on initialization.
contentTypeProtobuf = "application/vnd.kubernetes.protobuf"
)
func protobufSerializer(scheme *runtime.Scheme) (serializerType, bool) {
serializer := protobuf.NewSerializer(scheme, scheme, contentTypeProtobuf)
raw := protobuf.NewRawSerializer(scheme, scheme, contentTypeProtobuf)
return serializerType{
AcceptContentTypes: []string{contentTypeProtobuf},
ContentType: contentTypeProtobuf,
FileExtensions: []string{"pb"},
Serializer: serializer,
Framer: protobuf.LengthDelimitedFramer,
StreamSerializer: raw,
}, true
}
func init() {
serializerExtensions = append(serializerExtensions, protobufSerializer)
}
...@@ -84,7 +84,7 @@ func (d *testBufferedMarshalable) DeepCopyObject() runtime.Object { ...@@ -84,7 +84,7 @@ func (d *testBufferedMarshalable) DeepCopyObject() runtime.Object {
} }
func TestRecognize(t *testing.T) { func TestRecognize(t *testing.T) {
s := protobuf.NewSerializer(nil, nil, "application/protobuf") s := protobuf.NewSerializer(nil, nil)
ignores := [][]byte{ ignores := [][]byte{
nil, nil,
{}, {},
...@@ -172,7 +172,7 @@ func TestEncode(t *testing.T) { ...@@ -172,7 +172,7 @@ func TestEncode(t *testing.T) {
} }
for i, test := range testCases { for i, test := range testCases {
s := protobuf.NewSerializer(nil, nil, "application/protobuf") s := protobuf.NewSerializer(nil, nil)
data, err := runtime.Encode(s, test.obj) data, err := runtime.Encode(s, test.obj)
switch { switch {
...@@ -251,7 +251,7 @@ func TestProtobufDecode(t *testing.T) { ...@@ -251,7 +251,7 @@ func TestProtobufDecode(t *testing.T) {
Kind: "test", Kind: "test",
}, },
// content type is set because the prefix matches the content // content type is set because the prefix matches the content
ContentType: "application/protobuf", ContentType: runtime.ContentTypeProtobuf,
Raw: []byte{0x6b, 0x38, 0x73, 0x00, 0x01, 0x02, 0x03}, Raw: []byte{0x6b, 0x38, 0x73, 0x00, 0x01, 0x02, 0x03},
}, },
data: wire2, data: wire2,
...@@ -259,7 +259,7 @@ func TestProtobufDecode(t *testing.T) { ...@@ -259,7 +259,7 @@ func TestProtobufDecode(t *testing.T) {
} }
for i, test := range testCases { for i, test := range testCases {
s := protobuf.NewSerializer(nil, nil, "application/protobuf") s := protobuf.NewSerializer(nil, nil)
unk := &runtime.Unknown{} unk := &runtime.Unknown{}
err := runtime.DecodeInto(s, test.data, unk) err := runtime.DecodeInto(s, test.data, unk)
...@@ -334,7 +334,7 @@ func TestDecodeObjects(t *testing.T) { ...@@ -334,7 +334,7 @@ func TestDecodeObjects(t *testing.T) {
for i, test := range testCases { for i, test := range testCases {
scheme.AddKnownTypes(schema.GroupVersion{Version: "v1"}, &v1.Carp{}) scheme.AddKnownTypes(schema.GroupVersion{Version: "v1"}, &v1.Carp{})
require.NoError(t, v1.AddToScheme(scheme)) require.NoError(t, v1.AddToScheme(scheme))
s := protobuf.NewSerializer(scheme, scheme, "application/protobuf") s := protobuf.NewSerializer(scheme, scheme)
obj, err := runtime.Decode(s, test.data) obj, err := runtime.Decode(s, test.data)
switch { switch {
......
...@@ -971,7 +971,7 @@ func expectPartialObjectMetaEvents(t *testing.T, d *json.Decoder, values ...stri ...@@ -971,7 +971,7 @@ func expectPartialObjectMetaEvents(t *testing.T, d *json.Decoder, values ...stri
func expectPartialObjectMetaEventsProtobuf(t *testing.T, r io.Reader, values ...string) { func expectPartialObjectMetaEventsProtobuf(t *testing.T, r io.Reader, values ...string) {
scheme := runtime.NewScheme() scheme := runtime.NewScheme()
metav1.AddToGroupVersion(scheme, schema.GroupVersion{Version: "v1"}) metav1.AddToGroupVersion(scheme, schema.GroupVersion{Version: "v1"})
rs := protobuf.NewRawSerializer(scheme, scheme, "application/vnd.kubernetes.protobuf") rs := protobuf.NewRawSerializer(scheme, scheme)
d := streaming.NewDecoder( d := streaming.NewDecoder(
protobuf.LengthDelimitedFramer.NewFrameReader(ioutil.NopCloser(r)), protobuf.LengthDelimitedFramer.NewFrameReader(ioutil.NopCloser(r)),
rs, rs,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment