summaryrefslogtreecommitdiff
path: root/vendor/k8s.io/apimachinery/pkg/runtime/serializer
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/k8s.io/apimachinery/pkg/runtime/serializer')
-rw-r--r--vendor/k8s.io/apimachinery/pkg/runtime/serializer/codec_factory.go100
-rw-r--r--vendor/k8s.io/apimachinery/pkg/runtime/serializer/protobuf/protobuf.go21
-rw-r--r--vendor/k8s.io/apimachinery/pkg/runtime/serializer/versioning/versioning.go8
3 files changed, 97 insertions, 32 deletions
diff --git a/vendor/k8s.io/apimachinery/pkg/runtime/serializer/codec_factory.go b/vendor/k8s.io/apimachinery/pkg/runtime/serializer/codec_factory.go
index 01f56c987..d1d407397 100644
--- a/vendor/k8s.io/apimachinery/pkg/runtime/serializer/codec_factory.go
+++ b/vendor/k8s.io/apimachinery/pkg/runtime/serializer/codec_factory.go
@@ -48,28 +48,40 @@ type serializerType struct {
StreamSerializer runtime.Serializer
}
-func newSerializersForScheme(scheme *runtime.Scheme, mf json.MetaFactory) []serializerType {
- jsonSerializer := json.NewSerializer(mf, scheme, scheme, false)
- jsonPrettySerializer := json.NewSerializer(mf, scheme, scheme, true)
- yamlSerializer := json.NewYAMLSerializer(mf, scheme, scheme)
- serializer := protobuf.NewSerializer(scheme, scheme)
- raw := protobuf.NewRawSerializer(scheme, scheme)
+func newSerializersForScheme(scheme *runtime.Scheme, mf json.MetaFactory, options CodecFactoryOptions) []serializerType {
+ jsonSerializer := json.NewSerializerWithOptions(
+ mf, scheme, scheme,
+ json.SerializerOptions{Yaml: false, Pretty: false, Strict: options.Strict},
+ )
+ jsonSerializerType := serializerType{
+ AcceptContentTypes: []string{runtime.ContentTypeJSON},
+ ContentType: runtime.ContentTypeJSON,
+ FileExtensions: []string{"json"},
+ EncodesAsText: true,
+ Serializer: jsonSerializer,
+
+ Framer: json.Framer,
+ StreamSerializer: jsonSerializer,
+ }
+ if options.Pretty {
+ jsonSerializerType.PrettySerializer = json.NewSerializerWithOptions(
+ mf, scheme, scheme,
+ json.SerializerOptions{Yaml: false, Pretty: true, Strict: options.Strict},
+ )
+ }
- serializers := []serializerType{
- {
- AcceptContentTypes: []string{"application/json"},
- ContentType: "application/json",
- FileExtensions: []string{"json"},
- EncodesAsText: true,
- Serializer: jsonSerializer,
- PrettySerializer: jsonPrettySerializer,
+ yamlSerializer := json.NewSerializerWithOptions(
+ mf, scheme, scheme,
+ json.SerializerOptions{Yaml: true, Pretty: false, Strict: options.Strict},
+ )
+ protoSerializer := protobuf.NewSerializer(scheme, scheme)
+ protoRawSerializer := protobuf.NewRawSerializer(scheme, scheme)
- Framer: json.Framer,
- StreamSerializer: jsonSerializer,
- },
+ serializers := []serializerType{
+ jsonSerializerType,
{
- AcceptContentTypes: []string{"application/yaml"},
- ContentType: "application/yaml",
+ AcceptContentTypes: []string{runtime.ContentTypeYAML},
+ ContentType: runtime.ContentTypeYAML,
FileExtensions: []string{"yaml"},
EncodesAsText: true,
Serializer: yamlSerializer,
@@ -78,10 +90,10 @@ func newSerializersForScheme(scheme *runtime.Scheme, mf json.MetaFactory) []seri
AcceptContentTypes: []string{runtime.ContentTypeProtobuf},
ContentType: runtime.ContentTypeProtobuf,
FileExtensions: []string{"pb"},
- Serializer: serializer,
+ Serializer: protoSerializer,
Framer: protobuf.LengthDelimitedFramer,
- StreamSerializer: raw,
+ StreamSerializer: protoRawSerializer,
},
}
@@ -104,14 +116,56 @@ type CodecFactory struct {
legacySerializer runtime.Serializer
}
+// CodecFactoryOptions holds the options for configuring CodecFactory behavior
+type CodecFactoryOptions struct {
+ // Strict configures all serializers in strict mode
+ Strict bool
+ // Pretty includes a pretty serializer along with the non-pretty one
+ Pretty bool
+}
+
+// CodecFactoryOptionsMutator takes a pointer to an options struct and then modifies it.
+// Functions implementing this type can be passed to the NewCodecFactory() constructor.
+type CodecFactoryOptionsMutator func(*CodecFactoryOptions)
+
+// EnablePretty enables including a pretty serializer along with the non-pretty one
+func EnablePretty(options *CodecFactoryOptions) {
+ options.Pretty = true
+}
+
+// DisablePretty disables including a pretty serializer along with the non-pretty one
+func DisablePretty(options *CodecFactoryOptions) {
+ options.Pretty = false
+}
+
+// EnableStrict enables configuring all serializers in strict mode
+func EnableStrict(options *CodecFactoryOptions) {
+ options.Strict = true
+}
+
+// DisableStrict disables configuring all serializers in strict mode
+func DisableStrict(options *CodecFactoryOptions) {
+ options.Strict = false
+}
+
// NewCodecFactory provides methods for retrieving serializers for the supported wire formats
// and conversion wrappers to define preferred internal and external versions. In the future,
// as the internal version is used less, callers may instead use a defaulting serializer and
// only convert objects which are shared internally (Status, common API machinery).
+//
+// Mutators can be passed to change the CodecFactoryOptions before construction of the factory.
+// It is recommended to explicitly pass mutators instead of relying on defaults.
+// By default, Pretty is enabled -- this is conformant with previously supported behavior.
+//
// TODO: allow other codecs to be compiled in?
// TODO: accept a scheme interface
-func NewCodecFactory(scheme *runtime.Scheme) CodecFactory {
- serializers := newSerializersForScheme(scheme, json.DefaultMetaFactory)
+func NewCodecFactory(scheme *runtime.Scheme, mutators ...CodecFactoryOptionsMutator) CodecFactory {
+ options := CodecFactoryOptions{Pretty: true}
+ for _, fn := range mutators {
+ fn(&options)
+ }
+
+ serializers := newSerializersForScheme(scheme, json.DefaultMetaFactory, options)
return newCodecFactory(scheme, serializers)
}
diff --git a/vendor/k8s.io/apimachinery/pkg/runtime/serializer/protobuf/protobuf.go b/vendor/k8s.io/apimachinery/pkg/runtime/serializer/protobuf/protobuf.go
index 8af889d35..0f33e1d82 100644
--- a/vendor/k8s.io/apimachinery/pkg/runtime/serializer/protobuf/protobuf.go
+++ b/vendor/k8s.io/apimachinery/pkg/runtime/serializer/protobuf/protobuf.go
@@ -203,7 +203,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
switch t := obj.(type) {
case bufferedMarshaller:
// this path performs a single allocation during write but requires the caller to implement
- // the more efficient Size and MarshalTo methods
+ // the more efficient Size and MarshalToSizedBuffer methods
encodedSize := uint64(t.Size())
estimatedSize := prefixSize + estimateUnknownSize(&unk, encodedSize)
data := make([]byte, estimatedSize)
@@ -283,6 +283,12 @@ type bufferedMarshaller interface {
runtime.ProtobufMarshaller
}
+// Like bufferedMarshaller, but is able to marshal backwards, which is more efficient since it doesn't call Size() as frequently.
+type bufferedReverseMarshaller interface {
+ proto.Sizer
+ runtime.ProtobufReverseMarshaller
+}
+
// estimateUnknownSize returns the expected bytes consumed by a given runtime.Unknown
// object with a nil RawJSON struct and the expected size of the provided buffer. The
// returned size will not be correct if RawJSOn is set on unk.
@@ -414,6 +420,19 @@ func unmarshalToObject(typer runtime.ObjectTyper, creater runtime.ObjectCreater,
// Encode serializes the provided object to the given writer. Overrides is ignored.
func (s *RawSerializer) Encode(obj runtime.Object, w io.Writer) error {
switch t := obj.(type) {
+ case bufferedReverseMarshaller:
+ // this path performs a single allocation during write but requires the caller to implement
+ // the more efficient Size and MarshalToSizedBuffer methods
+ encodedSize := uint64(t.Size())
+ data := make([]byte, encodedSize)
+
+ n, err := t.MarshalToSizedBuffer(data)
+ if err != nil {
+ return err
+ }
+ _, err = w.Write(data[:n])
+ return err
+
case bufferedMarshaller:
// this path performs a single allocation during write but requires the caller to implement
// the more efficient Size and MarshalTo methods
diff --git a/vendor/k8s.io/apimachinery/pkg/runtime/serializer/versioning/versioning.go b/vendor/k8s.io/apimachinery/pkg/runtime/serializer/versioning/versioning.go
index a04a2e98b..ee5cb86f7 100644
--- a/vendor/k8s.io/apimachinery/pkg/runtime/serializer/versioning/versioning.go
+++ b/vendor/k8s.io/apimachinery/pkg/runtime/serializer/versioning/versioning.go
@@ -230,11 +230,3 @@ func (c *codec) Encode(obj runtime.Object, w io.Writer) error {
// Conversion is responsible for setting the proper group, version, and kind onto the outgoing object
return c.encoder.Encode(out, w)
}
-
-// DirectEncoder was moved and renamed to runtime.WithVersionEncoder in 1.15.
-// TODO: remove in 1.16.
-type DirectEncoder = runtime.WithVersionEncoder
-
-// DirectDecoder was moved and renamed to runtime.WithoutVersionDecoder in 1.15.
-// TODO: remove in 1.16.
-type DirectDecoder = runtime.WithoutVersionDecoder